Search code examples
javascripttabulator

Export tabulator data as json file via AJAX request


I am trying send data from table one by one as AJAX request, but if I remove one of row in the middle of table and press button BTNwriteToFlash, I got tons of errors. I don't know how to check if row exist in the table before send.

Can someone show the way how to do it right? Or better how to send whole table data as json file? I am beginner and I cant help myself. I am using latest Tabulator v4.7.2. http://tabulator.info/ Here is stripped part of my code for sending rows one by one:

$("#BTNwriteToFlash").click(function () {
  flashUpdateStarted = true;
  userToFlashCounter = userTable.getDataCount(false);
  SendAjaxRequest("command", "RebuiltDatabase", false);
  FlashUpdateLoop = setInterval(UpdateFlash, 0);
});

function UpdateFlash() {
  if (flashUpdateStarted == false) return;
  if (userToFlashCounter > 0) {
    var rowdata = userTable.getRow(userToFlashCounter);
    var upData = '{"id":' + rowdata.getData().id + ',"name":"' + rowdata.getData().name + "}";
    SendAjaxRequest("NewUser", upData, false);
    userToFlashCounter--;
    return;
  }

  if (userToFlashCounter == 0) {
    console.log("All users sent");
    flashUpdateStarted = false;
    clearInterval(FlashUpdateLoop);
  }
}

Thank you!


Solution

  • I referenced the http://tabulator.info/ library document. If there are no rows in the table when the getRow() function is called, the result is False. So if you call the getRow() function and the result is False, you can just go to the next row and continue executing the function.

    Modify the UpdateFlash function

    function UpdateFlash() {
        if (flashUpdateStarted == false) return;
        if (userToFlashCounter > 0) {
            var rowdata = userTable.getRow(userToFlashCounter);
    
            if (rowdata === false) {
                return --userToFlashCounter;
            }
    
            var upData = "{\"id\":" + rowdata.getData().id + ",\"name\":\"" + rowdata.getData().name + "}";
            SendAjaxRequest("NewUser", upData, false);
            return --userToFlashCounter;
        }
    
        if (userToFlashCounter == 0) {
            console.log("All users sent");
            flashUpdateStarted = false;
            clearInterval(FlashUpdateLoop);
        }
    }