You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为DataTables的td元素设置Ajax返回的note字段动态title属性

解决DataTables中td动态title的问题

嘿,这事儿其实很容易搞定!你只需要利用createdCell回调里的rowData参数就行——它包含了当前行的所有数据,自然也包括你定义的note字段。

原来的代码里你给title设了静态值'columns',现在把它换成rowData.note就能拿到对应行的note内容了,修改后的columnDefs部分应该是这样:

"columnDefs": [{
    'targets': 4,
    'createdCell': function (td, cellData, rowData, row, col) {
        // 直接用rowData.note作为title的动态值
        $(td).attr('title', rowData.note);
        // 要是担心note为空的话,可以加个默认值,比如:
        // $(td).attr('title', rowData.note || '无备注信息');
    }
}]

这样一来,第4列(对应你的note列)的每个td元素的title就会自动显示该行的note字段内容啦。要是你的note字段可能为空,上面注释里的默认值写法能让界面更友好哦。

完整的初始化代码修改后大概是这样:

{
    "ajax": { 
        "url": "{{ route('cust_continfo_data_table') }}", 
        "dataType": "json", 
        "type": "POST", 
        "data": { 
            _token: "{{ csrf_token() }}", 
            cust_id_copy: $("#cust_id_copy").val() 
        } 
    },
    "columnDefs": [{
        'targets': 4,
        'createdCell': function (td, cellData, rowData, row, col) {
            $(td).attr('title', rowData.note);
        }
    }],
    "columns": [
        { "data": "id" }, 
        { "data": "receipt_date" }, 
        { "data": "info_division_name" }, 
        { "data": "contact_status" }, 
        { "data": "note" }, 
        { "data": "created_at" }, 
        { "data": "updated_at" } 
    ]
}

内容的提问来源于stack exchange,提问作者andil01

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:04:11