|
|
@@ -1,57 +1,74 @@
|
|
|
<!-- 搜索表单 -->
|
|
|
<template>
|
|
|
<div style="margin-top: 10px">
|
|
|
- <el-button type="primary" @click="addColumn" v-if="edit">新增列</el-button>
|
|
|
- <el-button type="primary" @click="addRow" v-if="edit">新增行</el-button>
|
|
|
-
|
|
|
- <table style="margin-top: 10px" :id="id">
|
|
|
- <thead>
|
|
|
- <tr>
|
|
|
- <th
|
|
|
- v-for="(item, index) in columns"
|
|
|
- class="tableTh"
|
|
|
- :style="{ width: item.width + 'px' }"
|
|
|
- >
|
|
|
- <i
|
|
|
- class="el-icon-delete delete"
|
|
|
- style="display: none"
|
|
|
- @click="removeColumn(index)"
|
|
|
- v-if="edit"
|
|
|
- ></i>
|
|
|
- <input
|
|
|
- v-model="item.value"
|
|
|
- type="text"
|
|
|
- class="templateInput"
|
|
|
- :id="item.id"
|
|
|
- :readonly="item.readonly == 2 || !edit"
|
|
|
- @click="inputClick(item, 'columns')"
|
|
|
- />
|
|
|
- </th>
|
|
|
- <th v-if="edit"></th>
|
|
|
- </tr>
|
|
|
- </thead>
|
|
|
- <tbody>
|
|
|
- <tr v-for="(row, rowIndex) in rows">
|
|
|
- <td v-for="item in row.cells"
|
|
|
- ><input
|
|
|
- v-model="item.value"
|
|
|
- type="text"
|
|
|
- class="templateInput"
|
|
|
- :id="item.id"
|
|
|
- :readonly="item.readonly == 2 || !edit"
|
|
|
- @click="inputClick(item)"
|
|
|
- @input="calculation"
|
|
|
- /></td>
|
|
|
- <td v-if="edit">
|
|
|
- <i
|
|
|
- class="sort-handle el-icon-delete delete"
|
|
|
- style="color: #f56c6c"
|
|
|
- @click="removeRow(rowIndex)"
|
|
|
- ></i
|
|
|
- ></td>
|
|
|
- </tr>
|
|
|
- </tbody>
|
|
|
- </table>
|
|
|
+ <el-button type="primary" @click="addColumn()" v-if="edit"
|
|
|
+ >新增列</el-button
|
|
|
+ >
|
|
|
+ <el-button type="primary" @click="addRow(columns.length)" v-if="edit"
|
|
|
+ >新增行</el-button
|
|
|
+ >
|
|
|
+ <div class="table" style="margin-top: 10px" :id="id">
|
|
|
+ <div class="table-body" style="display: flex">
|
|
|
+ <template v-for="(row, index) in columns">
|
|
|
+ <div class="column" :style="{ width: row[0].width + 'px' }">
|
|
|
+ <div
|
|
|
+ class="table-body-item tableTd"
|
|
|
+ v-for="(item, rowIndex) in row"
|
|
|
+ :style="{
|
|
|
+ height: item.rowspan * 30 + 'px',
|
|
|
+ display: item.rowspan ? 'block' : 'none'
|
|
|
+ }"
|
|
|
+ >
|
|
|
+ <i
|
|
|
+ class="el-icon-delete delete"
|
|
|
+ style="display: none"
|
|
|
+ @click="removeColumn(index)"
|
|
|
+ v-if="edit && rowIndex == 0"
|
|
|
+ ></i>
|
|
|
+ <i
|
|
|
+ class="el-icon-circle-plus-outline add"
|
|
|
+ style="display: none"
|
|
|
+ @click="addColumn(index)"
|
|
|
+ v-if="edit && rowIndex == 0"
|
|
|
+ ></i>
|
|
|
+ <i
|
|
|
+ class="el-icon-delete deleteRow"
|
|
|
+ @click="removeRow(rowIndex)"
|
|
|
+ v-if="edit && rowIndex != 0"
|
|
|
+ ></i>
|
|
|
+ <i
|
|
|
+ class="el-icon-circle-plus-outline addRow"
|
|
|
+ style="display: none"
|
|
|
+ @click="addRow(rowIndex, index)"
|
|
|
+ v-if="edit && rowIndex != 0 && index != columns.length - 1"
|
|
|
+ ></i>
|
|
|
+ <textarea
|
|
|
+ v-if="item.rowspan > 1"
|
|
|
+ v-model="item.value"
|
|
|
+ class="templateInput"
|
|
|
+ :id="item.id"
|
|
|
+ :readonly="item.readonly == 2 || !edit"
|
|
|
+ @click="inputClick(item)"
|
|
|
+ @input="calculation"
|
|
|
+ autocomplete="off"
|
|
|
+ style="resize: none"
|
|
|
+ ></textarea>
|
|
|
+ <input
|
|
|
+ v-else
|
|
|
+ v-model="item.value"
|
|
|
+ class="templateInput"
|
|
|
+ :id="item.id"
|
|
|
+ :readonly="item.readonly == 2 || !edit"
|
|
|
+ @click="inputClick(item, rowIndex == 0 ? 'columns' : null)"
|
|
|
+ @input="calculation"
|
|
|
+ type="text"
|
|
|
+ autocomplete="off"
|
|
|
+ />
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ </template>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
</div>
|
|
|
</template>
|
|
|
|
|
|
@@ -76,52 +93,126 @@
|
|
|
domId: '',
|
|
|
rightClickShow: false,
|
|
|
columns: [],
|
|
|
- rows: [],
|
|
|
+ // rows: [],
|
|
|
equation: {}
|
|
|
};
|
|
|
},
|
|
|
created() {},
|
|
|
methods: {
|
|
|
// 方法:添加新列
|
|
|
- addColumn() {
|
|
|
- this.columns.push({
|
|
|
- value: `列${this.columns.length + 1}`,
|
|
|
- id: generateRandomString(5),
|
|
|
- readonly: 1
|
|
|
- });
|
|
|
- this.rows.forEach((row) => {
|
|
|
- row.cells.push({
|
|
|
- value: '',
|
|
|
- readonly: 1,
|
|
|
- id: generateRandomString(5)
|
|
|
- });
|
|
|
- });
|
|
|
+ addColumn(index) {
|
|
|
+ let length = this.columns[0]?.length || 1;
|
|
|
+ if (index === 0 || index) {
|
|
|
+ this.columns.splice(
|
|
|
+ index + 1,
|
|
|
+ 0,
|
|
|
+ Array(length)
|
|
|
+ .fill(null)
|
|
|
+ .map(() => ({
|
|
|
+ readonly: 1,
|
|
|
+ value: '',
|
|
|
+ rowspan: 1,
|
|
|
+ id: generateRandomString(5)
|
|
|
+ }))
|
|
|
+ );
|
|
|
+ } else {
|
|
|
+ this.columns.push(
|
|
|
+ Array(length)
|
|
|
+ .fill(null)
|
|
|
+ .map(() => ({
|
|
|
+ readonly: 1,
|
|
|
+ value: '',
|
|
|
+ rowspan: 1,
|
|
|
+ id: generateRandomString(5)
|
|
|
+ }))
|
|
|
+ );
|
|
|
+ }
|
|
|
},
|
|
|
|
|
|
// 方法:删除指定列
|
|
|
removeColumn(index) {
|
|
|
this.columns.splice(index, 1);
|
|
|
- this.rows.forEach((row) => {
|
|
|
- row.cells.splice(index, 1);
|
|
|
- });
|
|
|
},
|
|
|
|
|
|
// 方法:添加新行
|
|
|
- addRow() {
|
|
|
- const newRow = {
|
|
|
- cells: Array(this.columns.length)
|
|
|
- .fill(null)
|
|
|
- .map(() => ({
|
|
|
+ addRow(rowIndex, columnIndex) {
|
|
|
+ if (columnIndex > 0) {
|
|
|
+ this.columns.forEach((item, newColumnIndex) => {
|
|
|
+ let newRow = {
|
|
|
readonly: 1,
|
|
|
value: '',
|
|
|
+ rowspan: 1,
|
|
|
id: generateRandomString(5)
|
|
|
- }))
|
|
|
- };
|
|
|
- this.rows.push(newRow);
|
|
|
+ };
|
|
|
+ if (newColumnIndex < columnIndex) {
|
|
|
+ newRow.rowspan = 0;
|
|
|
+ let _rowIndex = null;
|
|
|
+ if (item[rowIndex].rowspan == 0) {
|
|
|
+ _rowIndex = this.getPreventRowspan(newColumnIndex, rowIndex);
|
|
|
+ }
|
|
|
+ console.log(_rowIndex, '_rowIndex');
|
|
|
+ let rowspan =
|
|
|
+ this.columns[newColumnIndex][_rowIndex || rowIndex].rowspan ||
|
|
|
+ 1;
|
|
|
+
|
|
|
+ this.$set(
|
|
|
+ this.columns[newColumnIndex][_rowIndex || rowIndex],
|
|
|
+ 'rowspan',
|
|
|
+ (rowspan += 1)
|
|
|
+ );
|
|
|
+ }
|
|
|
+ this.columns[newColumnIndex].splice(rowIndex + 1, 0, newRow);
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ this.columns.forEach((item, index) => {
|
|
|
+ let _rowIndex = rowIndex;
|
|
|
+ if (item[rowIndex].rowspan > 1) {
|
|
|
+ _rowIndex += Number(item[rowIndex].rowspan);
|
|
|
+ }
|
|
|
+ this.columns[index].splice(_rowIndex + 1, 0, {
|
|
|
+ readonly: 1,
|
|
|
+ value: '',
|
|
|
+ rowspan: 1,
|
|
|
+ id: generateRandomString(5)
|
|
|
+ });
|
|
|
+ });
|
|
|
+ console.log(this.columns, 'this.columns');
|
|
|
+ }
|
|
|
+ },
|
|
|
+ //找到真正需要改变rowspan的行
|
|
|
+ getPreventRowspan(columnIndex, rowIndex) {
|
|
|
+ let preventRowspan = null;
|
|
|
+ this.columns[columnIndex].forEach((item, newRowIndex) => {
|
|
|
+ if (newRowIndex < rowIndex && item.rowspan > 1) {
|
|
|
+ preventRowspan = newRowIndex;
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ return preventRowspan;
|
|
|
},
|
|
|
// 方法:删除指定行
|
|
|
- removeRow(index) {
|
|
|
- this.rows.splice(index, 1);
|
|
|
+ removeRow(rowIndex) {
|
|
|
+ this.columns.forEach((item, columnIndex) => {
|
|
|
+ if (item[rowIndex].rowspan == 0) {
|
|
|
+ let preventRowspanIndex = this.getPreventRowspan(
|
|
|
+ columnIndex,
|
|
|
+ rowIndex
|
|
|
+ );
|
|
|
+ if (preventRowspanIndex) {
|
|
|
+ this.$set(
|
|
|
+ this.columns[columnIndex][preventRowspanIndex],
|
|
|
+ 'rowspan',
|
|
|
+ this.columns[columnIndex][preventRowspanIndex].rowspan - 1
|
|
|
+ );
|
|
|
+ }
|
|
|
+ } else if (item[rowIndex].rowspan > 1) {
|
|
|
+ let data = item[rowIndex];
|
|
|
+ data.rowspan--;
|
|
|
+ this.$set(this.columns[columnIndex], [rowIndex + 1], data);
|
|
|
+ }
|
|
|
+
|
|
|
+ item.splice(rowIndex, 1);
|
|
|
+ });
|
|
|
},
|
|
|
|
|
|
objInit() {
|
|
|
@@ -136,13 +227,12 @@
|
|
|
},
|
|
|
calculation() {
|
|
|
this.$emit('calculation');
|
|
|
-
|
|
|
},
|
|
|
equationValue({ domId, value }) {
|
|
|
- this.rows.forEach((item, index) => {
|
|
|
- let cellIndex = item.cells.findIndex((cell) => cell.id == domId);
|
|
|
+ this.columns.forEach((item, index) => {
|
|
|
+ let cellIndex = item.findIndex((cell) => cell.id == domId);
|
|
|
if (cellIndex != '-1') {
|
|
|
- this.$set(this.rows[index].cells[cellIndex], 'value', value);
|
|
|
+ this.$set(this.columns[index][cellIndex], 'value', value);
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
@@ -151,35 +241,26 @@
|
|
|
form: null,
|
|
|
equation: this.equation,
|
|
|
valueObj: {
|
|
|
- columns: this.columns,
|
|
|
- rows: this.rows
|
|
|
+ columns: this.columns
|
|
|
}
|
|
|
};
|
|
|
},
|
|
|
init({ form, valueObj, equation }) {
|
|
|
this.form = form;
|
|
|
this.columns = valueObj.columns;
|
|
|
- this.rows = valueObj.rows;
|
|
|
+
|
|
|
this.equation = equation || {};
|
|
|
},
|
|
|
editInputChange(domObj) {
|
|
|
if (domObj.equation) {
|
|
|
this.equation[this.domId] = domObj.equation;
|
|
|
}
|
|
|
- let columnsIndex = this.columns.findIndex(
|
|
|
- (item) => item.id == this.domId
|
|
|
- );
|
|
|
- if (columnsIndex >= 0) {
|
|
|
- this.$set(this.columns, columnsIndex, domObj);
|
|
|
- return;
|
|
|
- }
|
|
|
- this.rows.forEach((item, index) => {
|
|
|
- let rowsIndex = item.cells.findIndex(
|
|
|
- (cells) => cells.id == this.domId
|
|
|
- );
|
|
|
+
|
|
|
+ this.columns.forEach((item, index) => {
|
|
|
+ let rowsIndex = item.findIndex((cells) => cells.id == this.domId);
|
|
|
|
|
|
if (rowsIndex >= 0) {
|
|
|
- this.$set(this.rows[index].cells, rowsIndex, domObj);
|
|
|
+ this.$set(this.columns[index], rowsIndex, domObj);
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
@@ -199,6 +280,7 @@
|
|
|
id: item.id,
|
|
|
readonly: item.readonly,
|
|
|
value: item.value,
|
|
|
+ rowspan: item.rowspan,
|
|
|
equation: this.equation[item.id]
|
|
|
}
|
|
|
});
|
|
|
@@ -215,20 +297,51 @@
|
|
|
text-align: center;
|
|
|
background-color: #fff;
|
|
|
}
|
|
|
- table {
|
|
|
- width: 100%;
|
|
|
- border-collapse: collapse;
|
|
|
- }
|
|
|
- th,
|
|
|
- td {
|
|
|
- border: 1px solid #ddd;
|
|
|
- padding: 0;
|
|
|
- text-align: center;
|
|
|
- height: 30px;
|
|
|
+
|
|
|
+ .table-header {
|
|
|
+ span {
|
|
|
+ display: inline-block;
|
|
|
+ height: 30px;
|
|
|
+ border: 1px solid #ddd;
|
|
|
+ width: 100px;
|
|
|
+ background-color: #f2f2f2 !important;
|
|
|
+ }
|
|
|
+
|
|
|
+ input {
|
|
|
+ background-color: #f2f2f2 !important;
|
|
|
+ }
|
|
|
}
|
|
|
- th {
|
|
|
- background-color: #f2f2f2;
|
|
|
+ .table-body {
|
|
|
+ white-space: nowrap;
|
|
|
+ .column {
|
|
|
+ display: inline-block;
|
|
|
+ width: 100px;
|
|
|
+ > div {
|
|
|
+ height: 30px;
|
|
|
+ border: 1px solid #ddd;
|
|
|
+ width: 100%;
|
|
|
+ }
|
|
|
+ > div:nth-of-type(1) {
|
|
|
+ background-color: #f2f2f2 !important;
|
|
|
+ > input {
|
|
|
+ background-color: #f2f2f2 !important;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
+ // th,
|
|
|
+ // td {
|
|
|
+ // border: 1px solid #ddd;
|
|
|
+ // padding: 2px;
|
|
|
+ // text-align: center;
|
|
|
+ // height: 30px;
|
|
|
+ // }
|
|
|
+ // th {
|
|
|
+ // background-color: #f2f2f2;
|
|
|
+ // > input {
|
|
|
+ // background-color: #f2f2f2 !important;
|
|
|
+ // }
|
|
|
+ // }
|
|
|
tr:nth-child(even) {
|
|
|
background-color: #f9f9f9;
|
|
|
}
|
|
|
@@ -236,8 +349,17 @@
|
|
|
border-color: #66afe9; /* 改变边框颜色 */
|
|
|
outline: none; /* 移除默认的轮廓线 */
|
|
|
}
|
|
|
- .tableTh:hover {
|
|
|
+ .tableTd {
|
|
|
position: relative;
|
|
|
+ }
|
|
|
+ .deleteRow {
|
|
|
+ display: block !important;
|
|
|
+ position: absolute;
|
|
|
+ bottom: 0px;
|
|
|
+ right: -15px;
|
|
|
+ color: #f56c6c;
|
|
|
+ }
|
|
|
+ .tableTd:hover {
|
|
|
.delete {
|
|
|
display: block !important;
|
|
|
position: absolute;
|
|
|
@@ -245,5 +367,20 @@
|
|
|
top: 0;
|
|
|
color: #f56c6c;
|
|
|
}
|
|
|
+
|
|
|
+ .add {
|
|
|
+ display: block !important;
|
|
|
+ position: absolute;
|
|
|
+ right: 20px;
|
|
|
+ top: 0;
|
|
|
+ color: #409eff;
|
|
|
+ }
|
|
|
+ .addRow {
|
|
|
+ display: block !important;
|
|
|
+ position: absolute;
|
|
|
+ bottom: 0px;
|
|
|
+ right: 10px;
|
|
|
+ color: #409eff;
|
|
|
+ }
|
|
|
}
|
|
|
</style>
|