Skip to content

Commit 40305da

Browse files
author
hikki
committed
v3.6
1 parent 73c8696 commit 40305da

File tree

1 file changed

+48
-38
lines changed

1 file changed

+48
-38
lines changed

resources/assets/component.js

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,7 @@ window.ComponentLine = class {
862862
let foot = document.createElement('tr');
863863
foot.className = 'dlp-tr';
864864
let columns = this.COLUMNS;
865+
this.INSERT_ROW_MENUE_DATA = {};
865866
for (let column in columns) {
866867
if (!columns.hasOwnProperty(column)) continue;
867868
let val = columns[column];
@@ -878,7 +879,7 @@ window.ComponentLine = class {
878879
case 'select':
879880
let td = document.createElement('td');
880881
td.style = val.style;
881-
td.append(this.menuMake(column,[],val.options,val.options_limit,val.name));
882+
td.append(this.menuMake(column,[],val.options,val.options_limit,val.name,true));
882883
foot.append(td);
883884
break;
884885
case 'datetime':
@@ -920,40 +921,31 @@ window.ComponentLine = class {
920921
let tbody = document.createElement('tbody');
921922
let columns = this.COLUMNS;
922923
if (Array.isArray(this.DATA) === false) return;
923-
this.DATA.forEach((value, key) => {
924+
this.DATA.forEach((values, key) => {
924925
let tr = document.createElement('tr');
925926
tr.className = 'dlp-tr';
926927
tr.setAttribute('sortable-item', 'sortable-item');
927928
let record = {};
928-
for (let column in columns) {
929-
if (!columns.hasOwnProperty(column)) continue;
930-
if (columns[column].type === 'hidden') {
931-
if (value[column]) {
932-
record[column] = value[column];
933-
}
934-
continue;
929+
for (let name in columns) {
930+
if(!columns.hasOwnProperty(name))continue;
931+
let column = columns[name];
932+
if(!values[name]){
933+
this.DATA[key][name] = '';
934+
record[name] = '';
935935
}
936936
let td = document.createElement('td');
937-
if(columns[column].type === 'select') {
938-
let v = this.DATA[key][column];
937+
let v = this.DATA[key][name];
938+
if(column.type === 'select') {
939939
if (/^[0-9]+$/.test(v)) {
940940
v = [parseInt(v)];
941941
} else if (Array.isArray(v) === false) {
942942
v = [];
943943
}
944-
value[column] = v;
945944
}
946-
if (value[column]) {
947-
record[column] = value[column];
948-
this.makeTd(td, columns[column], value[column], column);
949-
} else {
950-
record[column] = '';
951-
this.makeTd(td, columns[column], '', column);
952-
}
953-
tr.setAttribute('data-key', key.toString());
945+
this.makeTd(td, name, column, v);
946+
record[name] = v;
954947
tr.appendChild(td);
955948
}
956-
957949
let td = document.createElement('td');
958950
this.operateButton(td);
959951
tr.appendChild(td);
@@ -989,7 +981,7 @@ window.ComponentLine = class {
989981
let tr = document.createElement('tr');
990982
tr.className = 'dlp-tr';
991983
tr.setAttribute('sortable-item', 'sortable-item');
992-
tr.setAttribute('data-key', this.DATA.length.toString());
984+
993985
for (let column in this.COLUMNS) {
994986
if (!this.COLUMNS.hasOwnProperty(column)) continue;
995987
let type = this.COLUMNS[column].insert_type ? this.COLUMNS[column].insert_type : this.COLUMNS[column].type;
@@ -1001,7 +993,7 @@ window.ComponentLine = class {
1001993
value = '';
1002994
}
1003995
insert[column] = value;
1004-
this.makeTd(td, this.COLUMNS[column], value, column);
996+
this.makeTd(td, column, this.COLUMNS[column], value);
1005997
tr.appendChild(td);
1006998
}
1007999

@@ -1017,7 +1009,7 @@ window.ComponentLine = class {
10171009
this.TABLE_DOM.querySelector('.insert_handel').appendChild(i);
10181010
}
10191011

1020-
makeTd(td, settings, value, column) {
1012+
makeTd(td, column, settings, value) {
10211013
let input;
10221014
let DATA = this.DATA;
10231015
let DATA_INPUT = this.DATA_INPUT;
@@ -1031,7 +1023,7 @@ window.ComponentLine = class {
10311023
input.setAttribute('data-column', column);
10321024
input.value = value;
10331025
input.addEventListener('input', () => {
1034-
let key = input.parentNode.parentNode.getAttribute('data-key');
1026+
let key = this.searchChildrenDomIndex(input.parentNode.parentNode);
10351027
let column = input.getAttribute('data-column');
10361028
if (DATA[key]) {
10371029
DATA[key][column] = input.value;
@@ -1059,7 +1051,7 @@ window.ComponentLine = class {
10591051
}
10601052
this.format_settings[column] = format;
10611053
input.addEventListener('blur', () => {
1062-
let key = input.parentNode.parentNode.getAttribute('data-key');
1054+
let key = this.searchChildrenDomIndex(input.parentNode.parentNode);
10631055
let column = input.getAttribute('data-column');
10641056
if (DATA[key]) {
10651057
DATA[key][column] = input.value;
@@ -1098,26 +1090,21 @@ window.ComponentLine = class {
10981090
D.addEventListener('click', () => {
10991091
let tr = D.parentNode.parentNode;
11001092
let tbody = tr.parentNode;
1101-
let key = tr.getAttribute('data-key');
1093+
let key = this.searchChildrenDomIndex(tr);
11021094

11031095
this.DATA.splice(key, 1);
11041096
tbody.removeChild(tr);
11051097
this.DATA_INPUT.value = JSON.stringify(this.DATA);
1106-
for (let node in tbody.childNodes) {
1107-
if (!tbody.childNodes.hasOwnProperty(node)) continue;
1108-
if (tbody.childNodes[node] instanceof HTMLElement) {
1109-
tbody.childNodes[node].setAttribute('data-key', node);
1110-
}
1111-
}
11121098
}, false);
11131099
td.appendChild(D);
11141100
}
11151101
td.className = 'operate-column';
11161102
}
11171103

1118-
menuMake(column,selected, select,limit,placeholder){
1104+
menuMake(column,selected, select,limit,placeholder,insertRow = false){
11191105
let menu = document.createElement('div');
11201106
menu.className = 'dlp-dot-menu';
1107+
if(insertRow)this.INSERT_ROW_MENUE_DATA[column] = [];
11211108

11221109
let menu_select = document.createElement('div');
11231110
menu_select.className = 'dlp-input dlp-dot-menu-select';
@@ -1144,29 +1131,46 @@ window.ComponentLine = class {
11441131
option.insertAdjacentHTML('afterbegin',`<div class="dlp dlp-text" data-v="${id}">${select[id]}</div><div></div>`);
11451132
option.addEventListener('click', ()=>{
11461133
id = parseInt(id);
1147-
let data_key = parseInt(menu.parentNode.parentNode.getAttribute('data-key'));
1148-
let selected = this.DATA[data_key][column];
1134+
let selected;
1135+
if(insertRow){
1136+
selected = this.INSERT_ROW_MENUE_DATA[column];
1137+
}else {
1138+
let key = this.searchChildrenDomIndex(menu.parentNode.parentNode);
1139+
selected = this.DATA[key][column];
1140+
}
11491141
let index = selected.indexOf(id);
11501142
if(index !== -1){
11511143
/*cancel*/
11521144
selected.splice(index,1);
11531145
option.classList.remove('option-active');
11541146
if(option.lastChild instanceof HTMLElement) option.lastChild.innerHTML = '';
11551147
menuSelect(select,selected,limit);
1156-
this.DATA[data_key][column] = selected;
11571148
if(selected.length === 0)menu_select.firstElementChild.textContent = placeholder;
1149+
if(insertRow === false){
1150+
this.DATA_INPUT.value = JSON.stringify(this.DATA);
1151+
}
11581152
return;
11591153
}
11601154
if (limit > 0 && selected.length >= limit) {
11611155
for (let line of list.childNodes){
1162-
if((selected.hasOwnProperty('0')) && line.getAttribute('data-id') === selected[0].toString()) line.click();
1156+
if((selected.hasOwnProperty('0')) && line.getAttribute('data-id') === selected[0].toString())line.click();
11631157
}
11641158
}
11651159
option.classList.add('option-active');
11661160
selected.push(id);
11671161
(option.lastChild instanceof HTMLElement) && option.lastChild.insertAdjacentHTML('afterbegin',check);
11681162
menuSelect(select,selected,limit);
1163+
if(insertRow === false){
1164+
this.DATA_INPUT.value = JSON.stringify(this.DATA);
1165+
}
11691166
}, false);
1167+
/*init selected*/
1168+
if (limit > 0 && selected.length >= limit) selected.slice(0,limit);
1169+
if(selected.indexOf(parseInt(id)) !== -1) {
1170+
option.classList.add('option-active');
1171+
(option.lastChild instanceof HTMLElement) && option.lastChild.insertAdjacentHTML('afterbegin', check);
1172+
menuSelect(select,selected,limit);
1173+
}
11701174
list.append(option);
11711175
}
11721176

@@ -1220,6 +1224,12 @@ window.ComponentLine = class {
12201224
});
12211225
}
12221226
}
1227+
1228+
searchChildrenDomIndex(dom){
1229+
let i = 0;
1230+
while((dom = dom.previousSibling) != null) i++;
1231+
return i;
1232+
}
12231233
};
12241234

12251235
window.ComponentPlane = class {

0 commit comments

Comments
 (0)