Commit be16cb41 authored by 覃振观's avatar 覃振观 👶

Grid 添加、删除

parent 1a9b5b9b
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
function mapping(dataMapping, rawData) { function mapping(dataMapping, rawData) {
window.dataSet = {}; window.dataSet = {};
window.dataSet.tempId = 950366; window.dataSet.tempId = 950366;
debugger;
if(!rawData) { if(!rawData) {
window.dataSet.isInsert = true; window.dataSet.isInsert = true;
var rawData = {}; var rawData = {};
...@@ -49,24 +48,28 @@ function mapping(dataMapping, rawData) { ...@@ -49,24 +48,28 @@ function mapping(dataMapping, rawData) {
} }
let listMap = {}, ids = [], rowId = ''; //tryForEach 变量作用域注意 let listMap = {}, ids = [], rowId = ''; //tryForEach 变量作用域注意
entityData.forEach(rowData => { entityData.forEach(rowData => {
debugger;
rowId = rowData[propertyNames.id]; rowId = rowData[propertyNames.id];
rowData['rowIndex'] = rowId;
listMap[rowId] = rowData; listMap[rowId] = rowData;
ids.push(rowId); ids.push(rowId);
tryForEach(propertyNames.columns, (property) => {
if(!mapper[property]) { settingGridMapper(propertyNames.columns, mapper, rowId, beanName);
mapper[property] = {}; // propertyNames.columns.forEach(property => {
} // if(!mapper[property]) {
let data = {}; // mapper[property] = {};
data['listeningKey'] = rowId; // }
mapper[property][beanName] = data; // if(!mapper[property][beanName]) {
}); // mapper[property][beanName] = {};
// }
// let mapperData = {};
// mapperData[rowId] = {};
// mapper[property][beanName].push(mapperData);
// });
}) })
rawData[beanName] = {}; rawData[beanName] = {};
rawData[beanName].raw = entityData; rawData[beanName].raw = entityData;
rawData[beanName].compares = listMap; rawData[beanName].compares = listMap;
rawData[beanName].ids = ids; rawData[beanName].ids = ids;
debugger;
return; return;
} }
}); });
...@@ -80,6 +83,21 @@ function mapping(dataMapping, rawData) { ...@@ -80,6 +83,21 @@ function mapping(dataMapping, rawData) {
return returnData; return returnData;
} }
function settingGridMapper(propertyNames, mapper, rowId, beanName, data) {
propertyNames.forEach(property => {
if(!mapper[property]) {
mapper[property] = {};
}
if(!mapper[property][beanName]) {
mapper[property][beanName] = {};
}
mapper[property][beanName][rowId] = {};
if(data) {
data[property] = '';
}
});
}
/** /**
* 映射关系 => elementName(alias): { objName: property } * 映射关系 => elementName(alias): { objName: property }
* @param data 数据源 * @param data 数据源
...@@ -158,17 +176,13 @@ function checkArrayMapper(str) { ...@@ -158,17 +176,13 @@ function checkArrayMapper(str) {
* @returns {*} * @returns {*}
*/ */
function listeningGrid(elId, options) { function listeningGrid(elId, options) {
tryForEach(Object.entries(window.dataMapping), entry => { let currentMapper = checkArrayMapper(elId);
let beanName = entry[0], property = entry[1];
if(elId.indexOf(beanName) >= 0) { if(!currentMapper.property.id) {
let gridId = property.id;
if(!gridId) {
console.error("若需要监听 GridData ,必须在映射配置指定 ID !请检查。"); console.error("若需要监听 GridData ,必须在映射配置指定 ID !请检查。");
throw {}; return;
}
options.columns.unshift({ name: gridId, hide: true });
} }
}); options.columns.unshift({ name: 'rowIndex', hide: true });
debugger; debugger;
let data = {}; let data = {};
if(!options.data){ if(!options.data){
...@@ -184,15 +198,14 @@ function listeningGrid(elId, options) { ...@@ -184,15 +198,14 @@ function listeningGrid(elId, options) {
toolbar.items toolbar.items
tryForEach(toolbar.items, but => { tryForEach(toolbar.items, but => {
// ------------------ Grid 新增行时,设置 mapper、 newData ----------------- // ------------------ Grid 新增行时,设置 mapper、 newData -----------------
debugger;
if(but.id === "menuAdd") { if(but.id === "menuAdd") {
// 使用 gridManager 对象 可以省略 页面监听操作。需要修改。 // 使用 gridManager 对象 可以省略 页面监听操作。需要修改。
const originalOnClick = but.click; const originalOnClick = but.click;
but.click = function() { but.click = function() {
Object.entries(window.dataMapping).forEach(entry => { debugger;
let beanName = entry[0]; let currentMapper = checkArrayMapper(elId);
if(elId.indexOf(beanName) >= 0) { let beanName = currentMapper.beanName;
let idProperty = entry[1].id; let idProperty = currentMapper.property.id;
if(!idProperty) { if(!idProperty) {
console.error("若需要监听 GridData ,必须在映射配置指定 ID !请检查。"); console.error("若需要监听 GridData ,必须在映射配置指定 ID !请检查。");
throw {}; throw {};
...@@ -200,18 +213,19 @@ function listeningGrid(elId, options) { ...@@ -200,18 +213,19 @@ function listeningGrid(elId, options) {
let mapper = window.dataMapping.mapper; let mapper = window.dataMapping.mapper;
let rowId = window.dataSet.tempId += 1 let rowId = window.dataSet.tempId += 1
let data = {}, mapperData = {}; let data = {}, mapperData = {};
// ------------- 设置 mapper ------------- // ------------- 设置 mapper -------------
debugger; settingGridMapper(currentMapper.property.columns, mapper, rowId, beanName);
tryForEach(entry[1].columns, property => {
mapperData['listeningKey'] = rowId + ''; // currentMapper.property.columns.forEach(property => {
if(!mapper[property]) { // mapperData[rowId] = {};
mapper[property] = {}; // if(!mapper[property]) {
} // mapper[property] = {};
mapper[property][beanName] = mapperData; // }
data[property] = ''; // mapper[property][beanName] = mapperData;
}); //
// });
data[idProperty] = rowId; data[idProperty] = rowId;
data['rowIndex'] =rowId
// ------------- 设置 compares ------------- // ------------- 设置 compares -------------
if(!window.dataSet.newData[beanName]) { if(!window.dataSet.newData[beanName]) {
window.dataSet.newData[beanName] = {}; window.dataSet.newData[beanName] = {};
...@@ -220,15 +234,12 @@ function listeningGrid(elId, options) { ...@@ -220,15 +234,12 @@ function listeningGrid(elId, options) {
window.dataSet.newData[beanName].compares = {}; window.dataSet.newData[beanName].compares = {};
} }
window.dataSet.newData[beanName].compares[rowId] = data; window.dataSet.newData[beanName].compares[rowId] = data;
// ------------- 设置 ids ------------- // ------------- 设置 ids -------------
if(!window.dataSet.newData[beanName].ids) { if(!window.dataSet.newData[beanName].ids) {
window.dataSet.newData[beanName].ids = []; window.dataSet.newData[beanName].ids = [];
} }
window.dataSet.newData[beanName].ids.push(rowId); window.dataSet.newData[beanName].ids.push(rowId);
UICtrl.addGridRow(gridManager, data); UICtrl.addGridRow(gridManager, data);
}
})
originalOnClick.call(this, data); originalOnClick.call(this, data);
} }
} }
...@@ -253,7 +264,6 @@ function listeningGrid(elId, options) { ...@@ -253,7 +264,6 @@ function listeningGrid(elId, options) {
} }
} }
}) })
debugger;
} }
addObserver(elId); addObserver(elId);
var gridManager = UICtrl.grid(elId, options); var gridManager = UICtrl.grid(elId, options);
...@@ -284,7 +294,7 @@ function onBeforeEdit_(options) { ...@@ -284,7 +294,7 @@ function onBeforeEdit_(options) {
console.error("dataMapping 中,无法找到该 DOMId 的映射。请检查!"); console.error("dataMapping 中,无法找到该 DOMId 的映射。请检查!");
} else { } else {
//mapperEntry[currentMapper.beanName]['rowId'] = parentDomId; //mapperEntry[currentMapper.beanName]['rowId'] = parentDomId;
mapperEntry[currentMapper.beanName]['rowIndex'] = editParm.rowindex; mapperEntry[currentMapper.beanName][rowId]['rowIndex'] = editParm.rowindex;
} }
} }
originalOnBeforeEdit.call(this, editParm, '11'); originalOnBeforeEdit.call(this, editParm, '11');
...@@ -310,26 +320,33 @@ function inputEventListener() { ...@@ -310,26 +320,33 @@ function inputEventListener() {
function listenerContent(event) { function listenerContent(event) {
const property = event.target.getAttribute('name'), editId = event.target.id; const property = event.target.getAttribute('name'), editId = event.target.id;
const beanMapping = window.dataMapping.mapper[property]; const beanMapping = window.dataMapping.mapper[property];
debugger;
if(!beanMapping) { if(!beanMapping) {
console.error("The configuration was not found in the 'dataMapping' : '"+property+"'") console.error("The configuration was not found in the 'dataMapping' : '"+property+"'")
} }
let control = getControlType(beanMapping); let control;
let inputIds = event.target.id.split('_');
let currentMapper = checkArrayMapper(inputIds[0]);
if (currentMapper.property.id) {
control = 'grid';
} else {
control = 'form';
}
// Grid 时执行此逻辑 // Grid 时执行此逻辑
if(control === 'grid') { if(control === 'grid') {
let inputIds = editId.split('_');
debugger; debugger;
tryForEach(Object.entries(beanMapping), mapperEntry => { tryForEach(Object.entries(beanMapping), mapperEntry => {
let mapperKey = mapperEntry[0], mapperVal = mapperEntry[1]; let mapperKey = mapperEntry[0], mapperVal = mapperEntry[1];
if(inputIds[0].lastIndexOf(mapperKey) >= 0) {
if(mapperVal.rowIndex === inputIds[2] * 1) { Object.entries(mapperEntry[1]).forEach(rowEntry => {
window.dataSet.newData[mapperKey].compares[mapperVal.listeningKey][inputIds[1]] = event.target.value; let rowId = rowEntry[0];
if(rowEntry[1].rowIndex != undefined) {
if(rowEntry[1].rowIndex === inputIds[2] * 1) {
window.dataSet.newData[mapperKey].compares[rowId][inputIds[1]] = event.target.value;
console.log('Input value changed:', event.target.value); console.log('Input value changed:', event.target.value);
} else {
console.error('当前输入框 '+ event.target.id+ '无法与数据行'+ mapperKey + ':' + mapperVal.rowIndex + '匹配!');
} }
} }
}) })
})
return; return;
} }
// Form 时执行此逻辑 // Form 时执行此逻辑
...@@ -344,16 +361,17 @@ function listenerContent(event) { ...@@ -344,16 +361,17 @@ function listenerContent(event) {
} }
} }
function getControlType(beanMapping) { // function getControlType(beanMapping) {
let control; // let control;
// 这里根据 grid 与 form 的 mapper 数据结构差异判定。 需要优化 // // 这里根据 grid 与 form 的 mapper 数据结构差异判定。 需要优化
if(Object.entries(beanMapping)[0][1]['listeningKey']) { // debugger;
control = 'grid'; // if(Object.entries(beanMapping)[0][1]['listeningKey']) {
} else { // control = 'grid';
control = 'form'; // } else {
} // control = 'form';
return control; // }
} // return control;
// }
/** /**
* 添加 DOM 监听 * 添加 DOM 监听
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment