[英]How to change specific jQuery dataTables cell value
I have 2 HTML
tables. 我有2个HTML
表。 Table 1
, and Summary Table 2
. Table 1
和Summary Table 2
。
I use the following function to add or remove a checkmark in a cell in Table 1
, and to then increment or decrement the relevant value in Summary Table 2
. 我使用以下函数在Table 1
的单元格中添加或删除复选标记,然后在Summary Table 2
递增或递减相关值。
function toggleCheckMark(elem, brand){
var cell = $(elem).html();
if(cell == ""){
$(elem).html("✔");
var brandval = parseInt($("#" + brand).find("td:eq(3)").html());
var newval = brandval + 1;
$("#" + brand).find("td:eq(3)").html("" + newval + "");
} else {
$(elem).html("");
var brandval = parseInt($("#" + brand).find("td:eq(3)").html());
var newval = brandval - 1;
$("#" + brand).find("td:eq(3)").html("" + newval + "");
}
}
This works absolutely fine when Summary Table 2
is a bog standard html table. 当Summary Table 2
是沼泽标准html表时,这绝对可以正常工作。 However when I used DataTables()
on Summary Table 2
. 但是,当我在Summary Table 2
上使用DataTables()
时。 The value is undefined, and therefor cannot be changed. 该值是不确定的,因此不能更改。
How can I change my function so that it will work when my table is formatted by DataTables()
? 如何更改我的函数,使其在使用DataTables()
格式化表时可以正常工作?
EDIT 编辑
My HTML already includes empty <table id="rangereviewsummarytable">
tags and I use the following function to populate the summary table. 我的HTML已经包含空的<table id="rangereviewsummarytable">
标记,并且我使用以下函数填充摘要表。
Here's a clickable cell: 这是一个可点击的单元格:
rrtable += "<td class='checkboxcell' onClick=\"toggleCheckMark(this, '" + item.brand + "')\">✔</td>";
And the function 和功能
function generateReviewSummary(summaryarray){
var summarytable = "";
$.each(summaryarray, function(brand, values){
summarytable += "<tr id='" + brand +"''>"; //here is the logic behind $("#" + brand)...
summarytable += "<td>" + brand + "</td>";
summarytable += "<td>" + values.current + "</td>";
summarytable += "<td>" + values.rec + "</td>";
summarytable += "<td>" + values.user + "</td>"; // this is the col I want to update
summarytable += "</tr>";
});
summarytable = "<tbody>" + summarytable + "</tbody>";
$("#rangereviewsummarytable").append(summarytable);
//$("#rangereviewsummarytable").DataTable();
$("#rangereviewsummarypanel").show();
}
Always use the API. 始终使用API。 You seem to want to update a specific column in a specific row - use the .cell().data()
API method instead of jQuery .html()
您似乎想更新特定行中的特定列-使用.cell().data()
API方法而不是jQuery .html()
var brandval = parseInt(summaryTable.cell({ row: 0, column: 2 }).data())
brandVal = !isNaN(brandVal) ? brandVal+1 : 1 //or -1
summaryTable.cell({ row: 0, column: 2 }).data(brandVal).draw()
I am referring to a summaryTable
, the dataTable instance you must have since you have instantiated "Summary Table 2" somewhere : 我指的是summaryTable
,因为在某处实例化了“ Summary Table 2”,所以必须具有dataTable实例:
var summaryTable = $("#" + brand).DataTable()
What the logic is behind $("#" + brand)
I can only guess. 我只能猜测$("#" + brand)
背后的逻辑是什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.