Ajax in "render" function

Ajax in "render" function

bernardini_davidebernardini_davide Posts: 1Questions: 1Answers: 0
edited June 2016 in Free community support

In the "render" function of a cell I have several ajax calls that load an internal status from the db and depending from that status it adds an image or/and a div. If I set the async to true in the ajax call the images are not shown(also if the success function runs), with async:false it works.
Since the async:false "is deprecated because of its detrimental effects to the end user's experience", as Chrome's console tell me each time, there's a way to make it work async?
I have deferRender set to true.
Here's some code:

var table = $('#interventi').DataTable({
                "order": [0, "desc"],
                "processing": true,
                "serverSide": true,
                "responsive": true,
                "deferRender": true,
                "dom": 'fB<"#titolo">rt<"bottom"lp><"clear">',
                "columnDefs": [ {
                    "render": function (data, type, row) {
                        var det = "<div class='div_com'>" + row[1];

                        $.ajax({
                            method: "GET",
                            async: false,
                            url: "count-prog.php",
                            data: {
                                "id": row[0]
                            }
                        }).success(function (response) {
                            if (response == -3)
                                det += "...some stuff...";
                           .....
                        det += "</div>";
                        return det;
                    },
                    "targets": 1
                }],
......

Answers

  • allanallan Posts: 61,723Questions: 1Answers: 10,108 Site admin

    Personally I would suggest that Ajax in the rendering method is about the worst thing you could possibly do for performance (magnified by using async: false). It means that every data fetch request DataTables makes for the rendered data (and it can do it 4 or more times per cell) its going to make an Ajax request. That's going to lead to it taking many seconds just to draw the table.

    Avoid that if you possibly can. Include that information the original data feed from the server.

    Allan

This discussion has been closed.