Abort Ajax call if search is null

Abort Ajax call if search is null

kirakatoukirakatou Posts: 4Questions: 1Answers: 0
edited May 2020 in Free community support
`var table = $('#data-table').DataTable({
    processing: true,
    serverSide: true,
    pageLength: 2,
    ajax: function (data, callback, settings) {
        if (!data.search) {
            callback({data: []}); 
            return;
        }

        //how to call parent function
    },
    columns: [
    { data: 'id', name: 'id' },
    { data: 'firstname', name: 'firstname' },
    { data: 'lastname', name: 'lastname' },
    { data: 'phone_number', name: 'phone_number' }
    ]
  });`

here is my code, i am trying to use $.getJSON function but it didnt make it to normal datatable
i just want it to hit API when search is not null

Answers

  • allanallan Posts: 64,061Questions: 1Answers: 10,559 Site admin
        ajax: function (data, callback, settings) {
            if (!data.search) {
                callback({data: []});
                return;
            }
     
            $.getJSON('/...', data, function (json) {
                callback(json);
            });
        },
    

    Allan

  • allanallan Posts: 64,061Questions: 1Answers: 10,559 Site admin

    Thinking about it - I'd suggest just checking that search is not empty on the server-side. If it is, then just return data as an empty array.

    Allan

  • kirakatoukirakatou Posts: 4Questions: 1Answers: 0
    $.getJSON('/...', data, function (json) {
        callback(json);
    });
    

    if using this the search and sort parameter not posted
    i am using global endpoint so can't change it

This discussion has been closed.