简体   繁体   English

为什么我的jquery数据表createdrow函数不起作用?

[英]Why is my jquery datatables createdrow function not working?

I am trying to use the Datatables Rows Created Callback to modify the data in the row before drawing it. 我正在尝试使用“数据表创建的行回调”在绘制之前修改行中的数据。 What I am trying to do is replace all &lt; 我想做的是替换所有&lt; and &gt; &gt; with '<' and '>' so I can put a line break in each cell and have text on separate lines. 加上'<''>'这样我就可以在每个单元格中放置一个换行符,并在单独的行中添加文本。 '\\n' or linefeed does not work. '\\n'linefeed不起作用。

var oTable = $('#table').DataTable( { 
      "createdRow" : function( row, data, index) {
         console.log( 'DATA WAS ' + data[0]);
         data[0] = data[0].replace(/&lt;/g,'<').replace(/&gt;/g,'>');
         console.log( 'DATA IS ' + data[0]);
       }

in the console I can see the data being modified correctly. 在控制台中,我可以看到数据已正确修改。 But its not actually modifying the table. 但是它实际上并没有修改表。 Is there a way to do this? 有没有办法做到这一点? or is the createdRow callback called after the row has already been drawn? 还是在绘制完行后调用createdRow回调?

Yep, correct. 是的,正确。 createdRow callback is only called after the row has already been drawn. 仅在已绘制行之后才调用createdRow回调。 Instead of fixing your current code, I will show you how to do it using the proper (?) way by using column defs :D Also, I'd think/hope there was something somewhere that would convert your &lt; 除了固定当前代码外,我还将向您展示如何通过使用列defs:D使用正确的(?)方法来实现它:此外,我认为/希望某些地方可以转换您的&lt; stuff automatically. 自动填充。

var oTable = $('#table').DataTable( { 
    "columnDefs": [ {
        "targets": 0,
        "render": function(data, type, row, meta) {
            html = data.replace(/&lt;/g,'<').replace(/&gt;/g,'>');
            return html;
        },
    ],
}

Add the modified element back to the td (column) in question. 将修改后的元素重新添加到相关的td(列)中。

var oTable = $('#table').DataTable( { 
    "createdRow" : function( row, data, index) {
    console.log( 'DATA WAS ' + data[0]);
    data[0] = data[0].replace(/&lt;/g,'<').replace(/&gt;/g,'>');
    console.log( 'DATA IS ' + data[0]);
    $('td', row).eq(0).append(data[0]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM