index.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <template>
  2. <div class="header-separate">
  3. <div class="header-separate-options">
  4. <div>
  5. 表头英文位置:
  6. <el-radio
  7. v-model="curQue.headerEnglishPosition"
  8. label="top"
  9. >上</el-radio>
  10. <el-radio
  11. v-model="curQue.headerEnglishPosition"
  12. label="right"
  13. >右</el-radio>
  14. <el-radio
  15. v-model="curQue.headerEnglishPosition"
  16. label="bottom"
  17. >下</el-radio>
  18. <el-radio
  19. v-model="curQue.headerEnglishPosition"
  20. label="left"
  21. >左</el-radio>
  22. </div>
  23. <div>
  24. 单元格拼音位置:
  25. <el-radio v-model="curQue.pinyinPosition" label="top">上</el-radio>
  26. <el-radio v-model="curQue.pinyinPosition" label="right">右</el-radio>
  27. <el-radio v-model="curQue.pinyinPosition" label="bottom">下</el-radio>
  28. <el-radio v-model="curQue.pinyinPosition" label="left">左</el-radio>
  29. </div>
  30. <div>
  31. 首列对齐方式:
  32. <el-radio v-model="curQue.firstColAligin" label="center">居中</el-radio>
  33. <el-radio v-model="curQue.firstColAligin" label="follow">跟随内容</el-radio>
  34. </div>
  35. <div>
  36. 内容对齐方式:
  37. <el-radio v-model="curQue.textAlign" label="left">左对齐</el-radio>
  38. <el-radio v-model="curQue.textAlign" label="center">居中</el-radio>
  39. <el-radio v-model="curQue.textAlign" label="right">右对齐</el-radio>
  40. </div>
  41. <div>
  42. <el-button @click="addCol">增加一列</el-button>
  43. <el-button @click="addRow">增加一行</el-button>
  44. </div>
  45. </div>
  46. <div class="header-separate-preview">
  47. <table class="preview-table">
  48. <thead>
  49. <tr>
  50. <th v-for="(num, i) in curQue.tableData.headers" :key="`th-${i}`">
  51. <el-input v-model="num.text">
  52. <template slot="prepend">文本</template>
  53. </el-input>
  54. <el-input v-model="num.english">
  55. <template slot="prepend">英文</template>
  56. </el-input>
  57. </th>
  58. </tr>
  59. </thead>
  60. <tbody>
  61. <tr v-for="(row, i) in curQue.tableData.body" :key="`tr-${i}`">
  62. <td v-for="(col, j) in row.content" :key="`td-${j}`">
  63. <el-button @click="edit(i, j)">编辑</el-button>
  64. </td>
  65. <td>
  66. <el-button size="mini" @click="deleteRow(i)">删除行</el-button>
  67. </td>
  68. </tr>
  69. <tr>
  70. <td
  71. v-for="(width, i) in curQue.tableData.colsConfig.width"
  72. :key="`width-${i}`"
  73. >
  74. <el-input v-model.number="width.val" size="mini">
  75. <template slot="prepend">宽度</template>
  76. </el-input>
  77. <el-button size="mini" @click="deleteCol(i)">删除列</el-button>
  78. </td>
  79. </tr>
  80. </tbody>
  81. </table>
  82. </div>
  83. <cell-edit
  84. :visible="visible"
  85. :body="curQue.tableData.body"
  86. :cur-index="curIndex"
  87. @close="close"
  88. />
  89. </div>
  90. </template>
  91. <script>
  92. import CellEdit from './components/CellEdit';
  93. export default {
  94. components: { CellEdit },
  95. props: {
  96. curQue: {
  97. type: Object,
  98. default: () => {
  99. return {
  100. isFirst: true,
  101. type: "header_separate",
  102. name: "表头分离表格",
  103. pinyinPosition: "top",
  104. headerEnglishPosition: "top",
  105. firstColAligin: "center",
  106. textAlign: "center",
  107. tableData: {
  108. headers: [
  109. {
  110. text: "",
  111. english: ""
  112. },
  113. {
  114. text: "",
  115. english: ""
  116. },
  117. {
  118. text: "",
  119. english: ""
  120. }
  121. ],
  122. body: [
  123. {
  124. content: [
  125. {
  126. type: "content",
  127. text: "",
  128. sentence_data: {
  129. type: "sentence_segword_chs",
  130. name: "句子分词",
  131. pyPosition: "top", // top 拼音在上面;bottom 拼音在下面
  132. sentence: "", // 句子
  133. segList: [], // 分词结果
  134. seg_words: "",
  135. wordsList: []
  136. }
  137. },
  138. {
  139. type: "content",
  140. text: "",
  141. sentence_data: {
  142. type: "sentence_segword_chs",
  143. name: "句子分词",
  144. pyPosition: "top", // top 拼音在上面;bottom 拼音在下面
  145. sentence: "", // 句子
  146. segList: [], // 分词结果
  147. seg_words: "",
  148. wordsList: []
  149. }
  150. },
  151. {
  152. type: "content",
  153. text: "",
  154. sentence_data: {
  155. type: "sentence_segword_chs",
  156. name: "句子分词",
  157. pyPosition: "top", // top 拼音在上面;bottom 拼音在下面
  158. sentence: "", // 句子
  159. segList: [], // 分词结果
  160. seg_words: "",
  161. wordsList: []
  162. }
  163. }
  164. ]
  165. }
  166. ],
  167. colsConfig: {
  168. width: [{ val: 100 }, { val: 100 }, { val: 100 }]
  169. }
  170. }
  171. };
  172. }
  173. },
  174. changeCurQue: {
  175. type: Function,
  176. required: true
  177. }
  178. },
  179. data() {
  180. return {
  181. visible: false,
  182. curIndex: {
  183. col: 0,
  184. row: 0
  185. }
  186. };
  187. },
  188. computed: {
  189. rows() {
  190. return this.curQue.tableData.body.length;
  191. },
  192. cols() {
  193. if (this.rows.length <= 0) return 0;
  194. return this.curQue.tableData.body[0].content.length;
  195. }
  196. },
  197. created() {
  198. if (this.curQue.isFirst) {
  199. this.curQue.isFirst = false;
  200. this.changeCurQue(this.curQue);
  201. }
  202. },
  203. methods: {
  204. addCol() {
  205. this.curQue.tableData.body.forEach(({ content }) => {
  206. content.push({
  207. type: "content",
  208. text: "",
  209. sentence_data: {
  210. type: "sentence_segword_chs",
  211. name: "句子分词",
  212. pyPosition: "top", // top 拼音在上面;bottom 拼音在下面
  213. sentence: "", // 句子
  214. segList: [], // 分词结果
  215. seg_words: "",
  216. wordsList: []
  217. }
  218. });
  219. });
  220. this.curQue.tableData.headers.push({
  221. text: "",
  222. english: ""
  223. });
  224. this.curQue.tableData.colsConfig.width.push({ val: 100 });
  225. },
  226. deleteCol(i) {
  227. this.$confirm("确定要删除此列吗?", "提示", {
  228. confirmButtonText: "确定",
  229. cancelButtonText: "取消",
  230. type: "warning",
  231. })
  232. .then(() => {
  233. if (this.cols <= 3) return this.$message.warning("必须留三列");
  234. this.curQue.tableData.body.forEach(({ content }) => {
  235. content.splice(i, 1);
  236. });
  237. this.curQue.tableData.headers.splice(i, 1);
  238. this.curQue.tableData.colsConfig.width.splice(i, 1);
  239. })
  240. },
  241. addRow() {
  242. let content = [];
  243. for (let i = 0; i < this.cols; i++) {
  244. content.push({
  245. type: "content",
  246. text: "",
  247. sentence_data: {
  248. type: "sentence_segword_chs",
  249. name: "句子分词",
  250. pyPosition: "top", // top 拼音在上面;bottom 拼音在下面
  251. sentence: "", // 句子
  252. segList: [], // 分词结果
  253. seg_words: "",
  254. wordsList: []
  255. }
  256. });
  257. }
  258. this.curQue.tableData.body.push({
  259. content
  260. });
  261. },
  262. deleteRow(i) {
  263. this.$confirm("确定要删除此行吗?", "提示", {
  264. confirmButtonText: "确定",
  265. cancelButtonText: "取消",
  266. type: "warning",
  267. })
  268. .then(() => {
  269. if (this.rows <= 1) return this.$message.warning("必须留一行");
  270. this.curQue.tableData.body.splice(i, 1);
  271. })
  272. },
  273. edit(i, j) {
  274. this.curIndex = {
  275. col: j,
  276. row: i
  277. };
  278. this.visible = true;
  279. },
  280. close() {
  281. this.visible = false;
  282. }
  283. }
  284. };
  285. </script>
  286. <style lang="scss" scoped>
  287. .header-separate {
  288. border: 1px solid #ccc;
  289. border-radius: 4px;
  290. padding: 8px 12px;
  291. &-options {
  292. > div {
  293. margin-bottom: 12px;
  294. }
  295. }
  296. &-preview {
  297. border-top: 1px solid #ccc;
  298. padding-top: 12px;
  299. .preview-table {
  300. border-collapse: collapse;
  301. td,
  302. th {
  303. padding: 8px;
  304. .el-input {
  305. max-width: 220px;
  306. }
  307. }
  308. td {
  309. border: 1px solid #aaa;
  310. }
  311. th {
  312. border: 1px solid #aaa;
  313. }
  314. }
  315. }
  316. }
  317. </style>