问题
I want the logic after the top header checkbox is clicked to be different. Currently (I am using custom formatter) it selects all first level rows.
I want the click to select/deselect all rows that are NOT a parent. Or at least not to select the parent rows.
In current set up parent row tree toggle
on click and then is deselected.
custom formatter looks like that. To be honest I do not understand the code. I tried to put console.log to every addEventListener
in below definition but it was not triggered.
I guess the answer might be in registerHeaderSelectCheckbox
but I have no idea how to use it.
Working jsFiddle for trying out.
formatter: function(cell, formatterParams, onRendered) {
const data = cell.getRow().getData();
if (do_not_show_checkbox_ids.indexOf(data['id']) == -1) {
var checkbox = document.createElement("input");
checkbox.type = 'checkbox';
if (this.table.modExists("selectRow", true)) {
checkbox.addEventListener("click", (e) => {
e.stopPropagation();
});
if (typeof cell.getRow == 'function') {
var row = cell.getRow();
if (row._getSelf().type == "row") {
checkbox.addEventListener("change", (e) => {
row.toggleSelect();
});
checkbox.checked = row.isSelected && row.isSelected();
this.table.modules.selectRow.registerRowSelectCheckbox(row, checkbox);
} else {
checkbox = "";
}
} else {
checkbox.addEventListener("change", (e) => {
if (this.table.modules.selectRow.selectedRows.length) {
this.table.deselectRow();
} else {
this.table.selectRow(formatterParams.rowRange);
}
});
this.table.modules.selectRow.registerHeaderSelectCheckbox(checkbox);
}
}
return checkbox;
}
return null;
},
回答1:
titleFormatter
option is for header row checkbox and formatter
option is for rows checkbox
var do_not_show_checkbox_ids = [1];
function customFormatter(isHeader = false) {
return function(cell, formatterParams, onRendered) {
var checkbox = null;
// check if selectable is true in options
if (this.table.modExists("selectRow", true)) {
checkbox = document.createElement("input");
checkbox.type = 'checkbox';
// add click event in checkbox
checkbox.addEventListener("click", (e) => {
console.log('header', isHeader);
e.stopPropagation();
});
// check if cell has getRow function if yes then it may be row
if (typeof cell.getRow == 'function') {
// get cell row
var row = cell.getRow();
// get cell data for condition testing
const data = cell.getData();
// if is row and not skippable id
if (row._getSelf().type == "row" && do_not_show_checkbox_ids.indexOf(data['id']) == -1) {
// add change event to toggle cell row
checkbox.addEventListener("change", (e) => {
row.toggleSelect();
});
// update checkbox after row toggle
checkbox.checked = row.isSelected && row.isSelected();
// registering for if row clicked from anywhere then checkbox check/uncheck
this.table.modules.selectRow.registerRowSelectCheckbox(row, checkbox);
} else {
// if is row and skippable id
checkbox = "";
}
} else {
// header checkbox then add change addEventListener for selecting/deselecting all rows.
checkbox.addEventListener("change", (e) => {
// get all rows
this.table.getRows().forEach(row => {
// get row nodes/children
row.getTreeChildren().forEach(child => {
// check if child selected if yes then deselect else select
child.isSelected() ? child.deselect() : child.select();
})
});
// for selecting/deselecting all rows
// if (this.table.modules.selectRow.selectedRows.length) {
// console.log('header', isHeader);
// this.table.deselectRow();
// } else {
// this.table.selectRow(formatterParams.rowRange);
// }
});
// updating internal header checkbox
this.table.modules.selectRow.registerHeaderSelectCheckbox(checkbox);
}
return checkbox;
}
return null;
};
}
var tableDataNested = [{
id: 1,
name: "BalanceOil",
_children: [{
id: 11,
name: "BalanceOil+",
cena: 31,
mn: 1,
cena_1: 159
},
{
id: 12,
name: "BalanceOil Aqua",
cena: 41,
mn: 1,
cena_1: 159
},
]
},
{
id: 2,
name: "Xtend",
cena: 23,
mn: 1
},
{
id: 2,
name: "Xtend",
cena: 23,
mn: 1
},
{
id: 2,
name: "Xtend",
cena: 23,
mn: 1
},
{
id: 3,
name: "Zinobiotic",
cena: 24,
mn: 1
}
];
var table = new Tabulator("#example-table", {
movableColumns: true,
data: tableDataNested,
dataTree: true,
selectable: true,
columns: [{
title: "Name",
field: "name",
headerSort: false,
width: 200
},
{
title: "Cena",
field: "cena",
headerSort: false
},
{
titleFormatter: customFormatter(true),
formatter: customFormatter(),
hozAlign: "center",
headerSort: false,
cellClick: function(e, cell) {
console.log('header', e, cell)
this.recalc();
}
},
{
title: "mn",
field: "mn",
editor: "number",
headerSort: false,
cellEdited: function(cell) {
updateSum(cell);
}
},
{
title: "Sum",
field: "sum",
headerSort: false
}
],
rowClick: function(e, row) {
// console.log(table.getRows().length);
},
rowSelectionChanged: function(data, rows) {
// console.log(data, rows);
// console.log(this.getData());
},
renderComplete: function(t) {
this.getRows().forEach(function(value, index) {
// console.log(value.isSelected());
var children = value.getTreeChildren();
for (let j = 0; j < children.length; j++) {
const name = children[j].getData().name;
}
children.forEach(function(value, index) {
// console.log("cena");
var cena = value.getData().cena; //price
// console.log(cena);
var mnozstvi = value.getData().mn; //amount
value.update({
sum: cena * mnozstvi
});
});
updateSum(value.getCell("mn"));
});
},
rowClick: function(e, row) {
if (row.getTreeChildren().length > 0) {
table.deselectRow(row.getData().id);
row.treeToggle();
}
},
});
function updateSum(cell) {
var cena = cell.getData().cena; //price
var mnozstvi = cell.getValue(); //amount
if (mnozstvi) {
cell.getRow().update({
sum: cena * mnozstvi
});
}
}
Here working example
tabulator
documentation links - rowSelection and dataTree
Note: For info about how tabulator
row selection works internally check here
来源:https://stackoverflow.com/questions/65818244/how-to-change-behaviour-of-header-checkbox-in-tabulator-js