javascript - 使用纯 json 输入时通过数据表获取 rowid 值

标签 javascript jquery json datatables

在我的项目中,我使用数据表直接显示数据库中的数据。我的输入数据是纯 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 不包含数据表站点提到的数据对象。如何解决这个问题?

最佳答案

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'] );
} );

关于javascript - 使用纯 json 输入时通过数据表获取 rowid 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48247581/

相关文章:

javascript - 捕获调用的函数及其作为参数传入的参数

javascript - $(文档).ready() 来源

php - 在第二页转发表单中显示输入名称

javascript - 在 jquery 中创建雪花

javascript - 比较 JSON 和 BSON

php - 将 JSON 中的值返回到 PHP 形式的 Javascript

php - 复选框状态应保留状态以在页面加载时隐藏/取消隐藏文本

JavaScript 正则表达式 : how to not capture an optional string on the right side

javascript - 如何在 Highchart 中自定义工具提示?

json - jq:根据同一数组中的其他项目从数组中删除与条件匹配的项目