feat: update table insert

This commit is contained in:
hlwwlh 2020-07-12 12:51:06 +00:00
parent 67c38d8127
commit b563450e46

View File

@ -1,13 +1,48 @@
<template> <template>
<el-dialog title="插入表格" class="insert__dialog" :visible="dialogFormVisible" @close="$emit('close')"> <el-dialog
<el-form class="insert__form" :model="config.form"> title="插入表格"
<el-form-item label="行数(表头不计入行数)"> class="insert__dialog"
<el-input v-model="config.form.rows"></el-input> :visible="dialogFormVisible"
</el-form-item> @close="$emit('close')"
<el-form-item label="列数"> border
<el-input v-model="config.form.cols"></el-input> >
</el-form-item> <el-row class="tb-options" type="flex" align="middle" :gutter="10">
</el-form> <el-col :span="6">
<el-input-number
v-model="rowNum"
controls-position="right"
@change="handleChange($event,'row')"
:min="1"
:max="100"
size="small"
></el-input-number>
</el-col>
<el-col :span="6">
<el-input-number
v-model="colNum"
controls-position="right"
@change="handleChange($event,'col')"
:min="1"
:max="100"
size="small"
></el-input-number>
</el-col>
</el-row>
<!-- -->
<table style="border-collapse: collapse">
<tr :class="{ 'head-style': row===1 }" v-for="row in rowNum+1" :key="row">
<td v-for="col in colNum" :key="col">
<el-input
align="center"
v-model="tableData[`k_${row-1}_${col-1}`]"
:placeholder="row===1?'表头':''"
/>
</td>
</tr>
</table>
<!-- -->
<div slot="footer" class="dialog-footer"> <div slot="footer" class="dialog-footer">
<el-button :type="btnType" plain @click="$emit('close')"> </el-button> <el-button :type="btnType" plain @click="$emit('close')"> </el-button>
<el-button :type="btnType" @click="insertTable" plain> </el-button> <el-button :type="btnType" @click="insertTable" plain> </el-button>
@ -16,8 +51,8 @@
</template> </template>
<script> <script>
import config from '../../assets/scripts/config' import config from "../../assets/scripts/config";
import {mapState, mapMutations} from 'vuex'; import { mapState, mapMutations } from "vuex";
export default { export default {
props: { props: {
dialogFormVisible: { dialogFormVisible: {
@ -27,49 +62,67 @@ export default {
}, },
data() { data() {
return { return {
config: config config: config,
} rowNum: 1,
colNum: 1,
tableData: {}
};
}, },
computed: { computed: {
btnType() { btnType() {
return !this.nightMode ? 'success' : 'default'; return !this.nightMode ? "success" : "default";
}, },
...mapState({ ...mapState({
nightMode: state=> state.nightMode, nightMode: state => state.nightMode,
editor: state=> state.editor editor: state => state.editor
}) })
}, },
methods: { methods: {
// //
insertTable() { insertTable() {
const cursor = this.editor.getCursor() const cursor = this.editor.getCursor();
const rows = parseInt(this.config.form.rows) const rows = this.rowNum;
const cols = parseInt(this.config.form.cols) const cols = this.colNum;
if (isNaN(rows) || isNaN(cols) || rows < 1 || cols < 1) {
this.$message({
showClose: true,
message: '输入的行/列数无效,请重新输入',
type: 'error'
})
return
}
let table = '' let table = "";
let currRow = [];
for (let i = 0; i < rows + 2; ++i) { for (let i = 0; i < rows + 2; ++i) {
for (let j = 0; j < cols + 1; ++j) { table += "|";
table += (j === 0 ? '|' : (i !== 1 ? ' |' : ' --- |')) currRow = [];
} for (let j = 0; j < cols; ++j) {
table += '\n' let rowIdx = i;
if (i > 1) {
rowIdx = i - 1;
} }
this.editor.replaceSelection(`\n${table}\n`, cursor) i === 1
this.$emit('close') ? currRow.push("---")
this.editorRefresh() : currRow.push(
}, this.tableData[`k_${rowIdx}_${j}`]
...mapMutations(['editorRefresh']) ? this.tableData[`k_${rowIdx}_${j}`]
: ""
);
} }
} table += currRow.join("|");
table += "|\n";
}
this.editor.replaceSelection(`\n${table}\n`, cursor);
this.$emit("close");
this.editorRefresh();
},
...mapMutations(["editorRefresh"]),
handleChange(val, type) {}
}
};
</script> </script>
<style lang="less" scoped> <style lang="less">
.tb-options {
margin-bottom: 20px;
}
.head-style .el-input__inner {
background-color: #f2f2f2;
}
</style> </style>