• vue之封装一个 table组件 ( 列表之中 渲染其他组件 )


    vue之封装一个 table组件 ( 列表之中 渲染其他组件 )

    listTable / index.vue

    <template>
      <div class="search-table">
        <!-- v-loading="listLoading" -->
        <el-table
          ref="searchTable"
          :data="tablelist"
          border
          stripe
          size="mini"
          @selection-change="handleSelectionChange"
          @sort-change="handleSortChange"
          @cell-click="handleCellClick"
        >
          <el-table-column v-if="isSelect" type="selection" width="55" />
          <el-table-column
            v-else
            label="序号"
            width="65px"
            type="index"
            :index="indexMethod"
            align="center"
          />
          <template v-for="item in columns">
            <el-table-column
              v-if="!item.hidden"
              :key="item.prop"
              :prop="item.prop"
              :label="item.label"
              :sortable="item.sortable"
              :width="item.width"
              :min-width="item.minWidth"
              :formatter="item.render"
              :render-header="item.renderHeader"
              :show-overflow-tooltip="!item.hiddenTooltip"
            >
              <template
                v-if="item.formatter && typeof item.formatter !== 'function'"
                v-slot:default="{ row, column, $index }"
              >
                <component
                  :is="item.formatter"
                  :key="row.id"
                  :table-data="tablelist"
                  :row="row"
                  :column="column"
                  :index="$index"
                  :col="item"
                  :cell-value="row[item.prop]"
                  :status="item.status"
                />
              </template>
              <template v-else-if="item.link" v-slot:default="{ row, column }">
                <span class="jump-color" @click="handleOperate(row, column)">{{
                  row[item.prop]
                }}</span>
              </template>
            </el-table-column>
          </template>
          <el-table-column v-if="isAction" label="操作" :width="operation[0].width ? operation[0].width : 120 ">
            <template v-slot:default="{ row, index }">
              <div v-for="(item, key) in operation" :key="key" class="opt-btn">
                <span
                  v-if="checkRenderStatus(row, item)"
                  class="operate-btn"
                  :disabled="checkDisabled(row, item)"
                  @click="handleOperate(item, index, row)"
                >
                  {{ item.label }}
                </span>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <!-- @size-change="handleSizeChange" :page-sizes="pagesizes"-->
        <el-pagination
          v-if="!hiddenPagination"
          class="table-pagination"
          layout="total, prev, pager, next"
          :total="total"
          :page-size="pagesize"
          :current-page="currentPage"
          @current-change="handlePageChange"
        />
      </div>
    </template>
    
    <script>
    // import { getTableList } from '@/api/common'
    // import { deleteAttr } from '@/utils/str'
    
    export default {
      name: 'SearchTable',
      props: {
        isInit: {
          type: [Boolean, String],
          default: false
        },
        searchForm: {
          type: Object,
          default: () => {}
        },
        defalutData: {
          type: Array,
          default: () => {
            return []
          }
        },
        url: {
          type: String,
          default: () => {
            return ''
          }
        },
        requestMethod: {
          type: String,
          default: () => {
            return 'GET'
          }
        },
        columns: {
          type: Array,
          default: () => {
            return []
          }
        },
        hiddenPagination: {
          type: Boolean,
          default: false
        },
        operation: {
          type: Array,
          default: () => {
            return []
          }
        },
        origin2formated: {
          type: Function,
          default(val) {
            return val
          }
        },
        isSelect: {
          type: Boolean,
          default: () => {
            return false
          }
        }
      },
      data() {
        return {
          tablelist: [],
          total: 0,
          // pagesizes: [15, 50, 100],
          pagesize: 20,
          currentPage: 1,
          // listLoading: false,
          currentOrderStr: null
        }
      },
      computed: {
        offset() {
          return (this.currentPage - 1) * this.pagesize
        },
        isAction() {
          return this.operation && this.operation.length > 0
        }
      },
      created() {
        if (this.isInit) return
        this.fetchData()
      },
      methods: {
        indexMethod(index) {
          return index + 1 + (this.currentPage - 1) * this.pagesize
        },
        resetPage() {
          this.currentPage = 1
        },
        async fetchData() {
          // this.listLoading = true
          const info = {}
          const params = {
            limit: this.pagesize,
            offset: this.offset,
            ordering: this.currentOrderStr,
            // ...this.$_.cloneDeep(this.searchForm)
            ...this.searchForm
          }
          console.log('params', params)
          this.tablelist = [
            {
              pathUrl: '/about11',
              errorType: 1,
              status: 1,
              txt_time: '2022-08-24 17:27:49'
            },
            {
              pathUrl: '/about12',
              errorType: 2,
              status: 0,
              txt_time: '2022-08-24 17:27:49'
            }
          ]
          // deleteAttr(params)
          // info = await getTableList(this.url, params, this.requestMethod).then(
          //   res => {
          //     const data = res.data
          //     if (res.code === 0) {
          //       if (data.results.length === 0 && this.defalutData.length > 0) {
          //         this.tablelist = this.defalutData
          //       } else {
          //         this.tablelist = []
          //         const results = this.origin2formated(data.results)
          //         this.tablelist = results
          //         this.total = data.count
          //       }
          //       this.listLoading = false
          //     }
          //     return data
          //   }
          // )
          return info
        },
        // handleSizeChange(val) {
        //   this.pagesize = val
        //   this.fetchData()
        // },
        handlePageChange(val) {
          this.currentPage = val
          this.fetchData()
        },
        handleOperate(item, index, row) {
          const operateInfo = {
            item: item,
            index: index,
            row: row
          }
          this.$emit('handleOperate', operateInfo)
        },
        handleSortChange({ column, prop, order }) {
          if (order === null) {
            this.currentOrderStr = null
          } else if (order === 'descending') {
            this.currentOrderStr = '-' + prop
          } else {
            this.currentOrderStr = prop
          }
          this.fetchData()
        },
        handleCellClick(row, column, cell, event) {
          const payload = { row, column, cell, event }
          this.$emit('handleCellClick', payload)
        },
        checkRenderStatus(row, operation) {
          if (operation.alwaysShow) {
            return true
          }
    
          const role = this.$store.state.user.role
          const accessedRoles = operation.accessedRoles || []
          if (accessedRoles.length > 0 && accessedRoles.indexOf(role) < 0) {
            return false
          }
          const defaultShowbyRow = {
            key: null,
            value: []
          }
          const showByRow = Object.assign(defaultShowbyRow, operation.showByRow)
          if (showByRow.key != null && showByRow.value.length !== 0) {
            const actionValue = row[showByRow.key]
            if (showByRow.value.indexOf(actionValue) < 0) {
              return false
            }
          }
          return true
        },
        checkDisabled(row, operation) {
          const showDisabled = operation.showDisabled
          if (showDisabled) {
            return row[showDisabled.key] !== showDisabled.value
          } else {
            return false
          }
        },
        handleSelectionChange(val) {
          this.$emit('handleOperate', val)
        },
        clearSelection() {
          this.$refs['searchTable'].clearSelection()
        }
      }
    }
    </script>
    
    <style lang="scss" scoped>
    .search-table {
      .table-pagination {
        margin-top: 10px;
        margin-bottom: 10px;
        text-align: center;
      }
      .opt-btn {
        display: inline;
        &:not(:first-child) {
          .el-button {
            margin-left: 5px;
          }
        }
      }
      .operate-btn {
        margin-right: 10px;
        font-size: 14px;
        color: rgba(64, 158, 255, 1);
        cursor: pointer;
      }
    }
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317

    table组件相关

    base.vue

    <template>
      <div />
    </template>
    
    <script>
    export default {
      name: 'BaseFormatter',
      props: {
        row: {
          type: Object,
          default: () => ({})
        },
        column: {
          type: Object,
          default: null
        },
        index: {
          type: Number,
          default: 0
        },
        col: {
          type: Object,
          default: () => ({})
        },
        cellValue: {
          type: [String, Boolean, Number, Object, Array],
          default: null
        }
      },
      data() {
        return {}
      }
    }
    </script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    DataFormatter.vue 时间格式化

    <template>
      <span>{{ iDateTime }}</span>
    </template>
    
    <script>
    import BaseFormatter from './base'
    export default {
      name: 'DataFormatter',
      extends: BaseFormatter,
      computed: {
        iDateTime() {
          let res = ''
          this.cellValue ? res = this.cellValue.substring(0, 10) : ''
          return res
        }
      }
    }
    </script>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    TagStatusFormatter.vue 状态格式化

    <template>
      <el-tag v-if="iType" :type="iType" effect="plain" size="small">{{ iLabel }}</el-tag>
    </template>
    
    <script>
    import BaseFormatter from './base.vue'
    export default {
      name: 'TagStatusFormatter',
      extends: BaseFormatter,
      props: {
        status: {
          type: Object,
          default: () => {}
        }
      },
      data() {
        return {}
      },
      computed: {
        iType() {
          return this.status[this.cellValue]?.type
        },
        iLabel() {
          return this.status[this.cellValue]?.label
        }
      }
    }
    </script>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    LinkTo.vue ( 跳转组件 )

    <template>
      <div class="assets-id-copy">
        <div class="router-link" @click="goto">
          {{ row.pathUrl }}
        </div>
      </div>
    </template>
    
    <script>
    import BaseFormatter from './base.vue'
    export default {
      name: 'LinkTo',
      extends: BaseFormatter,
      props: {
        row: {
          type: Object,
          default: () => {}
        }
      },
      data() {
        return {}
      },
      computed: {
        url() {
          const pathUrl = this.row.pathUrl
          return pathUrl
        }
      },
      methods: {
        goto() {
          this.$router.push(this.url)
          // 浏览器 打开一个新的窗口
          // const routeData = this.$router.resolve(this.url)
          // window.open(routeData.href, '_blank')
        }
      }
    }
    </script>
    <style lang="scss" scoped>
    .assets-id-copy {
      display: flex;
      .router-link {
        color: #0000ff;
        cursor: pointer;
        &:hover {
          text-decoration: none;
          color: #606266;
        }
      }
    }
    </style>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    使用组件

    ···js

    User2
    
    
    • 1

    ···

    效果

    在这里插入图片描述

  • 相关阅读:
    怎么写综述类论文? - 易智编译EaseEditing
    1312. 让字符串成为回文串的最少插入次数
    《数论概论》
    约瑟夫环递归算法详解与实现
    字典序问题
    [附源码]SSM计算机毕业设计基于SSM的酒店管理系统JAVA
    力扣刷题day42|121买卖股票的最佳时机、122买卖股票的最佳时机II
    2023香山杯re复现
    传输层TCP协议
    ArcgisForJS如何实现添加含图片样式的点要素?
  • 原文地址:https://blog.csdn.net/weixin_47409897/article/details/126628361