1

我从 DataTable https://www.datatables.net/开始。我使用持久的 DataTable。我可以通过 ajax 从 JSON 字符串加载数据到 DataTable。现在我想在连续单击时获取数据。正如您在http://debug.datatables.net/idihol看到的 DataTable 调试器, 这是我的页面测试.aspx

<table id="div_table" class="display cell-border compact" width="100%">
            <thead>
                <tr>
                    <td>No</td>
                    <td>Name</td>
                    <td>Des</td>
                    <td>LID</td>
                    <td>AID</td>
                    <td>DATE</td>
                    <td>BY</td>
                </tr>
            </thead>
        </table>

这是我的剧本

var table = $('#div_table').DataTable({
                "processing": false,
                "serverSide": false,
                "ajax": {
                    "url": "../BUS/WebService.asmx/LIST_LOCATION",
                    dataSrc: function (json) {
                        return $.parseJSON(json.d);
                    },
                    "dataType": "json",
                    "contentType": "application/json; charset=utf-8",
                    "type": "POST"
                },
                "aoColumns": [  //// 7 columns as Datatable
                    { "mData": null, "aTargets": [0], "sType": "integer", "bSearchable": false, "orderable": false },
                    { "mData": "LOCATION_NAME", "aTargets": [1], "sType": "string" },
                    { "mData": "LOCATION_DES", "aTargets": [2], "sType": "string" },
                    { "mData": "LOCATION_ID", "aTargets": [3], "sType": "string", "bVisible": false, "bSearchable": false, "orderable": false },
                    { "mData": "AREA_ID", "aTargets": [4], "sType": "string", "bVisible": false, "bSearchable": false, "orderable": false },
                    { "mData": "EDIT_DATE", "aTargets": [5], "sType": "date", "bVisible": false, "bSearchable": false, "orderable": false },
                    { "mData": "EDIT_BY", "aTargets": [6], "sType": "string", "bVisible": false, "bSearchable": false, "orderable": false }
                ],
                "order": [[1, 'asc']]
            }); 
            //table.columns([3, 4, 5, 6]).visible(false);           //// disable column 4,5,6,7
            //// create index column 1
            table.on('order.dt search.dt', function () {
                table.column(0, { search: 'applied', order: 'applied' }).nodes().each(function (cell, i) {
                    cell.innerHTML = i + 1;
                });
            }).draw();
 $('#div_table tbody').on('click', 'tr', function () {    // get full data or some columns at row selected
                $(this).toggleClass('selected');
                var data_ = table.row($(this)).data();
                alert(data_[3] + " and " + data_[4]);
                /// alert(table.row($(this)).data()); error it show info "object object"
            });

运行它后,我收到错误“未定义和未定义”你能告诉我问题并给我建议吗。谢谢。

4

1 回答 1

4

问题是您的 JSON 数据是具有类似属性的对象数组LOCATION_NAMELOCATION_DES但您尝试使用索引 ( data_[3], data_[3]) 检索数据,就好像您的 JSON 数据是数组数组一样。

row().data()手册页:

函数返回:行的数据源的数据源对象。如果您使用 DOM 源数据,这将是一个数组,否则它将是用于用数据填充表的数组/对象/实例。

您尝试检索的数据将在data_['LOCATION_ID']和中可用data_['AREA_ID']

于 2015-05-29T12:03:48.393 回答