jqueryajaxwordpressdatatablesserver-side-scripting

Fail to call ajax on second time button click using DataTable server-side-scripting


I'm using a datatable with server-side-scripting to show records on button click. The first time the button is clicked I am getting the response properly but the second time the button is clicked ajax is calling.

I have also used a draw function for it.

My ajax call js file:

$(document).on('click' , '.search-btn', function(){

var shape = "";
jQuery(".diamond_shape.diamond_selected").each(function () {
    shape += jQuery(this).attr("title") + ",";
});

var clarity = '';
jQuery("#select-clarity").each(function () {
    clarity += jQuery(this).attr("value") + ",";
});

var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
var dataTable = $('#example').DataTable( {
        processing: true,
        serverSide: true,
        retrieve: true,
        searching: false,   
        paging: false,
        dataType: "json",
        contentType: "application/json",
        "ajax":{
            "url" : ajaxurl, // json datasource 
            "type": "POST",
            "data": {action: 'getFilterDiamonds',dataShape: shape , dataClarity : clarity },
        },
        "columns": [
            {"data": "reportNo"},
            {"data": "reference"},
            {"data": "shape"},  
            {"data": "lab"},
            {"data": "weight"},
            {"data": "color"},
            {"data": "clarity"}
        ]
    });
    });

How I add the selected filter value in the input value attribute:


Solution

  • var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
    
    $(document).on('click' , '.search-btn', function(){
    
        var shape = "";
        jQuery(".diamond_shape.diamond_selected").each(function () {
            shape += jQuery(this).attr("title") + ",";
        });
    
        var clarity = '';
        jQuery("#select-clarity").each(function () {
            clarity += jQuery(this).attr("value") + ",";
        });
    
        /*- code to destory datatable -*/
        if ($.fn.dataTable.isDataTable('#example')) {
            $('#example').DataTable().destroy();
        }   
        /*---*/
    
        var dataTable = $('#example').DataTable( {
            "scrollX": true,
            processing: true,
            serverSide: true,
            retrieve: true,
            searching: false,
            destroy: true,  
            paging: false,
            dataType: "json",
            contentType: "application/json",
            "ajax":{
                "url" : ajaxurl, // json datasource 
                "type": "POST",
                "data": {action: 'getFilterDiamonds',dataShape: shape , dataClarity : clarity },
            },
            "columns": [
                {"data": "reportNo"},
                {"data": "reference"},
                {"data": "shape"},  
                {"data": "lab"},
                {"data": "weight"},
                {"data": "color"},
                {"data": "clarity"}
            ]
        });
    
    
    });
    

    using following code datatable will erase old data and load new data with filter. Must have to set destroy: true in datatable function.

    if ($.fn.dataTable.isDataTable('#example')) {
            $('#example').DataTable().destroy();
        }
    

    Thank you all for your quick response.