【问题标题】:Get rowid value with datatables when using pure json input使用纯 json 输入时使用数据表获取 rowid 值
【发布时间】:2018-01-14 07:43:13
【问题描述】:

在我的项目中,我使用数据表直接从数据库中显示数据。我的输入数据是纯 JSON,我从未使用过数据表网站中提到的任何后端处理方法

 $(document).ready(function() {
    var table = $('#example').DataTable( {

        "order": [[ -0 ]],
        "pageLength": 24,
        "oLanguage": { "sSearch": '<a class="btn searchBtn" id="searchBtn"><i class="fa fa-search"></i></a>' },
        "lengthChange": false,

        "ajax": {
            "url": "data.php?content=userdata",
            "dataSrc": ""
        },

        "columns": [            
            { "data": "id",
               "visible": false,
            },
            { "data": "oid" },
            { "data": "name" },
            { "data": "mobile" },
            { "data": "email" },
            { "data": null,
                "targets": -0,
                "data": null,
                "defaultContent": "<button>test</button>",
            }
        ],

    } );

    $('#example tbody').on( 'click', 'button', function () {
        var data = table.row( $(this).parents('tr') ).data();
        alert("oid is: "+ data[ 1] );
    } );
} );

我的 JSON 输入是这样的

[
    {
    id: "3",
    oid: "213",
    name: "Koh Tien Kit Thomas ",
    mobile: 0123456789,
    email: "some@mail.com",
    }
]

但是当我单击数据表行按钮时,它说值未定义。我的 json 不包括数据对象作为数据表站点提及。如何解决这个问题?

【问题讨论】:

    标签: javascript jquery json datatables


    【解决方案1】:

    API 方法row().data() 以原始格式返回数据。由于您使用了对象,因此 OID 将在 data['oid'] 中可用。

    改用下面的代码:

    $('#example tbody').on( 'click', 'button', function () {
        var data = table.row( $(this).parents('tr') ).data();
        alert("oid is: "+ data['oid'] );
    } );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-10-24
      • 2016-03-10
      • 2019-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多