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