I have 2 selectbox multiple. after selecting options in the 2 selectboxes, i need to add rows with input to a table, and only AFTER the rows were added to the table, i need to run ajax queries to populate each input with relevant data. here's my code:
$("#items, #sites").change(function () {
var itemsCount = $('#items option:selected').length;
var sitesCount = $('#sites option:selected').length;
if (sitesCount > 0 && itemsCount > 0){
popTable(itemsCount, sitesCount);
getZicouiData();
}
});
function popTable(itemsCount, sitesCount){
$('#cutbacks > tbody').empty();
var row = '';
$('#items option:selected').each(function(){
var item_id = $(this).val();
row +='<tr>';
row += '<td rowspan="'+sitesCount+'">'+$(this).text()+'</td>';
var site_i = 0;
$('#sites option:selected').each(function(){
var site_id = $(this).val();
if(site_i != 0){
row +='<tr>';
}
row += '<td>'+$(this).text()+'</td><td><input type="text" class="form-control waiting" data-site-id="'+site_id+'" data-item-id="'+item_id+'" value="מעדכן נתונים"/></td>';
row += '</tr>';
site_i ++
});
});
$('#cutbacks > tbody').append(row);
}
function getZicouiData(){
$('.waiting').each(function(){
var item_id = $(this).attr('data-item-id');
var site_id = $(this).attr('data-site-id');
url = '{{url('/')}}/sales/item_by_sale/'+item_id+'/'+site_id;
ordered_items = $.getData(url).total;
$(this).val(ordered_items);
});
}
jQuery.extend
(
{
getData: function(url)
{
var result = null;
$.ajax(
{
url: url,
type: 'get',
dataType: 'json',
async: false,
cache: false,
success: function(data)
{
result = data;
}
}
);
return result;
}
}
);
and here my html:
<select id='items' class='items' multiple>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
</select>
<select id='sites' class='sites' multiple>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
</select>
<table id='cutbacks' class='table'>
<tbody></tbody>
</table>
My problem is that my table is empty until both functions were finished.
What's wrong?