'Tabulator add a new row and edit content

I have following Tabulator setup:

var table = new Tabulator("#data-table", {
            layout: "fitColumns", //fit columns to width of table
            columns: [ //Define Table Columns
                { title: "Name", field: "name", editor:"input" },
                { title: "IP", field: "ip" },
            ],
            index:"name"
        });

Also, I register a function to insert a new row:

function insertRow(){
            table.addData([{
                name: "",
                ip: "",
            }], true).then(function(rows){
                if (rows.length == 0) return;
                rows[0].getCell("name").edit();
            })

        }

When I call the function insertRow(), it can insert a new row into the table but not open editing for the first cell.
However, following code works:

if (rows.length == 0) return;
                setTimeout(function(){
                    rows[0].getCell("name").edit();
                }, 100);

In the documentation http://tabulator.info/docs/4.9/update,

The addData method returns a promise, this can be used to run any other commands that have to be run after the data has been loaded into the table. By running them in the promise you ensure they are only run after the table has loaded the data.

So, it seem that we cannot call rows[0].getCell("name").edit() immediately inside the promise.
Is this correct?
Is there any problem with my code?



Solution 1:[1]

If you are only adding one row, use the addRow function instead of the addData function:

table.addRow({name: "", ip: ""], true)
.then(function(row){
   rows.getCell("name").edit();
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Oli Folkerd