如何在JavaScript中等待this.submit()函数完成

编辑

在Rory McCrossan指示之后,我更改了代码以获取此信息:

var backNext = [
    {
        text: "<",action: function (e) {

            this.submit( function () {

            },null,false );

            table.on( 'draw',function () {
                var indexes = table.rows( {search: 'applied'} ).indexes();
                var currentIndex = table.row( {selected: true} ).index();
                var currentPosition = indexes.indexOf( currentIndex );                         

                if ( currentPosition > 0 ) {
                    var prev;
                    for (let i = currentPosition; i > 0; i--){
                        if(table.row(i-1).data()["Date"] === ""){
                            prev = i-1;
                            break;
                        }
                    }
                    if(prev !== undefined){
                        table.row( currentIndex ).deselect();
                        table.row( indexes[ prev ] ).select();
                    }
                }

                // Trigger editing through the button
                table.button( 0 ).trigger();
            });        
        }
    },'Enregistrer',{
        text: ">",action: function (e) {

            this.submit( function() {

            },function () {
                var currentIndex = table.row( {selected: true} ).index();
                var currentPosition = indexes.indexOf( currentIndex );
                var indexes = table.rows( {search: 'applied'} ).indexes();

                if ( currentPosition < indexes.length-1 ) {
                    var next = 0;
                    for (let i = currentPosition; i < indexes.length-1; i++){
                        if(table.row(i+1).data()["Date"] === ""){
                            next = i+1;
                            break;
                        }
                    }
                    if (next !== 0){
                        table.row( currentIndex ).deselect();
                        table.row( indexes[ next ] ).select();
                    }
                }

                // Trigger editing through the button
                table.button( 0 ).trigger();
            });
        }
    }
];

它有效,但是行var currentPosition = indexes.indexOf( currentIndex );导致错误Uncaught TypeError: Cannot read property 'indexOf' of undefined。我认为这是因为代码是在此上下文之外执行的。因此,对解决此问题的任何帮助将不胜感激。


我当前正在使用数据表的previous / next editing选项。数据从服务器加载并由ajax发送。特殊之处在于,在编辑数据时,表的结构可能会更改(可能会添加其他行)。

示例:

| No. | Name  | Qty | Expiration Date | Balance |

  1     Books   10     ""               10
  2     Pens    5      ""               5

当我通过提供数据{'Name': 'Books','Qty': 6,'Expiration Date': 11/2022}编辑第一行时。行数更改:

| No. | Name  | Qty | Expiration Date | Balance |

  1     Books   4      ""                4
  2     Books   6      11/2022           0
  3     Pens    5      ""                5

这是我的代码的结构:

// Buttons array definition to create previous,save and next buttons in
// an Editor form
var backNext = [
    {
        text: "&lt;",action: function (e) {
            // On submit,find the currently selected row and select the previous one
            this.submit( function () {
            }).done( function (){
                var indexes = table.rows( {search: 'applied'} ).indexes();
                var currentIndex = table.row( {selected: true} ).index();
                var currentPosition = indexes.indexOf( currentIndex );

                if ( currentPosition > 0 ) {
                    var prev;
                    for (let i = currentPosition; i > 0; i--){
                        if(table.row(i-1).data()["Date"] === ""){
                            prev = i-1;
                            break;
                        }
                    }
                    if(prev !== undefined){
                        table.row( currentIndex ).deselect();
                        table.row( indexes[ prev ] ).select();
                    }
                }
                // Trigger editing through the button
                table.button( 0 ).trigger();
            });
        }
    },'Save',action: function (e) {
            this.submit( function() {

            }).done( function (){
                var currentIndex = table.row( {selected: true} ).index();
                var currentPosition = this.indexes.indexOf( currentIndex );
                var indexes = table.rows( {search: 'applied'} ).indexes();

                if ( currentPosition < indexes.length-1 ) {
                    var next = 0;
                    for (let i = currentPosition; i < indexes.length-1; i++){
                        if(table.row(i+1).data()["Date"] === ""){
                            next = i+1;
                            break;
                        }
                    }
                    if (next !== 0){
                        table.row( currentIndex ).deselect();
                        table.row( indexes[ next ] ).select();
                    }
                }
                // Trigger editing through the button
                table.button( 0 ).trigger();
            }); 
        }
    }
];

该代码应该做什么:

当我按下nextprevious按钮时,我必须转到下一个/上一行,,但仅当到期日期为空时

在上面的示例中,在编辑了第1行之后,next按钮必须通过跳过第二行将我带到第三行。

现在的问题是Ajax是异步的,这意味着通过在数据表站点上提供示例,索引的恢复和当前位置仅考虑表的先前结构。不考虑新行。这就是为什么我添加了done方法,以等待ajax请求完成和表被重绘,以检索索引和当前位置。

但是我的代码无法正常工作。我收到错误Uncaught TypeError: this.submit(...).done is not a function

如何解决这个问题?

huangyanglv 回答:如何在JavaScript中等待this.submit()函数完成

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3167033.html

大家都在问