sparkmix
sparkmix

Reputation: 2489

Jquery DataTable - function to process before ajax and disable ajax for a condition

I'm using Jquery DataTable 1.10.15 and here's my data table. I want to check when a user click on each page that page's record reached an amount and if it does I want to pop up saying narrow down your search instead of displaying the page they want which means no ajax call/table load. The below code worked on the alert but when I return false I thought it will not do the ajax call but it still does. Anyone know how to do it?

var myTable = $('#myTable')
.on('preXhr.dt', function ( e, settings, data ) {
    if (data.start + data.length >=10000) {
        alert('here');
        return false;
    }
})
.DataTable({
    searching: true,
    processing: false,
    serverSide: true,     
    columns: columnsList,
    pageLength: 25,
    ajax: {

Upvotes: 2

Views: 6415

Answers (1)

kblau
kblau

Reputation: 2107

This is how you do this:

$(document).ready(function () {
        //added this variable
        var stopAjaxing = false;
        $('#example').on('preXhr.dt', function (e, settings, data) {
            //changing to 100 since it works for rows, not pagges
            if (data.start + data.length >= 100) {
                //return false;
                //moved message to callback
                stopAjaxing = true;
            }
        })
        .dataTable({
            "preDrawCallback": function (settings) {
                if (stopAjaxing) {
                    alert("You need to refine  your search");
                    return false;
                }
                else {
                    return true;
                }
            },  
...

Upvotes: 1

Related Questions