Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC:

How to Ajaxify your relationship with Cook 07 Feb 2017 18:25 #14998

  • vlemos
  • vlemos's Avatar Topic Author
  • Online
  • Elite Member
  • Elite Member
  • Posts: 295
  • Thank you received: 41
How to Ajaxify your relationship with Cook

Update field type to Ajax:
<field name="users"
		alias="_user_id_name"
		label="TEST_FIELD_NAME"
		filter="array"
		multiple="true"
		type="ckajax"
		labelKey="_user_id_name"
		ajaxContext="test.users.ajax.users"/>

Build an Ajax routine to return the html element:
case 'users':
/* Ajax List : Test
 * Called from: view:test, layout:test
 */

	// Get only the selected items from the pivot table.
	// Remember to create the filter on the pivot model
	$model = CkJModel::getInstance('Pivot', 'XxxModel');
	$model->setState('filter.group', 'Registered');
	$items = $model->getItems();
	foreach ($items as $key => $item)
		$selected[] = $item->user_id;

	// Get all the registered users from the users table
	// to populate the dropdown
	$model = $this->getModel();
	$model->setState('filter.group_title', 'Registered');
	$items = $model->getItems();

	// Build the selectbox and mark the selected entries
	// This will create a standard select on the target form
	echo '<input type="hidden" name=jform[users][] value=""/>';
	echo '<select id="jform_users" name="jform[users][]"  multiple>';
		foreach ($items as $key => $item)
			echo "<option". (in_array($item->id, $selected) ? " selected " : " ") . "value=\"" . $item->id . "\">" . $item->_user_id_name . "</option>";
	echo '</select>';

	// Here is the magic, Cook uses "chosen" to transform the 
	// selectbox. You can pass the jQuery call as below or
	// place it in the ajax Complete in a js file or wherever 
	echo '<script>jQuery("select[id$=\'jform_users\']").chosen({"disable_search_threshold":10,"search_contains":true,"allow_single_deselect":true,"placeholder_text_multiple":"Type or select some options","placeholder_text_single":"Select an option","no_results_text":"No results match"});';

	break;

The multi-select relationship now works like the native cook n:m box but loads after the form is built. The model code is missing but is child's play for most cook users.

This idea can be applied to any n:m relation.

Good luck. . . .

Please Log in or Create an account to join the conversation.

Last edit: by vlemos.

How to Ajaxify your relationship with Cook :evil: 07 Feb 2017 18:28 #14999

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 986
All you wrote is soon deprecated.
Ajax fields will not anymore use any view function.
All the struff will be handled trough XML files (fields + filters)
This is another surprise coming for 3.2 with the new fields.
I can't wait to show you the achieved works.

Anyways K++ for this lesson you share here
Coding is now a piece of cake
The following user(s) said Thank You: vlemos

Please Log in or Create an account to join the conversation.

  • Page:
  • 1
Time to create page: 0.074 seconds

For starters it's just so easy to design an app in a way that I'm used to i.e. database first then views followed by customization. The fork system is pure brilliance from a developer standpoint as I can override things and still add and update my projects with minimal effort! Truly amazing to be able to build components in Joomla using incremental refinement without having to do everything by hand. Thanks for the great tool! I am so much more productive now than ever and I can't imagine building components any other way!!!!
Dave (Forum)

Get Started