如何通过 jQuery 数据表中的值查找特定行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38392464/
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
How to find a specific row by values in jQuery datatables?
提问by nhaberl
I would like to find a specific row by value within a datatables table out of a modal window. I was looking on https://datatables.net/reference/type/row-selectorbut as I understand it's all based on selectors or internal IDs. In my case I have 2 columns where I want to be able to lookup for the specific row to update the record after ajax request.
我想在模式窗口外的数据表中按值查找特定行。我正在查看https://datatables.net/reference/type/row-selector但据我所知,这一切都基于选择器或内部 ID。在我的情况下,我有 2 列,我希望能够在 ajax 请求后查找特定行以更新记录。
success: function (data) {
if (data.status_id > 0) {
alert(data.info);
} else {
alert(data.info);
}
contractsTable.row.add(dataJSON).draw(false);
}
EDIT
编辑
Here my code now - I've built my own unique rowidand used selector by id
现在这里是我的代码 - 我已经构建了自己唯一的rowid并按id使用了选择器
Retrieving the data object
检索数据对象
...
var d = datatable.row(this).data();
... set form values and so on
Save and Refresh datatable
保存和刷新数据表
$('#contractEditSave').on('click', function (e) {
dataJSON = {
id: $('#contractEditForm').data('contractid'),
member_id: $('#contractEditForm').data('memberid'),
member_name: $('#contractEditModalTitle').text(),
box_id: $('#contractBox').val(),
name: $('#contractName').val(),
description: $('#contractDescription').val(),
start: $('#contractStart').val(),
end: $('#contractEnd').val(),
amount: $('#contractAmount').val(),
unit: $('#contractUnit').val(),
max: 1
};
$.ajax({
type: 'POST',
url: '/save',
data: JSON.stringify(dataJSON),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (data) {
if (data.status_id == 0)
datatable.row('#' + dataJSON.id).data(dataJSON); //if update
...
} else {
datatable.row.add(dataJSON).draw(false); //if insert
...
}
$("#contractEditModal").modal('hide');
}
});
});
采纳答案by Gyrocode.com
You can use fnFindCellRowIndexes
to find row index holding certain data in given column.
您可以使用fnFindCellRowIndexes
在给定列中查找包含某些数据的行索引。
Then you can use cell().data()
API method to update the cell.
然后您可以使用cell().data()
API 方法更新单元格。
var table = $('#example').DataTable();
var rowId = $('#example').dataTable()
.fnFindCellRowIndexes('Angelica Ramos', 0);
table
.cell(rowId, 0)
.data('Angelica Ramos (UPDATED)')
.draw(false);
Please note that you need to include fnFindCellRowIndexes.js
in addition to jQuery DataTables CSS/JS files.
请注意,fnFindCellRowIndexes.js
除了 jQuery DataTables 之外,您还需要包含CSS/JS 文件。
See this jsFiddlefor code and demonstration.
有关代码和演示,请参阅此 jsFiddle。
回答by joshstrike
I found the fnFindCellRowIndexes plugin to be unbelievably slow, because it calls fnGetData on every cell, which itself is a terrible CPU hog. The answer turns out to be a lot simpler, and orders of magnitude faster to run. The data() of every column, or the whole table, is already ordered by the sorted column when you request it.So literally, all you have to do is request column(c).data() and iterate through that array looking for whatever you're looking for, and the indexes will match the row number as currently sorted. In Typescript this is basically like:
我发现 fnFindCellRowIndexes 插件慢得令人难以置信,因为它在每个单元格上调用 fnGetData,这本身就是一个可怕的 CPU 猪。结果证明答案要简单得多,而且运行速度要快几个数量级。当您请求时,每一列或整个表的 data()已经按已排序的列排序。所以从字面上看,您所要做的就是请求 column(c).data() 并遍历该数组以查找您要查找的任何内容,并且索引将与当前排序的行号匹配。在打字稿中,这基本上是这样的:
public findRows(s:string,col:number):number[] {
let r:number[] = [];
let dat:any = $('#example').DataTable().column(col).data();
let len:number = dat.length;
for (let k=0;k < len;k++) {
if ((<string>dat[k]).toLowerCase().includes(s.toLowerCase())) r.push(k);
}
return r;
}