0

I have a Tabulator datatable in my HTML file. Looks like this:

<div class="example-table">/div>

I have a JavaScript file that would populate my table with data by calling a rest API that returns with a JSON.

This is how my JS file looks like:

$(document).ready(function() {

    $(".example-table").tabulator({
        columns : [ {
            title : "ID",
            field : "myjson.firstname",
            width : 250
        }, {
            title : "Pred",
            field : "myjson.pred",
            sorter : "number",
            align : "left",
            formatter : "progress",
            width : 200
        }, ],
    });

    var tabledata = [];

    $.getJSON('http://127.0.0.1:7001/MySerices/service/rest', function(json) {
        tabledata.append(json);
    });

    $(".example-table").tabulator("setData", tabledata);

});

And the JSON which the REST service returns with looks like this:

{"myjson":
[{"firstname":"Piter","pred":"0,616540492"},
{"firstname":"Piter","pred":"0,616540492"}
]}

The Tabulator table apears but without any data. If I check my JS log, I can see the request is done wihout any error, and i can see the JSON in my response.

Can you help me how can I do it?

Thank you!

2

1 Answer 1

3

There are three major errors in your code.

First, your JSON response, the response should be as the tabulator js documentation shows:

//An array of objects not wrapped in an object
[
   {"firstname":"Piter","pred":"0,616540492"},
   {"firstname":"Parker","pred":"0,42325456"}
]

Second, the columns field should match each row:

$(".example-table").tabulator({
    columns : [ {
        title : "ID",
        field : "firstname",//changed here
        width : 250
    }, {
        title : "Pred",
        field : "pred",//and here
        sorter : "number",
        align : "left",
        formatter : "progress",
        width : 200
    }, ],
});

Third, getJSON is asynchronous, so you need to get and set the data only when the response arrives:

$.getJSON('http://127.0.0.1:7001/MySerices/service/rest', function(response) {
    //response is already a parsed JSON
    $(".example-table").tabulator("setData", response);

});

PS: arrays don't have the append method, you can use unshift or pushto prepend or append data to the array.

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you so much! I have o modified my server side REST response too, but your soluion solved my probleme! Thank you!
@solarenqu no problem
only one more question, what do you think is it possible to load the table line by line? :)
@solarenqu like one by one with a delay?
@Lucas, you can pass the URL directly to the setData function and it will make the ajax request for you
|

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.