Quantcast
Channel: Recent Discussions — DataTables forums
Viewing all articles
Browse latest Browse all 82460

Update Table after server side filter

$
0
0

I made a nice search filter that sorts my table out. The issue is that it calls a function when I select an option and if I try to update, delete or add an entry it does not update it in the table because it gets stuck in the function. Here is my current code:

        var dataTable = $('#user_data').DataTable({
            "processing":true,
            "serverSide":true,
            "order":[],
            "ajax":{
                url:"fetch.php",
                type:"POST"
            },
            "columnDefs":[
                {
                    "targets":[2, 3],
                    "orderable":false,
                },
            ],

        });

    function load_data(options){
        var dataTable = $('#user_data').DataTable({
            "processing":true,
            "serverSide":true,
            "order":[],
            "ajax":{
                url:"fetch.php",
                type:"POST",
                data: { options:options }
            },
            "columnDefs":[
                {
                    "targets":[2, 3],
                    "orderable":false,
                },
            ],

        });
    }

    $('#options').on('change', function(){
        var options = $(this).val();
        /* alert(options); */
        $('#user_data').DataTable().destroy();
        if(options != '') {
            load_data(options);
        }else{
            load_data();
        }
    });

And this is what happens in the php side:

$query = '';
$output = array();
$query .= "SELECT * FROM users ";
if(isset($_POST["options"]))
{
 $query .= "WHERE lastName = '".$_POST["options"]."' AND ";
}
else{ $query .= "WHERE "; }
if(isset($_POST["search"]["value"]))
{
    $query .= '(firstName LIKE "%'.$_POST["search"]["value"].'%" ';
    $query .= 'OR lastName LIKE "%'.$_POST["search"]["value"].'%") ';
}
if(isset($_POST["order"]))
{
    $query .= 'ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order']['0']['dir'].' ';
}
else
{
    $query .= 'ORDER BY id DESC ';
}
if($_POST["length"] != -1)
{
    $query .= 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
}
$statement = $connection->prepare($query);
$statement->execute();
$result = $statement->fetchAll();

When I first load the page everything updates correctly on edit, add or remove. Only after I select an option from the drop-down select list and I destroy the initial table I get stuck in limbo! How can I correct this issue!?


Viewing all articles
Browse latest Browse all 82460

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>