I have created a dynamic html table populated with json data as per the value of the variable on "go" button click. The search is working fine for the first time populated data but after changing the variable at run time when i re-populate the table on "go" button click with different values and as soon as i try to search a particular value from the table it try searching from the previous populated data ie, the data which was populated for the first time not from the current populated data and it modifies the table also with that data. No matter how many times you modify the data of the table on the basic of the value of the variable on "go" button click, although the table gets modified but as soon as you apply to search a value from the table it will search from the 1st populated value of the table and modify the table with that value.
$(document).ready(function() {
$("#report").hide();
$("#orderGraph").hide();
$("#profitGraph").hide();
$("#go").click(function() {
$("#orderGraph").show();
$("#profitGraph").show();
loadGraphOrderFilter();
loadGraphProfitFilter();
$("#report").show();
$("#datatable > tbody > tr").remove();
var e;
multicompanyUseCaseData="";
orderConditionData="";
orderTypeData="";
if(document.getElementById("mcuc_selection"))
{
e=document.getElementById("mcuc_selection");
multicompanyUseCaseData = e.options[e.selectedIndex].text;
}
if(document.getElementById("oc_selection"))
{
e = document.getElementById("oc_selection");
orderConditionData = e.options[e.selectedIndex].text;
}
if(document.getElementById("ot_selection"))
{
e = document.getElementById("ot_selection");
orderTypeData = e.options[e.selectedIndex].text;
}
var responseObj = $.parseJSON(response1);
$.each(responseObj, function (i, item) {
if( item.useCase == multicompanyUseCaseData || item.orderCondition == orderConditionData || item.orderType == orderTypeData )
{
$('<tr>').append(
$('<td>').text(item.orderId),
$('<td>').text(item.purchaseId),
$('<td>').text(item.shipmentId),
$('<td>').text(item.useCase),
$('<td>').text(item.orderCondition),
$('<td>').text(item.orderType)).appendTo('#datatable');
}
});
// this line performs the searching operation from the table
$('#datatable').dataTable();
});
});
var response1 = '[{"orderId":"19", "purchaseId":"Alon", "shipmentId":"5", "useCase":"AGS", "orderCondition":"2", "orderType":"Customer" }';
response1 += ',{"orderId":"19", "purchaseId":"Alon", "shipmentId":"5", "useCase":"AGL", "orderCondition":"2", "orderType":"Customer" }';
response1 += ',{"orderId":"19", "purchaseId":"Alon", "shipmentId":"5", "useCase":"NAFN", "orderCondition":"2", "orderType":"Customer" }]';
<div class="x_content">
<table id="datatable" class="table table-striped table-bordered">
<thead>
<tr>
<th>Order Id</th>
<th>Shipment Id</th>
<th>Purchase Id</th>
<th>Use Case</th>
<th>Order Condition</th>
<th>Order Type</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
.