How to disable data loading during init dataTable?

How to disable data loading during init dataTable?

nmiheynmihey Posts: 6Questions: 3Answers: 0

How do disable ajax the first time I load a page? The data will be loaded by clicking on the button.

Only serverSide = false

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,324Questions: 26Answers: 4,774

    Instead of using ajax in the Datatables init code you can use jQuery's ajax function outside of Datatables along with rows.add() to add the rows into the table. This example will hopefully get you started:
    http://live.datatables.net/heweruva/1/edit

    Kevin

  • nmiheynmihey Posts: 6Questions: 3Answers: 0
    edited March 2018

    Thank you.

    But, I use preXhr and xhr events. In preXhr event, I use settings.iDraw = 0 to display a message from loadingRecords. In your version it does not work. Is there another option to disable data loading when DataTable is initialized? Is it possible to add it in new versions?

  • kthorngrenkthorngren Posts: 20,324Questions: 26Answers: 4,774

    Maybe you can update my example with your code to show s what specifically you are doing. It will be easier to help and answer your questions.

    Kevin

  • nmiheynmihey Posts: 6Questions: 3Answers: 0
    edited March 2018

    I now have:

    $('#datatables').DataTable({
        ajax: {
            url: "/Home/GetItems",
            cache: false,
            type: "POST",
            dataSrc: "",
            data: function (d) {
    ...
            }
        },
        columns: [
    ...
        ],
        language: {
            emptyTable: "My empty message",
            loadingRecords: "My loading message"
        },
    ...
    });
    
    $('#datatables').on("preXhr.dt", function (e, settings, data) {
        $(this).DataTable().clear();
        settings.iDraw = 0;
        $(this).DataTable().draw();
    });
    

    When I do

    $('#datatables').DataTable().ajax.reload();
    

    displayed "My loading message"

    If I use jQuery's ajax this will not work.

  • kthorngrenkthorngren Posts: 20,324Questions: 26Answers: 4,774
    Answer ✓

    Thanks. Now I understand better what you are trying to do. The only way I can think to do this is to use the ajax function option. Within the function call determine if this is the initial table load and either return an empty response or the result of the ajax call. Fortunately I had an example that was close which I modified here:
    http://live.datatables.net/dugacuka/1/edit

    Hope this example helps you get started.

    Kevin

  • nmiheynmihey Posts: 6Questions: 3Answers: 0

    Thanks

This discussion has been closed.