|
@@ -6,17 +6,43 @@
|
|
|
* @version
|
|
|
*/
|
|
|
let header = [];
|
|
|
-let sheet = null;
|
|
|
+let gljSheet = null;
|
|
|
+let sourceData = [];
|
|
|
+let spread = null;
|
|
|
+let isChanging = false;
|
|
|
+let gljActiveRowIndex = 0;
|
|
|
$(document).ready(function () {
|
|
|
- // excel
|
|
|
- header = [
|
|
|
+
|
|
|
+ // 初始化表格
|
|
|
+ initExcel();
|
|
|
+
|
|
|
+ // 是否主动更改数据
|
|
|
+ $("#message").on('click', '#load-data', function() {
|
|
|
+ $("#notify").slideUp('fast');
|
|
|
+ if (changeInfo.length > 0) {
|
|
|
+ for (let index in changeInfo) {
|
|
|
+ let cell = sheet.getCell(changeInfo[index].row, changeInfo[index].col, GC.Spread.Sheets.SheetArea.viewport);
|
|
|
+ cell.value(changeInfo[index].newValue);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ changeInfo = [];
|
|
|
+ });
|
|
|
+
|
|
|
+});
|
|
|
+
|
|
|
+/**
|
|
|
+ * 初始化表格
|
|
|
+ *
|
|
|
+ */
|
|
|
+function initExcel() {
|
|
|
+ header = [
|
|
|
{name: '编码', field: 'code', visible: true},
|
|
|
{name: '名称', field: 'name', visible: true},
|
|
|
{name: '规格型号', field: 'unit_price.specs', visible: true},
|
|
|
{name: '单位', field: 'unit_price.unit', visible: true},
|
|
|
{name: 'ID', field: 'id', visible: false},
|
|
|
{name: '类型', field: 'unit_price.type', visible: false},
|
|
|
- {name: '总消耗量', field: '', visible: true},
|
|
|
+ {name: '总消耗量', field: 'quantity', visible: true},
|
|
|
{name: '基价单价', field: "unit_price.base_price", visible: true},
|
|
|
{name: '调整基价', field: 'adjust_price', visible: true},
|
|
|
{name: '市场单价', field: "unit_price.market_price", visible: true, validator: 'number'},
|
|
@@ -29,161 +55,56 @@ $(document).ready(function () {
|
|
|
{name: 'UID', field: 'unit_price.id', visible: false},
|
|
|
];
|
|
|
// 数据转换格式
|
|
|
- let sourceData = JSON.parse(jsonData);
|
|
|
-
|
|
|
+ sourceData = JSON.parse(jsonData);
|
|
|
+ let columnInfo = [];
|
|
|
let setting = {
|
|
|
header: []
|
|
|
};
|
|
|
for(let tmp of header) {
|
|
|
setting.header.push({headerName: tmp.name, headerWidth: 120});
|
|
|
+ columnInfo.push({name: tmp.field, displayName: tmp.name, visible: tmp.visible, cellType: tmp.cellType, size: 120});
|
|
|
}
|
|
|
|
|
|
- let spread = sheetCommonObj.buildSheet(document.getElementById("test"), setting, sourceData.length);
|
|
|
+ spread = sheetCommonObj.buildSheet(document.getElementById("project-glj"), setting, sourceData.length);
|
|
|
spread.options.scrollbarShowMax = true;
|
|
|
spread.options.scrollbarMaxAlign = true;
|
|
|
spread.options.showHorizontalScrollbar = true;
|
|
|
- sheet = spread.getActiveSheet();
|
|
|
+ gljSheet = spread.getActiveSheet();
|
|
|
|
|
|
// 设置表单不可编辑
|
|
|
- sheet.options.isProtected = true;
|
|
|
+ gljSheet.options.isProtected = true;
|
|
|
|
|
|
// 居中样式
|
|
|
let centerStyleSetting = {hAlign: 1};
|
|
|
- sheet.setStyle(-1, 10, getStyle(centerStyleSetting), GC.Spread.Sheets.SheetArea.viewport);
|
|
|
- sheet.setStyle(-1, 15, getStyle(centerStyleSetting), GC.Spread.Sheets.SheetArea.viewport);
|
|
|
- sheet.setStyle(-1, 3, getStyle(centerStyleSetting), GC.Spread.Sheets.SheetArea.viewport);
|
|
|
+ gljSheet.setStyle(-1, 10, getStyle(centerStyleSetting), GC.Spread.Sheets.SheetArea.viewport);
|
|
|
+ gljSheet.setStyle(-1, 15, getStyle(centerStyleSetting), GC.Spread.Sheets.SheetArea.viewport);
|
|
|
+ gljSheet.setStyle(-1, 3, getStyle(centerStyleSetting), GC.Spread.Sheets.SheetArea.viewport);
|
|
|
|
|
|
// 向右对齐样式
|
|
|
let rightStyleSetting = {hAlign: GC.Spread.Sheets.HorizontalAlign.right};
|
|
|
- sheet.setStyle(-1, 6, getStyle(rightStyleSetting), GC.Spread.Sheets.SheetArea.viewport);
|
|
|
- sheet.setStyle(-1, 7, getStyle(rightStyleSetting), GC.Spread.Sheets.SheetArea.viewport);
|
|
|
- sheet.setStyle(-1, 8, getStyle(rightStyleSetting), GC.Spread.Sheets.SheetArea.viewport);
|
|
|
- sheet.setStyle(-1, 9, getStyle(rightStyleSetting), GC.Spread.Sheets.SheetArea.viewport);
|
|
|
+ gljSheet.setStyle(-1, 6, getStyle(rightStyleSetting), GC.Spread.Sheets.SheetArea.viewport);
|
|
|
+ gljSheet.setStyle(-1, 7, getStyle(rightStyleSetting), GC.Spread.Sheets.SheetArea.viewport);
|
|
|
+ gljSheet.setStyle(-1, 8, getStyle(rightStyleSetting), GC.Spread.Sheets.SheetArea.viewport);
|
|
|
+ gljSheet.setStyle(-1, 9, getStyle(rightStyleSetting), GC.Spread.Sheets.SheetArea.viewport);
|
|
|
|
|
|
|
|
|
// 设置可编辑列
|
|
|
- sheet.getRange(-1, 9, -1, 1).locked(false);
|
|
|
- sheet.getRange(-1, 10, -1, 1).locked(false);
|
|
|
- sheet.getRange(-1, 15, -1, 1).locked(false);
|
|
|
+ gljSheet.getRange(-1, 9, -1, 1).locked(false);
|
|
|
+ gljSheet.getRange(-1, 10, -1, 1).locked(false);
|
|
|
+ gljSheet.getRange(-1, 15, -1, 1).locked(false);
|
|
|
|
|
|
- // 设置json数据
|
|
|
- let rowCounter = 0;
|
|
|
- for(let data of sourceData) {
|
|
|
- let columnCounter = 0;
|
|
|
- for(let columnInfo of header) {
|
|
|
- // 设置是否显示
|
|
|
- sheet.setColumnVisible(columnCounter, columnInfo.visible);
|
|
|
-
|
|
|
- let dataString = columnInfo.field !== '' && columnInfo.field !== undefined ?
|
|
|
- "data." + columnInfo.field : '';
|
|
|
- let cellData = eval(dataString);
|
|
|
-
|
|
|
- // 设置复选框
|
|
|
- if (columnInfo.cellType !== undefined) {
|
|
|
- let checkbox = new GC.Spread.Sheets.CellTypes.CheckBox();
|
|
|
- sheet.setCellType(rowCounter, columnCounter, checkbox, GC.Spread.Sheets.SheetArea.viewport);
|
|
|
- }
|
|
|
+ // 设置数据
|
|
|
+ gljSheet.autoGenerateColumns = false;
|
|
|
+ gljSheet.bindColumns(columnInfo);
|
|
|
+ gljSheet.setDataSource(sourceData);
|
|
|
|
|
|
- // 如果不为材料“是否暂估列”根据条件显示
|
|
|
- if (columnInfo.field === 'is_evaluate' && materialIdList.indexOf(data.unit_price.type + '') < 0) {
|
|
|
- let string = new GC.Spread.Sheets.CellTypes.Text();
|
|
|
- sheet.setCellType(rowCounter, columnCounter, string, GC.Spread.Sheets.SheetArea.viewport);
|
|
|
- cellData = '';
|
|
|
- // 并且锁定该单元格
|
|
|
- sheet.getRange(rowCounter, columnCounter, 1, 1).locked(true);
|
|
|
- }
|
|
|
-
|
|
|
- // 如果类型为混凝土、砂浆、配合比、机械,则市场单价不能修改
|
|
|
- if (columnInfo.field === 'unit_price.market_price' && canNotChangeTypeId.indexOf(data.unit_price.type) >= 0) {
|
|
|
- // 锁定该单元格
|
|
|
- sheet.getRange(rowCounter, columnCounter, 1, 1).locked(true);
|
|
|
- }
|
|
|
-
|
|
|
- sheet.setValue(rowCounter, columnCounter, cellData, GC.Spread.Sheets.SheetArea.viewport);
|
|
|
- columnCounter++;
|
|
|
- }
|
|
|
- rowCounter++;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- // 是否主动更改数据
|
|
|
- $("#message").on('click', '#load-data', function() {
|
|
|
- $("#notify").slideUp('fast');
|
|
|
- if (changeInfo.length > 0) {
|
|
|
- for (let index in changeInfo) {
|
|
|
- let cell = sheet.getCell(changeInfo[index].row, changeInfo[index].col, GC.Spread.Sheets.SheetArea.viewport);
|
|
|
- cell.value(changeInfo[index].newValue);
|
|
|
- }
|
|
|
- }
|
|
|
- changeInfo = [];
|
|
|
- });
|
|
|
+ specialColumn(sourceData);
|
|
|
|
|
|
// 绑定事件
|
|
|
- let isChanging = false;
|
|
|
- sheet.bind(GC.Spread.Sheets.Events.ValueChanged, function (element, info) {
|
|
|
- // 获取修改的数据
|
|
|
- let column = info.col;
|
|
|
- let row = info.row;
|
|
|
- let model = 'glj';
|
|
|
- let idString = 'id';
|
|
|
- let field = header[column] !== undefined && header[column].field !== undefined ?
|
|
|
- header[column].field : '';
|
|
|
- if (field === '') {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- // 切割字段
|
|
|
- let fieldArray = field.split('.');
|
|
|
- idString = fieldArray.length > 1 ? 'unit_price.id' : idString;
|
|
|
-
|
|
|
- // 防止快速同时提交
|
|
|
- if (isChanging) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- // 校验数据
|
|
|
- let validator = header[column].validator !== undefined ? header[column].validator : null;
|
|
|
- let value = info.newValue;
|
|
|
- if (validator && !checkData(validator, value)) {
|
|
|
- alert('数据格式错误,请重新输入!');
|
|
|
- sheet.setValue(row, column, info.oldValue);
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- // 获取id
|
|
|
- let idColumn = getFieldColumn(header, idString);
|
|
|
- if (idColumn < 0) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- let id = sheet.getValue(row, idColumn);
|
|
|
-
|
|
|
- $.ajax({
|
|
|
- url: '/glj/update',
|
|
|
- type: 'post',
|
|
|
- data: {id: id, field: field, value: value, model: model},
|
|
|
- dataType: 'json',
|
|
|
- error: function() {
|
|
|
- alert('数据传输有误!');
|
|
|
- isChanging = false;
|
|
|
- },
|
|
|
- beforeSend: function() {
|
|
|
- isChanging = true;
|
|
|
- },
|
|
|
- success: function(response) {
|
|
|
- isChanging = false;
|
|
|
- // 修改失败则恢复原值
|
|
|
- if (response.err !== 0) {
|
|
|
- sheet.setValue(row, column, info.oldValue);
|
|
|
- alert('更改数据失败!');
|
|
|
- } else {
|
|
|
- // 成功则触发相应事件
|
|
|
- successTrigger(field, info);
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
- });
|
|
|
+ gljSheet.bind(GC.Spread.Sheets.Events.ValueChanged, updateBindEvent);
|
|
|
|
|
|
// 绑定双击事件
|
|
|
- sheet.bind(GC.Spread.Sheets.Events.CellDoubleClick, function (element, info) {
|
|
|
+ gljSheet.bind(GC.Spread.Sheets.Events.CellDoubleClick, function (element, info) {
|
|
|
let column = info.col;
|
|
|
let row = info.row;
|
|
|
let field = header[column] !== undefined && header[column].field !== undefined ?
|
|
@@ -191,7 +112,7 @@ $(document).ready(function () {
|
|
|
|
|
|
// 获取类型
|
|
|
let typeColumn = getFieldColumn(header, 'unit_price.type');
|
|
|
- let type = sheet.getValue(row, typeColumn);
|
|
|
+ let type = gljSheet.getValue(row, typeColumn);
|
|
|
|
|
|
// 如果类型为混凝土、砂浆、配合比、机械,则提示
|
|
|
if (field === 'unit_price.market_price' && canNotChangeTypeId.indexOf(type) >= 0) {
|
|
@@ -199,7 +120,11 @@ $(document).ready(function () {
|
|
|
}
|
|
|
});
|
|
|
|
|
|
-});
|
|
|
+ // 绑定单击事件
|
|
|
+ gljSheet.bind(GC.Spread.Sheets.Events.CellClick, function (element, info) {
|
|
|
+ gljActiveRowIndex = gljSheet.getActiveRowIndex();
|
|
|
+ });
|
|
|
+}
|
|
|
|
|
|
/**
|
|
|
* 生成样式
|
|
@@ -272,7 +197,7 @@ function successTrigger(field, info) {
|
|
|
let row = info.row;
|
|
|
// 获取类型
|
|
|
let typeColumn = getFieldColumn(header, 'unit_price.type');
|
|
|
- let type = sheet.getValue(row, typeColumn);
|
|
|
+ let type = gljSheet.getValue(row, typeColumn);
|
|
|
|
|
|
// 基价单价的计算
|
|
|
basePriceCalculate(type, info);
|
|
@@ -288,6 +213,37 @@ function successTrigger(field, info) {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 设置特殊单元格数据
|
|
|
+ *
|
|
|
+ * @param {object} sourceData
|
|
|
+ * @return {void}
|
|
|
+ */
|
|
|
+function specialColumn(sourceData) {
|
|
|
+ let rowCounter = 0;
|
|
|
+ // 获取是否暂估的列号
|
|
|
+ let isEvaluateColumn = getFieldColumn(header, 'is_evaluate');
|
|
|
+ // 获取市场单价列号
|
|
|
+ let marketPriceColumn = getFieldColumn(header, 'unit_price.market_price');
|
|
|
+ for(let data of sourceData) {
|
|
|
+ // 只有材料才显示是否暂估
|
|
|
+ if (materialIdList.indexOf(data.unit_price.type + '') < 0) {
|
|
|
+ let string = new GC.Spread.Sheets.CellTypes.Text();
|
|
|
+ gljSheet.setCellType(rowCounter, isEvaluateColumn, string, GC.Spread.Sheets.SheetArea.viewport);
|
|
|
+ // 锁定该单元格
|
|
|
+ gljSheet.getRange(rowCounter, isEvaluateColumn, 1, 1).locked(true);
|
|
|
+ gljSheet.setValue(rowCounter, isEvaluateColumn, '');
|
|
|
+ }
|
|
|
+ // 如果类型为混凝土、砂浆、配合比、机械,则市场单价不能修改
|
|
|
+ if (canNotChangeTypeId.indexOf(data.unit_price.type) >= 0) {
|
|
|
+ // 锁定该单元格
|
|
|
+ gljSheet.getRange(rowCounter, marketPriceColumn, 1, 1).locked(true);
|
|
|
+ }
|
|
|
+ rowCounter++;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
* 基价单价计算
|
|
|
*
|
|
|
* @param {Number} type
|
|
@@ -300,7 +256,7 @@ function basePriceCalculate(type, info) {
|
|
|
// 主材、设备自动赋值基价单价=市场单价
|
|
|
case GLJTypeConst.MAIN_MATERIAL:
|
|
|
case GLJTypeConst.EQUIPMENT:
|
|
|
- sheet.setValue(info.row, basePriceColumn, info.newValue);
|
|
|
+ gljSheet.setValue(info.row, basePriceColumn, info.newValue);
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
@@ -320,7 +276,7 @@ function adjustPriceCalculate(type, info) {
|
|
|
case GLJTypeConst.MAIN_MATERIAL:
|
|
|
case GLJTypeConst.EQUIPMENT:
|
|
|
let basePrice = sheet.getValue(info.row, basePriceColumn);
|
|
|
- sheet.setValue(info.row, adjustPriceColumn, basePrice);
|
|
|
+ gljSheet.setValue(info.row, adjustPriceColumn, basePrice);
|
|
|
break;
|
|
|
|
|
|
}
|
|
@@ -337,9 +293,95 @@ function marketPriceCalculate(type, info) {
|
|
|
switch (type) {
|
|
|
// 人工、材料(普通材料)触发 需计算混凝土、砂浆、配合比、机械的市场单价 @todo 后续添加
|
|
|
case GLJTypeConst.LABOUR:
|
|
|
- case GLJTypeConst.LABOUR:
|
|
|
+ case GLJTypeConst.GENERAL_MATERIAL:
|
|
|
// 计算
|
|
|
console.log('触发计算');
|
|
|
break;
|
|
|
}
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 筛选数据
|
|
|
+ *
|
|
|
+ * @param {Array} typeList
|
|
|
+ * @return {void}
|
|
|
+ */
|
|
|
+function filterDataByType(type) {
|
|
|
+ sourceData = [];
|
|
|
+ let testData = JSON.parse(jsonData);
|
|
|
+ for(let tmp of testData) {
|
|
|
+ if (type.indexOf(tmp.unit_price.type) >= 0) {
|
|
|
+ sourceData.push(tmp);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ gljSheet.setDataSource(sourceData);
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 绑定事件
|
|
|
+ *
|
|
|
+ * @param {object} element
|
|
|
+ * @param {object} info
|
|
|
+ * @return {void|boolean}
|
|
|
+ */
|
|
|
+function updateBindEvent(element, info) {
|
|
|
+ // 获取修改的数据
|
|
|
+ let column = info.col;
|
|
|
+ let row = info.row;
|
|
|
+ let model = 'glj';
|
|
|
+ let idString = 'id';
|
|
|
+ let field = header[column] !== undefined && header[column].field !== undefined ?
|
|
|
+ header[column].field : '';
|
|
|
+ if (field === '') {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 切割字段
|
|
|
+ let fieldArray = field.split('.');
|
|
|
+ idString = fieldArray.length > 1 ? 'unit_price.id' : idString;
|
|
|
+
|
|
|
+ // 防止快速同时提交
|
|
|
+ if (isChanging) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ // 校验数据
|
|
|
+ let validator = header[column].validator !== undefined ? header[column].validator : null;
|
|
|
+ let value = info.newValue;
|
|
|
+ if (validator && !checkData(validator, value)) {
|
|
|
+ alert('数据格式错误,请重新输入!');
|
|
|
+ gljSheet.setValue(row, column, info.oldValue);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取id
|
|
|
+ let idColumn = getFieldColumn(header, idString);
|
|
|
+ if (idColumn < 0) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ let id = gljSheet.getValue(row, idColumn);
|
|
|
+
|
|
|
+ $.ajax({
|
|
|
+ url: '/glj/update',
|
|
|
+ type: 'post',
|
|
|
+ data: {id: id, field: field, value: value, model: model},
|
|
|
+ dataType: 'json',
|
|
|
+ error: function() {
|
|
|
+ alert('数据传输有误!');
|
|
|
+ isChanging = false;
|
|
|
+ },
|
|
|
+ beforeSend: function() {
|
|
|
+ isChanging = true;
|
|
|
+ },
|
|
|
+ success: function(response) {
|
|
|
+ isChanging = false;
|
|
|
+ // 修改失败则恢复原值
|
|
|
+ if (response.err !== 0) {
|
|
|
+ gljSheet.setValue(row, column, info.oldValue);
|
|
|
+ alert('更改数据失败!');
|
|
|
+ } else {
|
|
|
+ // 成功则触发相应事件
|
|
|
+ successTrigger(field, info);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|