How do I put an AJAX result in the 'render' section of a column?

How do I put an AJAX result in the 'render' section of a column?

DaevinDaevin Posts: 5Questions: 3Answers: 0
edited May 2016 in Free community support

I have a table that gets data added to it based on user selection, and in the render section of a column I want to make an AJAX request to check the status of the row added to the table. Right now I have:

columns: [
    ...
    {
        data: objectID,
        render: function (data, type, full, meta) {
            return $.ajax({
                url: '/api/CheckStatus?objectID=' + data
            }).done(function (data) {
                return data.toString()
            });
        }
    },
    ...
]

which obviously just displays "[object Object]" since it's just returning the AJAX object. Is there a way to have the 'render' portion of a column do this?

Answers

  • DaevinDaevin Posts: 5Questions: 3Answers: 0
    edited May 2016

    I'm a little silly. Instead of bothering with promises and other async stuff, I just did this:

    columns: [
        ...
        {
            data: objectID,
            render: function (data, type, full, meta) {
                var currentCell = $("#table-id").DataTable().cells({"row":meta.row, "column":meta.col}).nodes(0);
                $.ajax({
                    url: '/api/CheckStatus?objectID=' + data
                }).done(function (data) {
                    $(currentCell).text(data);
                });
                return null;
            }
        },
        ...
    ]
    

    In words: I grabbed the desired cell using the cells() API and 'meta' data in the 'render' function, then in the AJAX done() function set the text of that cell to the result of the AJAX call.

    It's the most simple way I can think of doing it. If anyone has a better idea, feel free to contribute.

This discussion has been closed.