Remove select checkboxes when actions unavailable

This commit is contained in:
Shweelan Samson authored and Zaid Abdulla committed 2019-07-09 02:19:54 -07:00
1 parent 795a0f8273
commit b51c97e97a
1 file changed
+42 -35
+42 -35
View File
@@ -40,21 +40,34 @@ function renderTable(
recordActionsCallback?: Children | ((record: Record<string, any>) => Children) recordActionsCallback?: Children | ((record: Record<string, any>) => Children)
): Children { ): Children {
const records = data || []; const records = data || [];
const selectAll = m("input", {
type: "checkbox", // Actions bar
checked: records.length && selected.size === records.length, let buttons: Children = [];
onchange: e => { if (typeof actionsCallback === "function") {
for (const record of records) { buttons = actionsCallback(selected);
const id = record["_id"] || record["DeviceID.ID"].value[0]; if (!Array.isArray(buttons)) buttons = [buttons];
if (e.target.checked) selected.add(id); } else if (Array.isArray(actionsCallback)) {
else selected.delete(id); buttons = actionsCallback;
} }
},
disabled: !total
});
// Table header // Table header
const labels = [m("th", selectAll)]; const labels = [];
if (buttons.length) {
const selectAll = m("input", {
type: "checkbox",
checked: records.length && selected.size === records.length,
onchange: e => {
for (const record of records) {
const id = record["_id"] || record["DeviceID.ID"].value[0];
if (e.target.checked) selected.add(id);
else selected.delete(id);
}
},
disabled: !total
});
labels.push(m("th", selectAll));
}
for (let i = 0; i < attributes.length; i++) { for (let i = 0; i < attributes.length; i++) {
const attr = attributes[i]; const attr = attributes[i];
const label = attr.label; const label = attr.label;
@@ -87,20 +100,23 @@ function renderTable(
const rows = []; const rows = [];
for (const record of records) { for (const record of records) {
const id = record["_id"] || record["DeviceID.ID"].value[0]; const id = record["_id"] || record["DeviceID.ID"].value[0];
const checkbox = m("input", { const tds = [];
type: "checkbox", if (buttons.length) {
checked: selected.has(id), const checkbox = m("input", {
onchange: e => { type: "checkbox",
if (e.target.checked) selected.add(id); checked: selected.has(id),
else selected.delete(id); onchange: e => {
}, if (e.target.checked) selected.add(id);
onclick: e => { else selected.delete(id);
e.stopPropagation(); },
e.redraw = false; onclick: e => {
} e.stopPropagation();
}); e.redraw = false;
}
});
tds.push(m("td", checkbox));
}
const tds = [m("td", checkbox)];
for (const attr of attributes) { for (const attr of attributes) {
const attrs = {}; const attrs = {};
let valueComponent; let valueComponent;
@@ -180,15 +196,6 @@ function renderTable(
m("tr", m("td", { colspan: labels.length }, footerElements)) m("tr", m("td", { colspan: labels.length }, footerElements))
); );
// Actions bar
let buttons: Children = [];
if (typeof actionsCallback === "function") {
buttons = actionsCallback(selected);
if (!Array.isArray(buttons)) buttons = [buttons];
} else if (Array.isArray(actionsCallback)) {
buttons = actionsCallback;
}
const children = [ const children = [
m( m(
"table.table.highlight", "table.table.highlight",