motoma
motoma

Reputation: 31

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!

Upvotes: 0

Views: 395

Answers (2)

motoma
motoma

Reputation: 31

Thank you for fast reply. I have also done this job by similar way:

$('#BTNwriteToFlash').click(function() {
flashUpdateStarted = true;                                                                  
userToFlashCounter = userTable.getDataCount(false);                                         
console.log( "user count = " + userToFlashCounter );
totalUsers = userToFlashCounter;
usrcnt = 1;
newUserId = 1;
SendAjaxRequest( 'command' ,'RebuiltDatabase',  false );                                                                        
FlashUpdateLoop = setInterval( UpdateFlash, 0 );                                            

})

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

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

}

this also "repair" id for all items in table

Thank you very much!

Upvotes: 0

Anteater99
Anteater99

Reputation: 86

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);
    }
}

Upvotes: 1

Related Questions