I've got a table which is listing orders and I want to add the possiblity to filter results. Therefore I created a select field and an onchange effect in Jquery which is loading the same table with those filtered results into my Div. Therefore I used Ajax to update this "new Content" to my div. The problem is, that this table has several JQuery effects (for example a slide down when you click on a row) which doesn't work when the Content got created by the AjaxRequest. I have read some things already here in Stackoverflow and in other boards, but there was so many specific cases that I couldn't use any. I've heared something about a "init-function" which I need to call somewhere, so the JQuery things will also work on my Ajax generated content.
This is my Ajax Request:
$('.category').on('change', function (e) {
var optionSelected = $("option:selected", this);
var valueSelected = this.value;
$.ajax({
type: "POST",
url: "includes/listorders.php",
data: "filter=" + valueSelected,
success: function( data ) {
$( "#latestOrders" ).html(data);
}
});
});
This is what my listorders.php basically returns (probably not necessary to know to fix my issue):
$filter = $_POST['filter'];
//Prepare Queries
$lastOrders = "SELECT * FROM Orders ORDER BY dateAdded DESC LIMIT 0,48";
$ps_orders = $db->query($lastOrders);
$data = $ps_orders->fetchAll();
foreach($data as $row)
{
$orderid =$row['id'];
$username = $row['name'];
$done = $row['refsDone'];
$quantity = $row['quantity'];
$running =$row['refsRunning'];
$untouched = $quantity - ($done+$running);
?>
<tr class="header mySlideToggler" data-id="<? echo $orderid ?>">
<td class="align-center">TEST<? echo $username ?></td>
<td>0 %</td>
<td>22 Aug 2014</td>
<td><? echo $done . " / " . $quantity ?></td>
<td><? echo $running ?></td>
<td><? echo $untouched ?></td>
<td>
<a href="#" class="table-icon edit" title="Edit"></a>
<a href="#" class="table-icon expand mySlideToggler" data-id=$orderid title="Expand"></a>
<a href="#" class="table-icon delete" title="Delete"></a>
</td>
</tr><tr style="padding: 0"><td colspan="7" style="padding: 0">
<div class="accounts" style="display: none">
</div>
</td></tr>
<?
}
?>
My Slide effect including another Ajax request for the content which should be displayed into the slided div:
// Slide effect for listing account information to an order
$('.mySlideToggler').click(function( event ){
event.preventDefault();
event.stopPropagation();
var id = $(this).data('id');
var div = $(this).closest('tr').next().find('.accounts');
$.ajax({
type: "POST",
url: "includes/listaccounts.php",
data: "orderid=" + id,
success: function( data ) {
div.html(data);
}
});
$(this).closest('tr').next().find('.accounts').slideToggle();
});