jquery DataTables - 更改单元格的值而不仅仅是显示值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3299280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:11:30  来源:igfitidea点击:

jquery DataTables - change value of a cell not just display value

jquerydatatables

提问by Shibbott

Using DataTablesI want to change the value of the data before rendering the table. I used this:

使用DataTables我想在呈现表格之前更改数据的值。我用过这个:

"fnRowCallback": function( nRow, aData, iDisplayIndex ) {
    if ( aData[2] == "0" ){
        $('td:eq(1)', nRow).html( '<b>6</b>' );
    }
}

But I found that although I changed the displayed text to be 0 to 6, when I sort by the column it's still sorting by the data and not the displayed text.

但是我发现虽然我将显示的文本从 0 改为 6,但是当我按列排序时,它仍然是按数据排序,而不是按显示的文本排序。

Does anyone know how I can actually change the data in the cell so that when I sort it will correctly sort by 0-6?

有谁知道我如何实际更改单元格中的数据,以便在排序时按 0-6 正确排序?

回答by Yisroel

You need to update the datatable, not html.

您需要更新数据表,而不是 html。

oTable.fnUpdate( newValue, rowPos, columnPos);

assuming oTableis a reference to the datatable.

假设oTable是对数据表的引用。

回答by Organiccat

You should probably paste some more of your code, especially the sorting area.

您可能应该粘贴更多代码,尤其是排序区域。

It seems you're mixing up val() and html():

看来您正在混淆 val() 和 html():

This will get you the input or cell value as in the value tag "value=?"

这将为您提供输入值或单元格值,如值标签“value=?”

$("#currentRow").val()

This will get you the actual html (data) between the tag "<td>data</td>"

这将为您提供标签“<td>data</td>”之间的实际 html(数据)

$("#currentRow").html()