edit() API not working in for loop

edit() API not working in for loop

binexlinebinexline Posts: 27Questions: 10Answers: 0

How come edit() API doesn't work in a for loop..? It only works on the first loop but doesn't get processed afterwards.

            navigator.clipboard.readText().then(text => {
                var cell = tableEditable.cell({ focused: true });
                if (!cell.index()) return;

                var rowIndex = cell.index().row;
                var columnIndex = cell.index().column;
                var arrayRows = text.split("\n");
                arrayRows.forEach(element => {
                    var array = element.split("\t");
                    var data = new Object();
                    var cIndex = columnIndex;
                    array.forEach(value => {
                        var column = $(tableEditable.column(cIndex++).header()).text();
                        data[column] = value;
                    });
                    var row = tableEditable.row(rowIndex++);
                    editor.edit(row, false).set(data).submit();
                });
            });

For example, if text = 'CI-SEA\t07-31 22:00\nKE-SEA\t08-08 22:00;', below are console.log(data) values:
data in first loop => {REF: "CI-SEA", PU: "07-31 22:00"}
data in second loop => {REF: "KE-SEA", PU: "08-08 22:00"}

The first data is saved at the right positions, but the second data is gone...nowhere to be found...even ajax responds with the first data only..please help..

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,213Questions: 1Answers: 10,415 Site admin
    Answer ✓

    How come edit() API doesn't work in a for loop..?

    Because the Ajax operation it triggers is asynchronous. You need to wait for the submitComplete event to happen before you can trigger the next submit.

    A better solution would be to use multi-row editing so you can submit all changes in a single Ajax request.

    Allan

  • binexlinebinexline Posts: 27Questions: 10Answers: 0

    Thank you as always! :)

This discussion has been closed.