@logimaxx/kgrid 0.3.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -1
- package/dist/kgrid.js +858 -65
- package/dist/kgrid.min.js +2 -2
- package/docs/ai-guide.md +14 -5
- package/docs/api.md +19 -3
- package/docs/configuration.md +27 -2
- package/docs/field-types.md +22 -0
- package/docs/table-shell.md +1 -0
- package/integrations/maxxops.example.js +0 -1
- package/package.json +1 -1
- package/src/cells.js +19 -1
- package/src/column-chooser.js +155 -0
- package/src/configure.js +66 -15
- package/src/constants.js +12 -1
- package/src/data-body.js +40 -30
- package/src/dom.js +40 -4
- package/src/events.js +8 -0
- package/src/filters.js +37 -2
- package/src/init.js +56 -4
- package/src/insert-row.js +14 -7
- package/src/preferences.js +404 -0
- package/styles/table.css +85 -3
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Column chooser: checkbox visibility + HTML5 drag reorder.
|
|
3
|
+
*/
|
|
4
|
+
(function (CT) {
|
|
5
|
+
function layoutFromPanel($panel) {
|
|
6
|
+
const columns = [];
|
|
7
|
+
$panel.find(".kgrid-column-chooser-list li[data-name]").each(function () {
|
|
8
|
+
const name = this.getAttribute("data-name");
|
|
9
|
+
const checked = $(this).find("input[type='checkbox']").prop("checked");
|
|
10
|
+
columns.push({ name: name, hidden: !checked });
|
|
11
|
+
});
|
|
12
|
+
return { v: CT.PREFERENCES_VERSION, columns: columns };
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function visibleCount(layout) {
|
|
16
|
+
return layout.columns.filter(function (c) {
|
|
17
|
+
return !c.hidden;
|
|
18
|
+
}).length;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
CT.renderColumnChooserPanel = function ($panel, options, api) {
|
|
22
|
+
$panel.empty();
|
|
23
|
+
const $list = $("<ul>").addClass("kgrid-column-chooser-list").appendTo($panel);
|
|
24
|
+
CT.chooserColumns(options.columns).forEach(function (col) {
|
|
25
|
+
const $li = $("<li>")
|
|
26
|
+
.attr("draggable", "true")
|
|
27
|
+
.attr("data-name", col.name)
|
|
28
|
+
.appendTo($list);
|
|
29
|
+
$("<span>")
|
|
30
|
+
.addClass("kgrid-column-chooser-handle")
|
|
31
|
+
.attr("aria-hidden", "true")
|
|
32
|
+
.text("⋮⋮")
|
|
33
|
+
.appendTo($li);
|
|
34
|
+
const $label = $("<label>").appendTo($li);
|
|
35
|
+
const $cb = $("<input>")
|
|
36
|
+
.attr("type", "checkbox")
|
|
37
|
+
.prop("checked", !col.userHidden)
|
|
38
|
+
.appendTo($label);
|
|
39
|
+
if (col.locked) {
|
|
40
|
+
$cb.prop("disabled", true).attr("title", "This column cannot be hidden");
|
|
41
|
+
}
|
|
42
|
+
$label.append(document.createTextNode(" " + (col.label || col.name)));
|
|
43
|
+
});
|
|
44
|
+
$("<button>")
|
|
45
|
+
.attr("type", "button")
|
|
46
|
+
.addClass("btn btn-sm btn-outline-secondary kgrid-column-chooser-reset")
|
|
47
|
+
.text(options.columnChooserResetLabel || "Reset columns")
|
|
48
|
+
.appendTo($panel);
|
|
49
|
+
|
|
50
|
+
$panel
|
|
51
|
+
.off("change.kgridChooser")
|
|
52
|
+
.on("change.kgridChooser", "input[type='checkbox']", function () {
|
|
53
|
+
const layout = layoutFromPanel($panel);
|
|
54
|
+
if (visibleCount(layout) < 1) {
|
|
55
|
+
this.checked = true;
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
api.setLayout(layout);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
$panel
|
|
62
|
+
.off("click.kgridChooserReset")
|
|
63
|
+
.on("click.kgridChooserReset", ".kgrid-column-chooser-reset", function () {
|
|
64
|
+
api.resetLayout();
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
let dragEl = null;
|
|
68
|
+
$list.off(".kgridChooserDrag");
|
|
69
|
+
$list.on("dragstart.kgridChooserDrag", "li", function (e) {
|
|
70
|
+
dragEl = this;
|
|
71
|
+
$(this).addClass("kgrid-column-chooser-dragging");
|
|
72
|
+
if (e.originalEvent && e.originalEvent.dataTransfer) {
|
|
73
|
+
e.originalEvent.dataTransfer.effectAllowed = "move";
|
|
74
|
+
e.originalEvent.dataTransfer.setData("text/plain", this.getAttribute("data-name"));
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
$list.on("dragend.kgridChooserDrag", "li", function () {
|
|
78
|
+
$(this).removeClass("kgrid-column-chooser-dragging");
|
|
79
|
+
dragEl = null;
|
|
80
|
+
});
|
|
81
|
+
$list.on("dragover.kgridChooserDrag", "li", function (e) {
|
|
82
|
+
e.preventDefault();
|
|
83
|
+
if (!dragEl || dragEl === this) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const rect = this.getBoundingClientRect();
|
|
87
|
+
const mid = rect.top + rect.height / 2;
|
|
88
|
+
const clientY = e.originalEvent ? e.originalEvent.clientY : 0;
|
|
89
|
+
if (clientY < mid) {
|
|
90
|
+
this.parentNode.insertBefore(dragEl, this);
|
|
91
|
+
} else {
|
|
92
|
+
this.parentNode.insertBefore(dragEl, this.nextSibling);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
$list.on("drop.kgridChooserDrag", "li", function (e) {
|
|
96
|
+
e.preventDefault();
|
|
97
|
+
api.setLayout(layoutFromPanel($panel));
|
|
98
|
+
});
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
CT.setupColumnChooser = function ($host, options, api) {
|
|
102
|
+
if (!options.features || !options.features.columnChooser) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
const $shell = CT.getTableInteractionHost($host);
|
|
106
|
+
$shell.find(".kgrid-column-chooser").remove();
|
|
107
|
+
const label = options.columnChooserLabel || "Columns";
|
|
108
|
+
const $wrap = $("<div>").addClass("kgrid-column-chooser");
|
|
109
|
+
const $btn = $("<button>")
|
|
110
|
+
.attr("type", "button")
|
|
111
|
+
.addClass("kgrid-column-chooser-toggle btn btn-sm btn-outline-secondary")
|
|
112
|
+
.attr("title", label)
|
|
113
|
+
.attr("aria-expanded", "false")
|
|
114
|
+
.attr("aria-haspopup", "true")
|
|
115
|
+
.html(
|
|
116
|
+
'<i class="fas fa-table-columns" aria-hidden="true"></i>' +
|
|
117
|
+
'<span class="kgrid-column-chooser-toggle-label"> ' +
|
|
118
|
+
$("<div>").text(label).html() +
|
|
119
|
+
"</span>"
|
|
120
|
+
);
|
|
121
|
+
const $panel = $("<div>")
|
|
122
|
+
.addClass("kgrid-column-chooser-panel")
|
|
123
|
+
.attr("hidden", "hidden");
|
|
124
|
+
$wrap.append($btn, $panel);
|
|
125
|
+
$shell.prepend($wrap);
|
|
126
|
+
CT.renderColumnChooserPanel($panel, options, api);
|
|
127
|
+
|
|
128
|
+
function close() {
|
|
129
|
+
$panel.attr("hidden", "hidden");
|
|
130
|
+
$btn.attr("aria-expanded", "false");
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
$btn.on("click", function (e) {
|
|
134
|
+
e.stopPropagation();
|
|
135
|
+
const open = !$panel.attr("hidden");
|
|
136
|
+
if (open) {
|
|
137
|
+
close();
|
|
138
|
+
} else {
|
|
139
|
+
$panel.removeAttr("hidden");
|
|
140
|
+
$btn.attr("aria-expanded", "true");
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
const ns = "kgridChooser" + CT.uuid();
|
|
145
|
+
$(document).on("pointerdown." + ns, function (e) {
|
|
146
|
+
if (!document.body.contains($wrap[0])) {
|
|
147
|
+
$(document).off("pointerdown." + ns);
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
if (!$wrap[0].contains(e.target)) {
|
|
151
|
+
close();
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
};
|
|
155
|
+
})(window.KGrid);
|
package/src/configure.js
CHANGED
|
@@ -2,6 +2,69 @@
|
|
|
2
2
|
* Host-app integration: call KGrid.configure({ ... }) before mounting tables.
|
|
3
3
|
*/
|
|
4
4
|
(function (CT) {
|
|
5
|
+
/**
|
|
6
|
+
* Flag is on when the API/form value is true, 1, or "1".
|
|
7
|
+
* @param {*} v
|
|
8
|
+
* @returns {boolean}
|
|
9
|
+
*/
|
|
10
|
+
CT.isFlagOn = function (v) {
|
|
11
|
+
return v === true || v === 1 || v === "1";
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Walk form.elements (includes fields associated via form="id").
|
|
16
|
+
* Checkboxes always emit "1" or "0" — FormData omits unchecked boxes.
|
|
17
|
+
* @param {HTMLFormElement} form
|
|
18
|
+
* @returns {Record<string, *>}
|
|
19
|
+
*/
|
|
20
|
+
function defaultSerializeForm(form) {
|
|
21
|
+
const out = {};
|
|
22
|
+
const els = form && form.elements;
|
|
23
|
+
if (!els) {
|
|
24
|
+
return out;
|
|
25
|
+
}
|
|
26
|
+
for (let i = 0; i < els.length; i++) {
|
|
27
|
+
const el = els[i];
|
|
28
|
+
const name = el.name;
|
|
29
|
+
if (!name || el.disabled) {
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
const type = el.type;
|
|
33
|
+
if (type === "submit" || type === "button" || type === "reset" || type === "file") {
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
if (type === "checkbox") {
|
|
37
|
+
out[name] = el.checked ? "1" : "0";
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
if (type === "radio") {
|
|
41
|
+
if (!el.checked) {
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
out[name] = el.value;
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (el.tagName === "SELECT" && el.multiple) {
|
|
48
|
+
out[name] = Array.from(el.selectedOptions).map(function (o) {
|
|
49
|
+
return o.value;
|
|
50
|
+
});
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
const value = el.value;
|
|
54
|
+
if (Object.prototype.hasOwnProperty.call(out, name)) {
|
|
55
|
+
if (!Array.isArray(out[name])) {
|
|
56
|
+
out[name] = [out[name]];
|
|
57
|
+
}
|
|
58
|
+
out[name].push(value);
|
|
59
|
+
} else {
|
|
60
|
+
out[name] = value;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return out;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
CT.serializeFormDefault = defaultSerializeForm;
|
|
67
|
+
|
|
5
68
|
const defaultConfig = {
|
|
6
69
|
log: function () {},
|
|
7
70
|
onError: function (err) {
|
|
@@ -16,21 +79,7 @@
|
|
|
16
79
|
},
|
|
17
80
|
/** @type {((context: object, onConfirm: Function, onCancel?: Function) => void)|null} */
|
|
18
81
|
deleteConfirm: null,
|
|
19
|
-
serializeForm:
|
|
20
|
-
const fd = new FormData(form);
|
|
21
|
-
const out = {};
|
|
22
|
-
for (const [key, value] of fd.entries()) {
|
|
23
|
-
if (Object.prototype.hasOwnProperty.call(out, key)) {
|
|
24
|
-
if (!Array.isArray(out[key])) {
|
|
25
|
-
out[key] = [out[key]];
|
|
26
|
-
}
|
|
27
|
-
out[key].push(value);
|
|
28
|
-
} else {
|
|
29
|
-
out[key] = value;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
return out;
|
|
33
|
-
},
|
|
82
|
+
serializeForm: defaultSerializeForm,
|
|
34
83
|
/** @type {Record<string, Function|object>|null} name → wrapper fn or field type plugin */
|
|
35
84
|
customInputTypes: null,
|
|
36
85
|
/** @deprecated use customInputTypes */
|
|
@@ -39,6 +88,8 @@
|
|
|
39
88
|
kviews: null,
|
|
40
89
|
/** Default delay (ms) before filter submit; 0 = immediate. Applies to the resolved filter-submit event. */
|
|
41
90
|
filterDebounceMs: 300,
|
|
91
|
+
/** { get(key), set(key, value|null) } — null uses localStorage */
|
|
92
|
+
preferencesStorage: null,
|
|
42
93
|
};
|
|
43
94
|
|
|
44
95
|
CT._config = Object.assign({}, defaultConfig);
|
package/src/constants.js
CHANGED
|
@@ -13,8 +13,15 @@
|
|
|
13
13
|
"create": false,
|
|
14
14
|
"update": false,
|
|
15
15
|
"delete": false,
|
|
16
|
-
"clone": false
|
|
16
|
+
"clone": false,
|
|
17
|
+
"columnChooser": false
|
|
17
18
|
},
|
|
19
|
+
/** Persist key for layout (and filters). Required for localStorage. */
|
|
20
|
+
storageKey: null,
|
|
21
|
+
/** Extra suffix for saved filters only (e.g. company id). Layout ignores this. */
|
|
22
|
+
filterStorageScope: null,
|
|
23
|
+
columnChooserLabel: "Columns",
|
|
24
|
+
columnChooserResetLabel: "Reset columns",
|
|
18
25
|
"defaultInteraction": "view",
|
|
19
26
|
"insertFormRow": {
|
|
20
27
|
"position": "top"
|
|
@@ -41,6 +48,10 @@
|
|
|
41
48
|
label: null,
|
|
42
49
|
name: null,
|
|
43
50
|
hidden: false,
|
|
51
|
+
/** When true, column chooser cannot hide this column (still reorderable). */
|
|
52
|
+
locked: false,
|
|
53
|
+
/** Runtime: user hid this column via chooser. Not a schema flag. */
|
|
54
|
+
userHidden: false,
|
|
44
55
|
/** CSS class(es) on header/filter/data/insert cells (alias: columnClass) */
|
|
45
56
|
class: null,
|
|
46
57
|
columnClass: null,
|
package/src/data-body.js
CHANGED
|
@@ -9,38 +9,16 @@
|
|
|
9
9
|
return td;
|
|
10
10
|
};
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
/**
|
|
13
|
+
* Fill a data <tr> with cells + optional row-actions (KViews item template).
|
|
14
|
+
* @param {JQuery} dataRow
|
|
15
|
+
* @param {Object} options
|
|
16
|
+
* @returns {JQuery} dataRow
|
|
17
|
+
*/
|
|
18
|
+
CT.fillDataRow = function (dataRow, options) {
|
|
13
19
|
const columns = [...options.columns];
|
|
14
|
-
const dataRow = $("<tr>").appendTo(dataBody);
|
|
15
|
-
if(options.dataRowAttrs && typeof options.dataRowAttrs!=="object") {
|
|
16
|
-
throw new Error("options.dataRowAttrs must be an object");
|
|
17
|
-
}
|
|
18
|
-
const dataRowAttrs = {...(options.dataRowAttrs ?? {})};
|
|
19
|
-
Object.keys(dataRowAttrs).forEach(attr => dataRow.attr(attr,dataRowAttrs[attr]));
|
|
20
|
-
|
|
21
|
-
{
|
|
22
|
-
dataBody.data("emptyview",noDataTbody);
|
|
23
|
-
|
|
24
|
-
if(options?.features?.sorting) {
|
|
25
|
-
dataBody.data("sort",labelsRow);
|
|
26
|
-
}
|
|
27
|
-
if(pagingFooter) {
|
|
28
|
-
dataBody.data("paging",pagingFooter.find(".pages"))
|
|
29
|
-
.data("pagesizeinp",pagingFooter.find(".pagesize"))
|
|
30
|
-
.data("totalrecscount",pagingFooter.find(".totalrecscount"));
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
if(filterForm) {
|
|
34
|
-
dataBody.data("filter",filterForm);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
if(options.type) {
|
|
38
|
-
dataBody.data("type",options.type);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
20
|
const dataRowFormId = "data_row_form_"+CT.uuid()+"_{{this.id}}";
|
|
43
|
-
const editForm = options.features.update
|
|
21
|
+
const editForm = options.features && options.features.update
|
|
44
22
|
? $("<form class='edit-form table-row-form'>").attr("id", dataRowFormId)
|
|
45
23
|
: null;
|
|
46
24
|
|
|
@@ -90,7 +68,39 @@
|
|
|
90
68
|
if (editForm) {
|
|
91
69
|
CT.anchorRowForm(editForm, dataRow);
|
|
92
70
|
}
|
|
71
|
+
return dataRow;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
CT.setupDataBody = function (dataBody, options, labelsRow, filterForm, pagingFooter, noDataTbody) {
|
|
75
|
+
const dataRow = $("<tr>").appendTo(dataBody);
|
|
76
|
+
if(options.dataRowAttrs && typeof options.dataRowAttrs!=="object") {
|
|
77
|
+
throw new Error("options.dataRowAttrs must be an object");
|
|
78
|
+
}
|
|
79
|
+
const dataRowAttrs = {...(options.dataRowAttrs ?? {})};
|
|
80
|
+
Object.keys(dataRowAttrs).forEach(attr => dataRow.attr(attr,dataRowAttrs[attr]));
|
|
81
|
+
|
|
82
|
+
{
|
|
83
|
+
dataBody.data("emptyview",noDataTbody);
|
|
84
|
+
|
|
85
|
+
if(options?.features?.sorting) {
|
|
86
|
+
dataBody.data("sort",labelsRow);
|
|
87
|
+
}
|
|
88
|
+
if(pagingFooter) {
|
|
89
|
+
dataBody.data("paging",pagingFooter.find(".pages"))
|
|
90
|
+
.data("pagesizeinp",pagingFooter.find(".pagesize"))
|
|
91
|
+
.data("totalrecscount",pagingFooter.find(".totalrecscount"));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if(filterForm) {
|
|
95
|
+
dataBody.data("filter",filterForm);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if(options.type) {
|
|
99
|
+
dataBody.data("type",options.type);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
93
102
|
|
|
103
|
+
CT.fillDataRow(dataRow, options);
|
|
94
104
|
return dataBody;
|
|
95
105
|
};
|
|
96
106
|
|
package/src/dom.js
CHANGED
|
@@ -43,23 +43,58 @@
|
|
|
43
43
|
return !!(f.delete || f.update || f.create || f.clone);
|
|
44
44
|
};
|
|
45
45
|
|
|
46
|
+
/**
|
|
47
|
+
* Compact width for the row-actions column under table-layout:fixed
|
|
48
|
+
* (fixed layout ignores content; 1% caused overflow). Sized from max buttons
|
|
49
|
+
* shown in any mode (idle clone/delete vs editing save/cancel).
|
|
50
|
+
* @param {Object} [options]
|
|
51
|
+
* @returns {string} CSS width
|
|
52
|
+
*/
|
|
53
|
+
CT.actionColumnWidth = function (options) {
|
|
54
|
+
const f = (options && options.features) || {};
|
|
55
|
+
const idle = (f.clone ? 1 : 0) + (f.delete ? 1 : 0);
|
|
56
|
+
const editing = f.update ? 2 : 0;
|
|
57
|
+
const insert = f.create ? 1 : 0;
|
|
58
|
+
const n = Math.max(idle, editing, insert, 1);
|
|
59
|
+
return (2.5 * n + 0.75).toFixed(2) + "rem";
|
|
60
|
+
};
|
|
61
|
+
|
|
46
62
|
/**
|
|
47
63
|
* Sync <colgroup> so row-actions width can collapse in view (table-layout: fixed).
|
|
48
64
|
* @param {JQuery} $table
|
|
49
65
|
* @param {number} dataColumnCount visible data columns (no row-actions)
|
|
50
66
|
* @param {boolean} hasActions
|
|
67
|
+
* @param {Object} [options] table options (for action column width)
|
|
51
68
|
*/
|
|
52
|
-
CT.syncActionColumnColgroup = function ($table, dataColumnCount, hasActions) {
|
|
69
|
+
CT.syncActionColumnColgroup = function ($table, dataColumnCount, hasActions, options, layoutColumns) {
|
|
53
70
|
let $colgroup = $table.children("colgroup.kgrid-colgroup");
|
|
54
71
|
if (!$colgroup.length) {
|
|
55
72
|
$colgroup = $("<colgroup>").addClass("kgrid-colgroup").prependTo($table);
|
|
56
73
|
}
|
|
57
74
|
$colgroup.empty();
|
|
58
|
-
|
|
59
|
-
|
|
75
|
+
const named = Array.isArray(layoutColumns) ? layoutColumns : null;
|
|
76
|
+
if (named && named.length) {
|
|
77
|
+
named.forEach(function (col) {
|
|
78
|
+
const $col = $("<col>");
|
|
79
|
+
if (col && col.name) {
|
|
80
|
+
$col.attr("data-name", col.name);
|
|
81
|
+
}
|
|
82
|
+
if (col && col.userHidden) {
|
|
83
|
+
$col.addClass("kgrid-user-hidden");
|
|
84
|
+
}
|
|
85
|
+
$colgroup.append($col);
|
|
86
|
+
});
|
|
87
|
+
} else {
|
|
88
|
+
for (let i = 0; i < dataColumnCount; i++) {
|
|
89
|
+
$colgroup.append($("<col>"));
|
|
90
|
+
}
|
|
60
91
|
}
|
|
61
92
|
if (hasActions) {
|
|
62
|
-
$colgroup.append(
|
|
93
|
+
$colgroup.append(
|
|
94
|
+
$("<col>")
|
|
95
|
+
.addClass("kgrid-row-actions-col")
|
|
96
|
+
.css("width", CT.actionColumnWidth(options))
|
|
97
|
+
);
|
|
63
98
|
}
|
|
64
99
|
};
|
|
65
100
|
|
|
@@ -118,6 +153,7 @@
|
|
|
118
153
|
if (cls) {
|
|
119
154
|
$el.addClass(cls);
|
|
120
155
|
}
|
|
156
|
+
$el.toggleClass("kgrid-user-hidden", !!col.userHidden);
|
|
121
157
|
return $el;
|
|
122
158
|
};
|
|
123
159
|
})(window.KGrid);
|
package/src/events.js
CHANGED
|
@@ -64,6 +64,14 @@
|
|
|
64
64
|
$(input).val(typeof val === "boolean" ? String(val) : val);
|
|
65
65
|
});
|
|
66
66
|
|
|
67
|
+
view.el.find("input[type='checkbox']").each((index, input) => {
|
|
68
|
+
const col = colMap.get(input.name);
|
|
69
|
+
if (!col) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
input.checked = CT.isFlagOn(item.attributes[col.name]);
|
|
73
|
+
});
|
|
74
|
+
|
|
67
75
|
view.el.find("form.edit-form").off("submit").on("submit",(event)=>{
|
|
68
76
|
const form = event.target;
|
|
69
77
|
event.preventDefault();
|
package/src/filters.js
CHANGED
|
@@ -144,8 +144,9 @@
|
|
|
144
144
|
* @param {HTMLFormElement|JQuery} filterFormEl
|
|
145
145
|
* @param {Object} options
|
|
146
146
|
* @param {Object} [collection] KViews collection
|
|
147
|
+
* @param {{ skipInitSubmit?: boolean }} [extra]
|
|
147
148
|
*/
|
|
148
|
-
CT.setupDefaultFilters = function (filterFormEl, options, collection) {
|
|
149
|
+
CT.setupDefaultFilters = function (filterFormEl, options, collection, extra) {
|
|
149
150
|
const form = $(filterFormEl)[0];
|
|
150
151
|
if (!form || !options || !options.features || !options.features.filtering) {
|
|
151
152
|
return;
|
|
@@ -206,7 +207,7 @@
|
|
|
206
207
|
const missing = persisted.some(function (col) {
|
|
207
208
|
return urlFilter.indexOf(col.name + "=") === -1;
|
|
208
209
|
});
|
|
209
|
-
if (missing) {
|
|
210
|
+
if (missing && !(extra && extra.skipInitSubmit)) {
|
|
210
211
|
collection.filtering.handleSubmit(form);
|
|
211
212
|
}
|
|
212
213
|
};
|
|
@@ -361,6 +362,40 @@
|
|
|
361
362
|
return filterForm;
|
|
362
363
|
};
|
|
363
364
|
|
|
365
|
+
/** Write current form fields into collection.url.parameters.filter (same rules as KViews Filtering). */
|
|
366
|
+
CT.syncFormFiltersToCollectionUrl = function (form, collection) {
|
|
367
|
+
if (!form || !collection || !collection.url) {
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
if (!collection.url.parameters) {
|
|
371
|
+
collection.url.parameters = {};
|
|
372
|
+
}
|
|
373
|
+
const filter = [];
|
|
374
|
+
const els = form.elements;
|
|
375
|
+
if (!els) {
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
for (let i = 0; i < els.length; i++) {
|
|
379
|
+
const el = els[i];
|
|
380
|
+
if (!el || !el.name) {
|
|
381
|
+
continue;
|
|
382
|
+
}
|
|
383
|
+
const $el = $(el);
|
|
384
|
+
const value = $el.val();
|
|
385
|
+
if (value == null || value === "") {
|
|
386
|
+
continue;
|
|
387
|
+
}
|
|
388
|
+
const operator = $el.attr("data-operator") || $el.data("operator") || "=";
|
|
389
|
+
filter.push(el.name + operator + value);
|
|
390
|
+
}
|
|
391
|
+
collection.offset = 0;
|
|
392
|
+
if (filter.length) {
|
|
393
|
+
collection.url.parameters.filter = filter.join(",");
|
|
394
|
+
} else {
|
|
395
|
+
delete collection.url.parameters.filter;
|
|
396
|
+
}
|
|
397
|
+
};
|
|
398
|
+
|
|
364
399
|
CT.FilterForm = function (form) {
|
|
365
400
|
this.form = $(form)[0];
|
|
366
401
|
this.filter = (name, value, operator = "~=~") => {
|
package/src/init.js
CHANGED
|
@@ -57,9 +57,40 @@
|
|
|
57
57
|
}
|
|
58
58
|
return api.setInteraction(api.getInteraction() === "edit" ? "view" : "edit");
|
|
59
59
|
},
|
|
60
|
+
getLayout() {
|
|
61
|
+
return CT.layoutFromColumns(options.columns);
|
|
62
|
+
},
|
|
63
|
+
setLayout(layout) {
|
|
64
|
+
CT.applyColumnLayout(api, options, layout);
|
|
65
|
+
CT.preferencesSaveLayout(options, api.getLayout());
|
|
66
|
+
return api.getLayout();
|
|
67
|
+
},
|
|
68
|
+
resetLayout() {
|
|
69
|
+
const byName = new Map();
|
|
70
|
+
options.columns.forEach(function (col) {
|
|
71
|
+
if (col && col.name) {
|
|
72
|
+
byName.set(col.name, col);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
options.columns = schemaOrder
|
|
76
|
+
.map(function (name) {
|
|
77
|
+
return byName.get(name);
|
|
78
|
+
})
|
|
79
|
+
.filter(Boolean);
|
|
80
|
+
CT.preferencesSaveLayout(options, null);
|
|
81
|
+
CT.applyColumnLayout(api, options, null);
|
|
82
|
+
return api.getLayout();
|
|
83
|
+
},
|
|
60
84
|
};
|
|
61
85
|
|
|
62
|
-
options.columns = options.columns.map((col) => CT.normalizeColumnConfig(col));
|
|
86
|
+
options.columns = (options.columns || []).map((col) => CT.normalizeColumnConfig(col));
|
|
87
|
+
const schemaOrder = options.columns.map(function (col) {
|
|
88
|
+
return col.name;
|
|
89
|
+
});
|
|
90
|
+
options.columns = CT.mergeLayoutIntoColumns(
|
|
91
|
+
options.columns,
|
|
92
|
+
CT.preferencesLoadLayout(options)
|
|
93
|
+
);
|
|
63
94
|
|
|
64
95
|
const handlers = options.handlers ?? {};
|
|
65
96
|
delete options.handlers;
|
|
@@ -127,7 +158,13 @@
|
|
|
127
158
|
const hasActionColumn = CT.hasActionColumn(options);
|
|
128
159
|
const visibleColumnsCount = labelsRow.find("th").length;
|
|
129
160
|
const dataColumnCount = visibleColumnsCount - (hasActionColumn ? 1 : 0);
|
|
130
|
-
CT.syncActionColumnColgroup(
|
|
161
|
+
CT.syncActionColumnColgroup(
|
|
162
|
+
table,
|
|
163
|
+
dataColumnCount,
|
|
164
|
+
hasActionColumn,
|
|
165
|
+
options,
|
|
166
|
+
CT.chooserColumns(options.columns)
|
|
167
|
+
);
|
|
131
168
|
|
|
132
169
|
const filterForm = options.filterForm ?? CT.setupFilterHeader(table, options);
|
|
133
170
|
|
|
@@ -191,7 +228,24 @@
|
|
|
191
228
|
if (options.insertUrl) {
|
|
192
229
|
api.instance.setUrl(options.insertUrl, "insert");
|
|
193
230
|
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
CT.setupDefaultFilters(filterForm, options, api.instance, { skipInitSubmit: true });
|
|
234
|
+
const filterFormEl = filterForm && (filterForm.jquery ? filterForm[0] : filterForm);
|
|
235
|
+
CT.applyUserFilters(
|
|
236
|
+
filterFormEl,
|
|
237
|
+
options.columns,
|
|
238
|
+
CT.preferencesLoadFilters(options)
|
|
239
|
+
);
|
|
240
|
+
CT.ensureUrlFiltersOnForm(filterFormEl, api.instance);
|
|
241
|
+
CT.setupFilterPersistence(filterFormEl, options);
|
|
242
|
+
CT.setupColumnChooser($host, options, api);
|
|
243
|
+
|
|
244
|
+
if (options.url) {
|
|
194
245
|
try {
|
|
246
|
+
if (options.features && options.features.filtering && filterFormEl) {
|
|
247
|
+
CT.syncFormFiltersToCollectionUrl(filterFormEl, api.instance);
|
|
248
|
+
}
|
|
195
249
|
await api.instance.loadFromRemote();
|
|
196
250
|
} catch (error) {
|
|
197
251
|
CT.onError(error);
|
|
@@ -203,8 +257,6 @@
|
|
|
203
257
|
throw new Error("Invalid data: missing datasource url or data for table");
|
|
204
258
|
}
|
|
205
259
|
|
|
206
|
-
CT.setupDefaultFilters(filterForm, options, api.instance);
|
|
207
|
-
|
|
208
260
|
return api;
|
|
209
261
|
};
|
|
210
262
|
})(window.KGrid);
|
package/src/insert-row.js
CHANGED
|
@@ -94,6 +94,10 @@
|
|
|
94
94
|
case "hidden":
|
|
95
95
|
input = $(`<input type='hidden' class='form-input form-control form-control-sm'/>`);
|
|
96
96
|
break;
|
|
97
|
+
case "checkbox":
|
|
98
|
+
input = $("<input autocomplete='off' type='checkbox' class='form-check-input'/>");
|
|
99
|
+
input.attr("data-type", insertType);
|
|
100
|
+
break;
|
|
97
101
|
default:
|
|
98
102
|
if(CT.isValidInputType(insertType)) {
|
|
99
103
|
input = $(`<input autocomplete='off' type='${insertType}' class='form-input form-control form-control-sm'/>`);
|
|
@@ -104,12 +108,14 @@
|
|
|
104
108
|
}
|
|
105
109
|
}
|
|
106
110
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
111
|
+
const rawDefault = (typeof insertConfig.default === "object" && insertConfig.default && insertConfig.default.value != null)
|
|
112
|
+
? insertConfig.default.value
|
|
113
|
+
: insertConfig.default;
|
|
114
|
+
if (insertType === "checkbox") {
|
|
115
|
+
input.prop("checked", CT.isFlagOn(rawDefault));
|
|
116
|
+
} else if(insertConfig.default != null && insertConfig.default !== "" && !input.val()) {
|
|
117
|
+
if(String(rawDefault).trim()) {
|
|
118
|
+
input.val(rawDefault).trigger("change");
|
|
113
119
|
}
|
|
114
120
|
}
|
|
115
121
|
|
|
@@ -132,7 +138,8 @@
|
|
|
132
138
|
}
|
|
133
139
|
|
|
134
140
|
if(insertConfig.type!=="hidden") {
|
|
135
|
-
const $
|
|
141
|
+
const $control = insertType === "checkbox" ? CT.wrapFlagSwitch(input) : input;
|
|
142
|
+
const $td = $("<td>").append($control).appendTo(newRecordRow).attr("data-label", col.label);
|
|
136
143
|
CT.applyColumnCellMeta($td, col);
|
|
137
144
|
}
|
|
138
145
|
CT.mountField({
|