首页 > 解决方案 > Manipulate column in datatable row

问题描述

I m using datatable to load data from the database. there is a datetime column that I want to Manipulate. So basically I want to use moment-js and convert datetime into the user's locale time and etc. I tried createdRow however that event is called after data has been inserted into the rows. is there beforeCreatedRow row type event so that I can Manipulate data before inserting it into a row?

            $("#ronin").DataTable({
            "order": [
                [0, "desc"]
            ],
            ajax: {
                url: "r1.php",
                dataSrc: ''
            },
            colReorder: {
                realtime: true
            },
            createdRow: function(row, data, dataIndex) {
                console.log(data.datetime);
                return data.datetime = '2222-22-22 11:11:11'//test
            },
            "aoColumns": [{
                    data: 'id'
                },
                {
                    data: 'seed'
                },
                {
                    data: 'useragent'
                },
                {
                    data: 'ip'
                },
                {
                    data: 'datetime'
                }
            ]
        });

here is the code.

thank you.

标签: datatablesdatatables-1.10

解决方案


要在数据显示之前对其进行操作,我首选的方法是使用列。渲染。从文档中不是很明显,但您也可以传递一个函数来渲染:

...

  "columnDefs": [ {
    "targets": 4,
    "data": "datetime",
    "render": function ( data, type, row, meta ) {
      return data = '2222-22-22 11:11:11'//test;
    }
...

推荐阅读