How can I dynamically add rows and columns to a YUI dataTable

回眸只為那壹抹淺笑 提交于 2020-01-06 08:46:06

问题


I am trying to modify the the YUI sortable dataTable example to add rows and columns dynamically after the dataTable is created. My code looks like this:

YUI().use("datatable-sort", function(Y) {
    var cols = [
        {key:"Company", label:"Click to Sort Column A", sortable:true},
        {key:"Phone", label:"Not Sortable Column B"},
        {key:"Contact", label:"Click to Sort Column C", sortable:true}
    ],
    data = [
        {Company:"Company Bee", Phone:"415-555-1234", Contact:"Sally Spencer"},
        {Company:"Acme Company", Phone:"650-555-4444", Contact:"John Jones"},
        {Company:"Industrial Industries", Phone:"408-555-5678", Contact:"Robin Smith"}
    ],
    table = new Y.DataTable({
        columns: cols,
        data   : data,
        summary: "Contacts list",
        caption: "Table with simple column sorting"
    }).render("#sort");

    // Add rows and columns here.
});

回答1:


You can dynamically add rows into a YUI dataTable by calling the addRow() or addRows() method. If you are using the YUI sortable dataTable example, you could do it this way:

table.addRows(
    [{Company:"New Company", Phone:"555-555-5555", Contact:"John Smith"},
    {Company:"Old Company", Phone:"555-555-6666", Contact:"Rowdy Piper"}]);

Dynamically loading columns can be done similarly with addColumn():

 table.addColumn({key:"NewColumn", label:"Column D"});

Note: you must include the datatable-mutable module in your code to utilize these methods.



来源:https://stackoverflow.com/questions/25278006/how-can-i-dynamically-add-rows-and-columns-to-a-yui-datatable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!