用element-ui的表格和表单结合写一个可编辑表格

约定不等于承诺〃 2022-12-20 02:58 296阅读 0赞

实现效果如下图:

在这里插入图片描述
在前面写了一个 vxe-table的插件,一个基于vue功能强大的表格组件(支持树形,编辑,增删改查以及校验等),也帮到了很多人解决工作中的问题,挺开心的;都说条条大路通罗马,同样的功能有很多实现方式,今天我们就一起看一下利用element-ui的表格和表单结合实现的可编辑表格,主要是记录一下自己用到的知识点,如果可以帮到你,我也很荣幸!

第一步:项目引进element

略,既然看到这里了,想必项目已经引入element-ui,也会怎么做,如果不会可以看 element-ui 官网

第二步:table.vue(可编辑表格)

  1. <template>
  2. <div>
  3. <el-form :model="config" ref="configTableForm">
  4. <el-table
  5. header-row-class-name="header_row_style"
  6. :data="config.list"
  7. style="width: 100%"
  8. max-height="450">
  9. <el-table-column
  10. label="名称"
  11. prop="code"
  12. show-overflow-tooltip
  13. width="80">
  14. </el-table-column>
  15. <el-table-column >
  16. <template slot-scope="scope">
  17. <el-form-item
  18. :prop="'list.'+scope.$index+'.value'">
  19. <el-input
  20. :disabled="scope.row.showType == 3 || scope.row.showType == 4? true : false"
  21. v-model.trim="scope.row.value"
  22. size="mini"
  23. placeholder="请填写值"></el-input>
  24. </el-form-item>
  25. </template>
  26. </el-table-column>
  27. <el-table-column
  28. label="字段类型"
  29. width="120"
  30. show-overflow-tooltip>
  31. <template slot-scope="scope">
  32. { { scope.row.type}}
  33. </template>
  34. </el-table-column>
  35. <el-table-column label="查询名">
  36. <template slot-scope="scope">
  37. <el-form-item
  38. :prop="'list.'+scope.$index+'.name'"
  39. :rules="[
  40. { required: scope.row.input, message: '不能为空', trigger: 'blur' }]">
  41. <el-input
  42. :disabled="scope.row.input ? false : true"
  43. placeholder="请输入查询名"
  44. v-model.trim="scope.row.name"
  45. :maxlength="20"
  46. size="mini"></el-input>
  47. </el-form-item>
  48. </template>
  49. </el-table-column>
  50. <el-table-column label="长度" width="100">
  51. <template slot-scope="scope">
  52. <el-input-number
  53. :min="0"
  54. v-model="scope.row.length"
  55. size="mini"
  56. controls-position="right"></el-input-number>
  57. </template>
  58. </el-table-column>
  59. <el-table-column label="描述">
  60. <template slot-scope="scope">
  61. <el-input
  62. placeholder="请输入描述"
  63. v-model.trim="scope.row.memo"
  64. size="mini"></el-input>
  65. </template>
  66. </el-table-column>
  67. <el-table-column label="是否排序" width="100">
  68. <template slot-scope="scope">
  69. <el-select
  70. size="mini"
  71. v-model="scope.row.sort"
  72. @change="showModeChange(scope.row.sort, scope.$index)">
  73. <el-option label="无" value="0"></el-option>
  74. <el-option label="升序" value="1"></el-option>
  75. <el-option label="降序" value="2"></el-option>
  76. </el-select>
  77. </template>
  78. </el-table-column>
  79. <el-table-column label="输入" width="50">
  80. <template slot-scope="scope">
  81. <el-checkbox
  82. v-model="scope.row.input"
  83. ></el-checkbox>
  84. </template>
  85. </el-table-column>
  86. <el-table-column width="80">
  87. <template slot="header" slot-scope="scope">
  88. <el-checkbox v-model="AallOutput" @change="handleAllOutput"></el-checkbox>
  89. <span>输出</span>
  90. </template>
  91. <template slot-scope="scope">
  92. <el-checkbox
  93. v-model="scope.row.output"
  94. ></el-checkbox>
  95. </template>
  96. </el-table-column>
  97. <el-table-column label="是否必填" width="80">
  98. <template slot-scope="scope">
  99. <el-checkbox
  100. v-model="scope.row.isMust"
  101. :disabled="!scope.row.input"></el-checkbox>
  102. </template>
  103. </el-table-column>
  104. <el-table-column label="展示类型" width="100">
  105. <template slot-scope="scope">
  106. <el-select
  107. size="mini"
  108. :disabled="scope.row.input ? false : true"
  109. v-model="scope.row.showType"
  110. @change="showModeChange(scope.row.showType, scope.$index)">
  111. <el-option label="文本" value="1"></el-option>
  112. <el-option label="数字" value="2"></el-option>
  113. <el-option label="日期" value="3"></el-option>
  114. <el-option label="下拉" value="4" disabled></el-option>
  115. </el-select>
  116. </template>
  117. </el-table-column>
  118. <el-table-column label="查询方式" width="100">
  119. <template slot-scope="scope">
  120. <el-select
  121. size="mini"
  122. :disabled="scope.row.input ? false : true"
  123. v-model="scope.row.queryType">
  124. <el-option
  125. label="模糊"
  126. value="1"
  127. :disabled="scope.row.displayType == 3 || scope.row.displayType == 4 ? true : false"></el-option>
  128. <el-option
  129. label="精准"
  130. value="2"></el-option>
  131. <el-option
  132. label="区间"
  133. value="3"
  134. :disabled="scope.row.displayType == 1 || scope.row.displayType == 4 ? true : false"></el-option>
  135. </el-select>
  136. </template>
  137. </el-table-column>
  138. </el-table>
  139. </el-form>
  140. <div class="btns">
  141. <el-button
  142. type="primary"
  143. @click="submitConfig('configTableForm')">保存并确定</el-button>
  144. </div>
  145. </div>
  146. </template>
  147. <script>
  148. export default {
  149. data(){
  150. return{
  151. // 配置列表
  152. config: {
  153. list: []
  154. },
  155. }
  156. },
  157. mounted(){
  158. this.getAllColumns()
  159. },
  160. methods:{
  161. // 获取表格数据进行渲染
  162. getAllColumns() {
  163. var data = this.$route.query
  164. getTableDetail(data).then((res) => {
  165. if (res.data.infoCode == 200) {
  166. this.configData = new Array();
  167. for (let i = 0; i < res.data.data.length; i++) {
  168. this.configData.push({
  169. value: res.data.data[i].value || '',
  170. sort:res.data.data[i].sort || '0',
  171. input: res.data.data[i].input || false,
  172. output: res.data.data[i].output || false,
  173. isMust: res.data.data[i].isMust || false,
  174. showType: res.data.data[i].showType || '1',
  175. queryType: res.data.data[i].queryType || '1',
  176. length: res.data.data[i].length,
  177. code: res.data.data[i].columnName,
  178. type: res.data.data[i].udtName || '',
  179. id: res.data.data[i].id,
  180. name: res.data.data[i].name,
  181. memo: res.data.data[i].memo,
  182. apiId: res.data.data[i].apiId,
  183. relName:res.data.data[i].relName || '',
  184. tableSchema:res.data.data[i].tableSchema,
  185. tableName:res.data.data[i].tableName,
  186. });
  187. }
  188. this.$set(this.config, 'list', this.configData);
  189. this.$nextTick(function() {
  190. this.$refs['configTableForm'].clearValidate();
  191. })
  192. } else {
  193. this.$set(this.config, 'list', new Array());
  194. }
  195. }).catch(() => { })
  196. },
  197. //修改表格后提交
  198. submitConfig(formName) {
  199. this.config.list.forEach(e => {
  200. e.apiId = this.id
  201. })
  202. this.$refs[formName].validate((valid) => {
  203. if (valid) {
  204. saveApiColumns(this.config.list).then((res) => {
  205. this.configLoading = false;
  206. if (res.data.infoCode == 200) {
  207. this.$message({
  208. type: 'success',
  209. message: '配置成功'
  210. });
  211. this.$router.back(-1)
  212. } else {
  213. this.$message({
  214. type: 'error',
  215. message: '配置失败!'+res.data.info
  216. });
  217. this.getAllColumns()
  218. }
  219. }).catch(() => {
  220. })
  221. } else {
  222. return false;
  223. }
  224. });
  225. },
  226. }
  227. }
  228. </script>

有点懒,就把自己项目的代码粘贴了一下,可以作为参考!

发表评论

表情:
评论列表 (有 0 条评论,296人围观)

还没有评论,来说两句吧...

相关阅读