Prevent automatic ajax call with deferloading but without serverside processing

Prevent automatic ajax call with deferloading but without serverside processing

dan@19ideas.comdan@19ideas.com Posts: 56Questions: 13Answers: 1

Just wondering is there a way to prevent an automatic ajax call on the initial load without using serverSide processing? I'm using asp core, and since the library isn't supported, I been replicating functionalities on the server. I read in the manual that deferLoading does what I need it to, but I need serverSide to be set to true. Is there a way around this? This is causing problems to fail on the server as certain criteria is not filled in yet.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974

    Maybe you can describe the problem you are trying to solve in more detail. You could remove the ajax option from your Datatables config and use it externally when ready then use rows.add() to add the data to the blank Datatable. Not sure if that helps without knowing more about your requirements.

    Kevin

  • dan@19ideas.comdan@19ideas.com Posts: 56Questions: 13Answers: 1

    I'm not using rows.add(), I'm using datatables the same way as I'm using something like this example:

    https://datatables.net/examples/ajax/objects.html

    The problem is, it has automatic ajax fetching when you render the table. The web service needs a parameter to be sent, which isn't ready yet until a user inputs that information.

    I'd like to defer the loading, hence, deferLoading, until I can call a redraw, or a ajax.reload() in order to send that new user input as the parameter.. initially this parameter is null -- which throws the error on the server.

    I am creating a new way of handling this, by sending a piece of criteria info to the server, where if the data is null, just don't get the list. It will still make the call to the server regardless though.

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi Dan,

    This is one of @kthorngren 's examples here from a different thread - it shows how you can call the Ajax load after the table initialisation. In that example it's on a button press, but it could be anything. Is this something that would work?

    Cheers,

    Colin

  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin
    Answer ✓

    It sounds like that isn't the full solution. And I'm afraid Dan, there isn't a complete solution for this one. The answer is that the delayLoading option was specifically implemented only for server-side processing. It was designed for cases when the table would be rendered in the default state by the server, and then new page requests in DataTable trigger a new data fetch.

    The closest you would get in client-side processing at the moment is to load the initial 10 (or whatever) rows from the DOM and then use an Ajax request to get the extra data.

    delayLoading isn't actually used much since it requires the data for the table to be laid out correctly (sorting, filtering, paging). Having that with client-side processing would increase the complexity of your code.

    I take the point that this would be a useful addition in some cases! But at the moment, I'm afraid its just not something that DataTables core supports. Something that would be good as a future enhancement though!

    Allan

  • dan@19ideas.comdan@19ideas.com Posts: 56Questions: 13Answers: 1

    I understand Allan, thanks. The only work around I made was to send information to the server to dictate that nothing should be returned.

    Colin's answer might be key though, where I can trigger some kind of event to fire off that ajax code.

  • dtdevdtdev Posts: 9Questions: 4Answers: 0
    edited March 2019

    I found a way to do this:

    Declare a variable to determine initial state:

    let initialLoad = true;
    

    Then, make use of ajax as a function:

    $("table").DataTable({
        ajax: function (data, callback, settings) {
            if (initialLoad) {
                initialLoad = false;
                callback({data: []}); // don't fire ajax, just return empty set
                return;
            }
    
            // Ajax will now only fire programmatically, via ajax.reload()
            $.getJSON("/url/to/data", function (data) {
                callback(data);
            });
        },
    });
    
  • protasovamsprotasovams Posts: 11Questions: 2Answers: 0

    Hello, Allan!
    Are there any plans to include this functionality in the future? It's a shame to have to use workarounds for this.

  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974

    One option is to not define the ajax option then use ajax.url().load() to load the data when ready, for example:
    http://live.datatables.net/wiqufunu/1/edit

    This option is a bit limited as I don't think there is a way to provide other Ajax parameters.

    Kevin

  • jhourladjhourlad Posts: 1Questions: 0Answers: 0

    Note: Not specifying ajax will not render your custom toolbar. Just saying.

  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974
    edited October 2020

    @jhourlad Do you mean like this?
    http://live.datatables.net/wiqufunu/12/edit

    From this example.

    Please provide an example oof the issue you are having.

    Kevin

This discussion has been closed.