⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

AJAX requery on Add/Edit form



davidoster

davidoster
  • profile picture
  • Member

Posted 04 November 2012 - 12:36 PM

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

When I add(or edit) a new sale and I choose the product [b]group[/b](from a list of groups)[b] [/b]I want to requery the field of [b]products[/b] 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!

davidoster

davidoster
  • profile picture
  • Member

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

victor

victor
  • profile picture
  • Member

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.