• 封装一个Element-ui生成一个可行内编辑的表格(vue2项目)


    这个封装的是一个供整个项目使用的表格,可多次复用.放在一个全局使用的公共组件文件下.

    大致功能介绍,封装自定义指令,点击获得焦点,显示输入框,失去焦点显示文本内容,类型是字典决定类型,图片可以显示图片名还是上传图片

     

    子组件

    1. <template>
    2. <div>
    3. <el-table :data="tableData" border style="width: 100%">
    4. <el-table-column
    5. :prop="field.prop"
    6. :label="field.label"
    7. :min-width="field.minWidth || 140"
    8. v-for="(field, index) in fields"
    9. :key="index"
    10. show-overflow-tooltip
    11. >
    12. <template slot-scope="scope">
    13. <div v-if="field.slot">
    14. <slot :name="field.slot" :row="scope.row" />
    15. div>
    16. <div v-else>
    17. <div>
    18. <el-input
    19. v-if="field.iseditor && scope.row.isnameSelected"
    20. v-model="scope.row[field.prop]"
    21. @focus="cellClick(field, scope.row)"
    22. @blur="blurEvent(field, scope.row)"
    23. v-focus
    24. >
    25. el-input>
    26. <p @click="cellClick(field, scope.row)" v-else>
    27. {{ scope.row[field.prop] || "--" }}
    28. p>
    29. div>
    30. div>
    31. template>
    32. el-table-column>
    33. el-table>
    34. div>
    35. template>
    36. <style scoped>
    37. style>
    38. <style>
    39. .el-tooltip__popper {
    40. max-width: 1000px;
    41. }
    42. .disabled .el-upload--picture-card {
    43. display: none;
    44. }
    45. .avatar-uploader .el-upload {
    46. border: 1px dashed #d9d9d9;
    47. border-radius: 6px;
    48. cursor: pointer;
    49. position: relative;
    50. overflow: hidden;
    51. }
    52. .avatar-uploader .el-upload:hover {
    53. border-color: #409eff;
    54. }
    55. .avatar-uploader-icon {
    56. font-size: 28px;
    57. color: #8c939d;
    58. width: 178px;
    59. height: 178px;
    60. line-height: 178px;
    61. text-align: center;
    62. }
    63. .avatar {
    64. width: 178px;
    65. height: 178px;
    66. display: block;
    67. }
    68. style>

    父组件

    这里还有封装了一个文件上传的组件嵌套在表格中,算是拓展

    1. <template>
    2. <div>
    3. <el-button @click="hAdd" type="primary">添加el-button>
    4. <editComponents :fields="fields" :tableData="tableData">
    5. <template #action="{ row }">
    6. <el-button type="text" size="small" @click="del(row)">删除el-button>
    7. template>
    8. <template #paragraphImage="{ row }">
    9. <div>
    10. <p>{{ row.paragraphImage || "--" }}p>
    11. <el-upload
    12. ref="upload"
    13. :file-list="fileList1"
    14. :action="upLoadUrl"
    15. list-type="picture-card"
    16. :on-success="(e) => handleSuccess(e, row)"
    17. :on-error="handleError"
    18. >
    19. <i slot="default" class="el-icon-plus">i>
    20. <div slot="file" slot-scope="{ file }">
    21. <img
    22. class="el-upload-list__item-thumbnail"
    23. :src="file.url"
    24. alt=""
    25. />
    26. <span class="el-upload-list__item-actions">
    27. <span
    28. class="el-upload-list__item-preview"
    29. @click="handlePictureCardPreview(file)"
    30. >
    31. <i class="el-icon-zoom-in">i>
    32. span>
    33. <span
    34. v-if="!disabled"
    35. class="el-upload-list__item-delete"
    36. @click="handleRemove(file)"
    37. >
    38. <i class="el-icon-delete">i>
    39. span>
    40. span>
    41. div>
    42. el-upload>
    43. <el-dialog :visible.sync="dialogVisible">
    44. <img width="100%" :src="dialogImageUrl" alt="" />
    45. el-dialog>
    46. div>
    47. template>
    48. <template #paragraphType="{ row }">
    49. <div>
    50. <el-select v-model="row.paragraphType" placeholder="请选择">
    51. <el-option label="选择一" value="1">el-option>
    52. <el-option label="选择二" value="2">el-option>
    53. el-select>
    54. div>
    55. template>
    56. editComponents>
    57. <el-card>
    58. <div>
    59. <el-button type="primary" round @click="onSubmit">提交el-button>
    60. div>
    61. el-card>
    62. div>
    63. template>
    64. <style scoped>
    65. style>

  • 相关阅读:
    Azure 基础
    Cesium深入浅出之自定义材质
    公司日常业务开发中,我是如何基于antd定制属于自己的主题
    04 后端增删改查【小白入门SpringBoot + Vue3】
    SCSS的基本使用(一)
    Lambda 表达式
    软件设计23种设计模式
    SELECT后面有自定义函数的优化方法
    Games104现代游戏引擎入门-lecture14游戏引擎的引擎工具高级概念与应用
    【云原生 | Kubernetes 系列】--Gitops持续交付 ArgoCD 部署与概念
  • 原文地址:https://blog.csdn.net/weixin_53818172/article/details/133892992