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

如何在jQuery DataTable中合并两个字段为单个列显示?

jQuery DataTable合并locale后缀字段与对应字段到单列的实现方法

直接给你改好的代码,同时说明修改逻辑:

修改要点

  • 删掉原来单独的full_name_locale列
  • 在full_name列的配置里添加render函数,通过整行数据对象获取对应的locale字段值,合并后显示

完整修改后的代码

$(document).ready(function (e) {
    $('#data-grid-branch_infos').DataTable({
        bProcessing: true,
        bServerSide: true,
        sAjaxSource: 'GetBranchPaginatedList',
        "fnServerParams": function (aoData) {
            //aoData.push({ "name": "dstatus", "value": $('#ShowClosed').val() });
        },
        "aoColumns": [
            { "mData": 'row_no', name: "row_no", title: 'SNo.', width: 20, filterable: false },
            { "mData": 'is_corporate', name: "is_corporate", title: "Is Corporate", width: 150 },
            { "mData": 'parent_id', name: "parent_id", title: "Parent Id", width: 150 },
            { "mData": 'br_alias', name: "br_alias", title: "Branch Alias", width: 150 },
            { 
                "mData": 'full_name', 
                name: "full_name", 
                title: "Full Name", 
                width: 150,
                // 新增render函数合并字段
                "render": function(data, type, row) {
                    // 自定义合并后的显示格式,这里用换行分隔,可按需调整
                    let displayText = data || '';
                    if (row.full_name_locale) {
                        displayText += `<br/>${row.full_name_locale}`;
                    }
                    return displayText;
                }
            }
        ]
    });
});

代码说明

  • render函数的第三个参数row是当前行的完整数据对象,能直接通过row.full_name_locale获取对应的本地化字段值
  • 显示格式可随意调整:比如改成${data} / ${row.full_name_locale}用斜杠分隔,或者用括号包裹,只要修改拼接逻辑即可
  • 原代码最后一列存在语法错误(缺少闭合的}和数组逗号),已经一并修正

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 01:52:34