dataTable的columnDefs参数使用

1,datatable的同一column显示后台返回数据的多个字段

利用columnDefs的render,其中function(data,type,row){},data读取该column对应字段的数据,row读取一整条记录,可以获取每个字段的值,因此可将两个字段衔接一起。

"columns": [
                {"data": "name", "bSortable": false, "title": "项目名称"},
                {"data": "project_total_invest", "bSortable": false, "title": "总投资(亿)"},
                {"data": "", "bSortable": false, "title": "建设工期"}
            ],
"columnDefs": [
                {
                    "render": function (data, type, row) {
                        return data.toFixed(2);//data是该字段的数据,这里即"project_total_invest"字段的值
                    },
                    "targets": 1
                },
                {
                    "render": function (data, type, row) {
                        var start="";
                        var end="";
                        if(row.plan_start_date){//row是一整行的数据,可以读取该行各字段
                            start=row.plan_start_date;
                        }if(row.plan_end_date){
                            end=row.plan_end_date;
                        }
                        return (start.substring(0,10)).replace(/-/g,".")+"-"+(end.substring(0,10)).replace(/-/g,".");
                    },
                    "targets": 2
                }              
            ]

 

2,更多用法参考:https://blog.csdn.net/zxf1242652895/article/details/78802495

 

——————————补充——————————

想改变表头thead的内容,可以直接在columns参数里编辑,例如:

要对 DataTable 进行分页并合并多列,可以使用 jQuery DataTables 插件。该插件可以实现 DataTable 的分页、排序、搜索等功能,还可以自定义 DataTable 的显示样式。 下面是一个示例代码,演示如何使用 DataTables 插件对 DataTable 进行分页并合并多列: ```html <!-- 引入 DataTables 插件的 CSS 和 JS 文件 --> <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.25/css/jquery.dataTables.min.css"> <script type="text/javascript" src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.min.js"></script> <!-- 在 HTML 中定义一个表格 --> <table id="myTable" class="display" style="width:100%"> <thead> <tr> <th>Name</th> <th>Age</th> <th>Email</th> </tr> </thead> <tbody> <tr> <td>Tom</td> <td>20</td> <td>tom@example.com</td> </tr> <tr> <td>Jerry</td> <td>18</td> <td>jerry@example.com</td> </tr> <!-- 其他行省略 --> </tbody> </table> <script> $(document).ready(function() { // 初始化 DataTables 插件 $('#myTable').DataTable({ "pagingType": "full_numbers", // 分页样式 "pageLength": 10, // 每页显示的行数 "searching": false, // 禁用搜索功能 "ordering": false, // 禁用排序功能 "info": false, // 隐藏统计信息 "columnDefs": [{ // 合并多列 "targets": [0, 1], // 要合并的列的索引 "render": function(data, type, row, meta) { return data + ' (' + row[1] + ')'; } }] }); }); </script> ``` 在上面的代码中,我们通过引入 DataTables 插件的 CSS 和 JS 文件,并在 HTML 中定义一个表格。然后在 JavaScript 中初始化 DataTables 插件,并设置一些参数,比如分页样式、每页显示的行数、禁用搜索和排序功能等。最后使用 columnDefs 参数合并多列,将 Name 和 Age 两列合并为一列。 运行代码,就可以看到 DataTable 被分页并且 Name 和 Age 两列被合并为一列了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值