datatable 給某一列添加title屬性


簡單描述:采用datatable拼接的表格,沒有title屬性,嗯就是這個情況,直接上代碼

代碼:

//js代碼
$("#toAdd").click("click",function () {
var selectNodes = $('#table2').DataTable().rows(".active").nodes();
if(selectNodes.length==0){
layer.msg("請選擇操作項!!");
return false;
}
// 1.遍歷dataTable,獲取每頁中checkbox的選中狀態
// 2.tb1添加相應行
var nodes = $('#table2').DataTable().rows().nodes();
for (var i = 0; i < nodes.length; i++) {
var active = $(nodes[i]).hasClass('active');
if (active) {
var nowRow = $(nodes[i]);
var trId = nowRow.attr("id");
var tr = $('#table2').DataTable().row(i).data();//獲取table2的數據
var tb1 = $('#table1').DataTable();
var tr8 = "<div style=\"display: flex; align-items: center;\">\n" +
" <div>\n" +
" <img src="+rootPath+"assets-new/apps/img/shanchu.png\n" +
" onclick=\"deleteProd('"+ trId +"')\"\n" +
" title=\"刪除\"/>\n" +
" </div>\n" +
" </div>";
var trNode = tb1.row.add({
"DT_RowId": trId,
"0": tr[1],
"1": tr[2],
"2": tr[3],
"3": tr[4],
"4": tr[5],
"5": tr[6],
"6": tr[7],
"7": tr8
}).draw().node();
var trIndex = tb1.row(trNode).index();
tb1.cell(trIndex, 0).node().title = tr[1];
}

}
for (var i = 0; i < nodes.length; i++) {
var active = $(nodes[i]).hasClass('active');
if (active) {
var nowRow = $(nodes[i]);
var trId = nowRow.attr("id");
$('#table2').DataTable().row('#' + trId).remove().draw();
}
}
var rowCount = $("#table1").DataTable().page.info().recordsTotal;
if (rowCount > 0) {
document.getElementById("tableDivOne").style.display="block";//顯
}
}); 

 說明:最關鍵的就是綠色部分了,  .node() 是獲取節點, 然后獲取索引      注意cell(trIndex, 0)  其中0是你的表中的每行的第幾格(從0開始的)      tr[1]是直接獲取到內容


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM