简体   繁体   English

如何在 tabulator.js 中向嵌套表中添加新行

[英]How to add new row to nested table in tabulator.js

I have created a table inside parent table like this: http://tabulator.info/examples/4.7#tree - Check Nested Tables not the Nested Data Trees.我在父表中创建了一个表,如下所示: http://tabulator.info/examples/4.7#tree - 检查嵌套表而不是嵌套数据树。

Now, I want to add a new row to the nested table.现在,我想在嵌套表中添加一个新行。 We can add to parent table using table.addRow() but how can I add to the child/nested table?我们可以使用table.addRow()添加到父表,但是如何添加到子/嵌套表?

I am using 4.7 version我使用的是 4.7 版本

You would need to maintain a link the table object created when you called the constructor function on the nested table.当您在嵌套表上调用构造函数 function 时,您需要维护表 object 的链接。 You could store it on a property of the parent row data to make it accessible elsewhere.您可以将其存储在父行数据的属性中,以使其可在其他地方访问。

An oversimplified example:一个过于简单的例子:

var table = new Tabulator("#example-table", {
    columns:[
        {title:"Make", field:"make"},
        {title:"Model", field:"model"},
        {title:"Registration", field:"reg"},
        {title:"Color", field:"color"},
    ],
    rowFormatter:function(row){
        //create and style holder elements
       var tableEl = document.createElement("div");

       row.getElement().appendChild(tableEl);

       var subTable = new Tabulator(tableEl, {
           layout:"fitColumns",
           data:row.getData().serviceHistory,
           columns:[
           {title:"Date", field:"date", sorter:"date"},
           {title:"Engineer", field:"engineer"},
           {title:"Action", field:"actions"},
           ]
       })
      
       row.update({subTable:subTable});

    },
});

In example the subtable would then be accessible on the data object for the row component.在示例中,子表将可以在行组件的数据 object 上访问。 so for example, if you had the component for the row containing the table you want to add a new row to, you could call:例如,如果您有包含要添加新行的表的行的组件,您可以调用:

row.getData().subTable.setData({name:"bob", age:52}), add row to nested table inside row.

Before you have to set the subtable with an id o something在您必须使用 id o 设置子表之前

tableEl.setAttribute('class', "sottotabella subTable" + id + ""); 
var subTable = new Tabulator(tableEl, {}....)

then you can get the subtable with然后你可以得到子表

var tableSub = Tabulator.prototype.findTable(".subTable" + idSub)[0];

and add the row并添加行

tableSub.addData([{}])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM