| | |
| | | editing: { type: Boolean, default: false }, |
| | | columns: { type: Array, default: () => [] }, |
| | | tableKey: { type: String, default: "" }, |
| | | // 需要自动连续编号的列 prop(如 seq)。新增行、删除行后都会按行序重排, |
| | | // 避免出现 1、3、4 这种断号。不传则不处理。 |
| | | autoSeq: { type: String, default: "" }, |
| | | }, |
| | | data() { |
| | | return { |
| | |
| | | methods: { |
| | | addRow() { |
| | | const row = {}; |
| | | this.columns.forEach(col => { |
| | | if (col.type === 'number') row[col.prop] = null; |
| | | else if (col.type === 'date') row[col.prop] = ''; |
| | | else row[col.prop] = ''; |
| | | this.columns.forEach((col) => { |
| | | // 列上配了 default 就优先用它(可以是值,也可以是 (index, rows) => value) |
| | | if (col.default !== undefined) { |
| | | row[col.prop] = |
| | | typeof col.default === "function" |
| | | ? col.default(this.localData.length, this.localData) |
| | | : col.default; |
| | | } else if (col.type === "number") { |
| | | row[col.prop] = null; |
| | | } else { |
| | | row[col.prop] = ""; |
| | | } |
| | | }); |
| | | this.localData.push(row); |
| | | this.renumber(); |
| | | }, |
| | | deleteRows() { |
| | | // remove by reference |
| | | this.localData = this.localData.filter(row => !this.selectedRows.includes(row)); |
| | | this.localData = this.localData.filter((row) => !this.selectedRows.includes(row)); |
| | | this.selectedRows = []; |
| | | this.renumber(); |
| | | this.$emit("update:data", this.localData); |
| | | }, |
| | | /** 按行序重写 autoSeq 列的序号(1、2、3...) */ |
| | | renumber() { |
| | | if (!this.autoSeq) return; |
| | | this.localData.forEach((row, i) => { |
| | | row[this.autoSeq] = String(i + 1); |
| | | }); |
| | | }, |
| | | onSelect(rows) { this.selectedRows = rows; }, |
| | | tagType(v) { |
| | | if (!v) return ''; |