简体   繁体   English

DataTables将不是列的对象数据添加到行?

[英]DataTables add object data that is not a column to row?

I can't figure this one out, their API is very confusing. 我无法弄清楚这一点,他们的API非常令人困惑。

I have a table that displays tabular data of geometric features, so I want to have the coordinates saved in the row data. 我有一个表,用于显示几何特征的表格数据,因此我想将坐标保存在行数据中。

And I need a way to retrieve this coordinates, for example, when a user clicks on the row. 我需要一种方法来检索此坐标,例如,当用户单击该行时。

Example of what I'm trying to achieve : 我正在尝试实现的示例:

const dt = $('table').DataTable({
    columns : [
        {data : "NAME"},
        {data : "COLOR"}
    ]
})

dt.row.add({
    COLOR : "Red",
    NAME : "Point1",
    //Invalid parameter error
    geometry : {
        type : "point",
        coordinates : [1,1]
    }
})

I think it wouldn't be ideal storing in a hidden column, because the coordinates can get quite large for polygon types. 我认为将其存储在隐藏列中并不理想,因为对于多边形类型,坐标可能会变得很大。

I'm very new to DataTables and am very confused with the API, any suggestion on how to better organize/execute my concept is more than welcome. 我是DataTables的新手,并且对API感到非常困惑,关于如何更好地组织/执行我的概念的任何建议都值得欢迎。

Seems like what you have should work. 似乎您所拥有的应该可以工作。 I put together a little example for you: http://live.datatables.net/rikofefu/1/edit 我为您整理了一个小例子: http : //live.datatables.net/rikofefu/1/edit

It adds a row with the extra geometry object. 它添加了带有额外geometry对象的行。 Select the row then click the Show selected data button. 选择该行,然后单击“ Show selected data按钮。 The console will display the full row and just the geometry object. 控制台将显示整行,仅显示几何对象。

I'm using the Select extension to get the selected row. 我正在使用Select扩展名来获取选定的行。

HTML 的HTML

<table>
  <thead>
    <tr>NAME</tr>
    <tr>COLOR</tr>
    <tr></tr>
  </thead>
</table>

JS JS

        const dt = $('table').DataTable({
        columns: [
            {data: null},
            {data: null},
            {data: null}
        ],
         // This makes the row clickable, where you can get data later
        "createdRow": function (row, data, dataIndex) {
            var tds = $(row).find("td");
            $.each(tds, function (key, value) {
                $(value).attr("onclick", "see_the_data(this)");
            }
        },
        // this hides the coordinate column from view
        "columnDefs": [
                {
                    "targets": 2,
                    "visible": false
                }
         ]
    });

    dt.row.add({
        COLOR: "Red",
        NAME: "Point1",
        //Invalid parameter error
        geometry: {
            type: "point",
            coordinates: [1, 1]
        }
    });

    function see_the_data(e) {
        console.log("the data is ", e);  // you can then go further as you need
    }

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

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