通过 AJAX 请求将制表符数据导出为 json 文件

Export tabulator data as json file via AJAX request

我正在尝试作为 AJAX 请求从 table 一个一个地发送数据,但是如果我删除 table 中间的一行并按下按钮 BTNwriteToFlash,我得到大量的错误。我不知道如何在发送前检查 table 中是否存在行。

有人可以告诉我如何正确地做吗? 或者更好的方法是如何将整个 table 数据作为 json 文件发送? 我是初学者,我无法帮助自己。我正在使用最新的 Tabulator v4.7.2。 http://tabulator.info/ 这是我的代码的一部分,用于逐行发送:

$("#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);
  }
}

谢谢!

我引用了 http://tabulator.info/ 库文档。 如果在调用 getRow() 函数时 table 中没有行,则结果为 False。所以如果你调用了getRow()函数,结果为False,你可以跳到下一行继续执行函数。

修改UpdateFlash函数

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

感谢您的快速回复。 我也用类似的方式完成了这项工作:

$('#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 );                                            

})

函数 UpdateFlash() {
如果(flashUpdateStarted == false)return;
如果( userToFlashCounter > 0 ){
var rowdata = userTable.getRow( usrcnt );
如果(行数据==假){
usrcnt++;
return; }
var upData = "{"id":" + newUserId + ","name":"" + rowdata.getData().name + "","phone":"" + rowdata.getData().phone + "}"; SendAjaxRequest( 'NewUser', upData, false );
userToFlashCounter--;
usrcnt++; 新用户Id++; return;
}

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

}

这也“修复”了 table

中所有项目的 ID

非常感谢!