【发布时间】:2019-06-29 17:58:37
【问题描述】:
至少,它之前是渲染的。我创建了一个DataTable,其中包含多行文档(由 JSON 数据填充)和一个对应于每个文档的复选框。单击复选框会将其添加到单独的 div(“收藏夹列表”)中,并且直到最近才有效。
我将其他一些列合并到表中,由于某种原因,它似乎与复选框收藏夹冲突。我相信我必须在代码中添加/更新一些东西才能与新列一起使用,但我不确定该怎么做。有什么想法吗?
加载表格数据:
loadTableData() {
$.noConflict();
let tableRes = JSONfile.d.results.filter(function(val) {
return (val.FileLeafRef.trim().length > 0);
}).map(function(obj) {
return {
"Path": obj.EncodedAbsUrl,
"Titles": obj.File.Name,
"Categories": obj.ResourceType.results.map(function(val) {
return val.Label;
}).join(";"),
"Blank": ''
}
});
渲染数据表:
$('#km-table-id').DataTable( {
columns: [
{ data: "Blank" },
{ data: "Titles" }, // populates col-2 column with docs
{ data: "Categories" }, // hidden col-3 categories
{ data: "Blank" }
],
columnDefs: [
{
data: "Path",
ordering: true, targets: [1],
render: function(data, type, row) {
return $('<a>')
.attr({target: "_blank", href: row.Path})
.text(data)
.wrap('<div></div>')
.parent()
.html();
},
targets: [1]
},
{ searchable: true, targets: [2], visible: false },
],
data: tableRes,
language: { searchPlaceholder: "Search All Documents" },
lengthMenu: [ 10, 25, 50, 100, 250, 500 ],
order: [],
pageLength: 500,
// paging: true,
pagingType: "full_numbers",
responsive: true,
scrollCollapse: true,
scrollXInner: true,
scrollY: 550,
sDom: '<"top">rt<"bottom"flp><"left">' // puts dropdown on bottom
});
收藏功能:
function faveFunc(evt) {
var anchor = $($(evt.target).prev().find("a")[0]).clone();
switch($(".populate-faves").find("a:contains(" + $(anchor).text() + ")").length)
{
case 0:
$(".populate-faves").append(anchor);
break;
default:
$(".populate-faves > a:contains(" + $(anchor).text() + ")").remove();
break;
}
console.log(faveFunc);
}; // ------------ faveFunc
function newList() {
let data = $(evt.target).prev().find("a").eq(0).html();
let outputList = $(".populate-faves");
$(".populate-faves").html("");
$("#km-table-id tbody tr").each(function(i, el) {
let fave = $(".checkbox-class", el);
let itemText = $(data, el);
if(fave.prop("checked")) {
outputList.append("<li>" + itemText.html() + "<li>");
}
});
};
$(".checkbox-class").on("click", faveFunc);
$("#add-id").on("click", function(){
console.log($(this));
});
HTML sn-p:
<table id="km-table-id" class="cell-border display stripe-hover">
<thead>
<tr>
<th></th>
<th>Title</th>
<th>Categories</th>
<th>My Favorites</th>
</tr>
</thead>
<!-- <tbody>
</tbody> -->
</table>
【问题讨论】:
标签: javascript jquery datatables onclick append