Jump to content


AJAX requery on Add/Edit form

add edit requery datatables dependable field ajax

  • Please log in to reply
2 replies to this topic

#1 davidoster

davidoster

    Grocery CRUD Ninja

  • Advanced Member
  • PipPipPip
  • 1,068 posts
  • LocationAthens, Greece

Posted 04 November 2012 - 12:36 PM

I am using the datatables theme and I have a table e.g. sales.
Table sales has a field group_product(explained just under).
I also have two more tables, products and groups of products.
Table products has a field group_product that via set_relation gets a value from groups(hence each product belongs to one group).

When I add(or edit) a new sale and I choose the product group(from a list of groups) I want to requery the field of products so it displays ONLY the products that belong to this specific group, on the fly(via AJAX).

Any ideas how this can be done?

I tried using callback but the callback doesn't allow to ajaxify the queries!

____________________________________________________________

 

rtfm_small.jpg---!!!Please read these guidelines before asking to the forums!!!---

____________________________________________________________

 

David Oster aka George Pasparakis,
http://odphotography.com
http://eletter.gr


#2 davidoster

davidoster

    Grocery CRUD Ninja

  • Advanced Member
  • PipPipPip
  • 1,068 posts
  • LocationAthens, Greece

Posted 16 November 2012 - 08:35 AM

The only way I found up till now is by binding on the click of the first select a function that makes a json call, clears the select and ul of the second select and loads the new options and elements on the list. This works partially great, because even though when the select and the list is fully loaded with the new options and items,on the click of the second select, the old items come up again limited to the number of the new items that have being loaded to the select.
I hope somebody knows the answer and responds since it seems no one has posted anything all this time.
I post my function for your reference, under assets/grocery_crud/themes/datatables/js/datatables-add.js

$('#field-activities_id_chzn').click(function(){
$.ajax({
url: my_url,
dataType: 'json',
success: function(data) {
	 var select_text, select, option, ul;
select = document.getElementById('field-groups_id');
	 select.options.length = 0;
	 select.options.add(new Option());
	 ul = $('#field-groups_id_chzn ul');
	 ul.children().remove();
	 for (var x = 0; x < data.length; x++) {
	 var day, hour, description, y;
	 select_text = '';
	 y = 0;
	 if(data[x].day == null) day = '&nbsp;'; else day = data[x].day;
	 if(data[x].hour == null) hour = '&nbsp;'; else hour = data[x].hour;
	 if(data[x].description == null) description = '&nbsp;'; else description = data[x].description;
	 select_text = day + ' ' + hour + ' ' + description;
	 select.options.add(new Option(select_text,data[x].id));
	 y = x + 1;
	 ul.append('<li id="field-groups_id_chzn_o_' + y + '" class="active-result" style="">' + day + ' ' + hour + ' ' + description + '</li>');
     $('#field-groups_id').each(function() {$(this).trigger('listz:updated')});
	 }
}
});
});
Within the forms the jquery plugin chosen is used, to help the creation of the forms

Edited by David Olive Tree Oster, 20 November 2012 - 10:29 PM.

____________________________________________________________

 

rtfm_small.jpg---!!!Please read these guidelines before asking to the forums!!!---

____________________________________________________________

 

David Oster aka George Pasparakis,
http://odphotography.com
http://eletter.gr


#3 victor

victor

    grocery CRUD Hero

  • Advanced Member
  • PipPipPip
  • 967 posts
  • LocationMinsk

Posted 21 November 2012 - 08:42 AM

send me your database dump, controller and js. -------- . When I'll have a time I'll take a look on your code.
P.S. I'm busy today, sorry.





Also tagged with one or more of these keywords: add, edit, requery, datatables, dependable field, ajax

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users