javascript - 数据表从 json 对象获取 'undefined'

标签 javascript json datatables

我需要从按钮函数中的 json 对象获取一个 id。如果我尝试直接访问 id,我会得到 undefined (不是错误/警告),但如果我尝试访问该对象,我可以毫无问题地看到它(id 和其余数据)。

var table = $('#example').DataTable( {
    serverSide: true,
    dom: 'Bfrtip',
    ajax: '/get?op=2',
    columns: [
        { data: 'id' },
        // more columns
    ],
    buttons: [
        {
            text: 'New',
            action: function ( e, dt, node, config ) {
                window.location.href = '/url?op=new'
            }
        },
        {
            text: 'Modify',
            action: function ( e, dt, node, config ) {
                window.location.href = '/url?op=modify&id=' + dt.row( { selected: true } ).id() )
            },
            enabled: false
        },
        {
            text: 'Delete',
            action: function ( e, dt, node, config ) {
            },
            enabled: false
        }
    ],
    select: true
} );

我可以访问 json 对象来执行以下操作:

alert( JSON.stringify(dt.row( { selected: true } ).data()) );
// {"id":1,"key":"value","etc":"etc"}

工作正常,我可以在警报中看到该对象。

alert( dt.row( { selected: true } ).id() );  // undefined
alert( JSON.stringify(dt.row( { selected: true } ).id()) );  // "undefined"
alert( JSON.stringify(dt.row( { selected: true } ).data()[0]) );  // undefined

这不起作用,我可以看到 undefined 而不是警报中的整数。

我尝试了很多我什至不记得的事情,但没有一个起作用......

我做错了什么?

最佳答案

我认为这就是您正在尝试做的事情。看看我的修改按钮,我在你的按钮之后添加的。我正在使用 extn,但如果您的数据有 id 字段,您应该能够更改名称以匹配。做了一些其他更改,以便它可以在我的本地环境中运行,但如果您在 jsfiddle 中设置或在本地进行设置,它应该可以工作。

注意:这假设一次只能选择一行。

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <link href="https://cdn.datatables.net/1.10.10/css/jquery.dataTables.min.css " rel="stylesheet" type="text/css" />
        <link href="https://cdn.datatables.net/buttons/1.1.0/css/buttons.dataTables.min.css" rel="stylesheet" type="text/css" />

          <link href="https://cdn.datatables.net/select/1.1.0/css/select.dataTables.min.css" rel="stylesheet" type="text/css" />
        <script src="//code.jquery.com/jquery-1.11.3.min.js" type="text/javascript"></script>
        <script src="https://cdn.datatables.net/1.10.10/js/jquery.dataTables.min.js" type="text/javascript"></script>
        <script src="https://cdn.datatables.net/buttons/1.1.0/js/dataTables.buttons.min.js" type="text/javascript"></script>
        <script src="https://cdn.datatables.net/select/1.1.0/js/dataTables.select.min.js" type="text/javascript"></script>
        <script type="text/javascript">

            var mydata =  [
                  {
                      "name": "Tiger Nixon",
                      "position": "System Architect",
                      "salary": "$320,800",
                      "start_date": "2011/04/25",
                      "office": "Edinburgh",
                      "extn": "5421"
                  },
                   {
                       "name": "Garrett Winters",
                       "position": "Accountant",
                       "salary": "$170,750",
                       "start_date": "2011/07/25",
                       "office": "Tokyo",
                       "extn": "8422"

                   }];



            $(document).ready(function() {

                $.map(mydata, function (item, key) {
                           item["DT_RowId"] = item["extn"]});

               var table = $('#example').DataTable( {
                   serverSide: false,
                   dom: 'Bfrtip',
                   data:mydata,
                   columns: [

               { "data": "name" },
               { "data": "position" },
               { "data": "office" },
               { "data": "extn" },
               { "data": "start_date" },
               { "data": "salary" }
                       // more columns
                   ],

                   buttons: [
                       {
                           text: 'New',
                           action: function ( e, dt, node, config ) {
                               window.location.href = '/url?op=new'
                           }
                       },
                       {
                           text: 'Modify',
                           action: function (e, dt, node, config) {

                               window.location.href = '/url?op=modify&id=' + dt.row( { selected: true } ).id() ;
                           },
                           enabled: true
                       },
                       {
                           text: 'Delete',
                           action: function ( e, dt, node, config ) {
                           },
                           enabled: false
                       },
                       {
                            extend: 'selected',
                           text: 'Modify',
                           action: function ( e, dt, button, config ) {
                            var rw = dt.rows({ selected: true }).data()[0];
                            window.location.href = '/url?op=modify&id=' + rw.extn;
                        }

                }

                   ],
                   select: true
               } );
            } );

        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>

            <table width="100%" class="display" id="example" cellspacing="0">
                    <thead>
                        <tr>
                            <th>Name</th>
                            <th>Position</th>
                            <th>Office</th>
                            <th>Extn.</th>
                            <th>Start date</th>
                            <th>Salary</th>
                        </tr>
                    </thead>
                    <tfoot>
                        <tr>
                            <th>Name</th>
                            <th>Position</th>
                            <th>Office</th>
                            <th>Extn.</th>
                            <th>Start date</th>
                            <th>Salary</th>
                        </tr>

                    </tfoot>
                </table>
        </div>
        </form>
    </body>
    </html>

关于javascript - 数据表从 json 对象获取 'undefined',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34623883/

相关文章:

php - 使用 zend_json : not found 时出现 fatal error

javascript - 为 Google MAPS API v3 放置来自 JSON 数据的标记

javascript - 将对象转换为数组 - 维护命名键

c# - C#、Asp.Net Web 应用程序中出现奇怪的 System.StackOverflowException 错误

jquery - 搜索不适用于动态创建的行

jQuery - 每个函数

javascript - JavaScript 中 HTML 文本的淡入淡出

javascript+jquery Monkey修补/替换/覆盖类方法

javascript - JSON 解析在 jQuery 中不起作用

javascript - 数据表中的列排序问题