2

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: {
3
  • All you need to do is modiy your .on('preXhr.dt'..) and add a callback Commented May 3, 2017 at 23:01
  • Your can do other things like data.draw(false), but this will not be cross browser compliant and is wrong. Commented May 4, 2017 at 16:05
  • Did this work for you? Commented May 9, 2017 at 3:23

1 Answer 1

1

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;
                }
            },  
...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.