index.vue 9.57 KB
<!-- 表格+分页 -->
<template>
  <div class="glsx-table">
    <el-table
      :ref="tableParams.mutiSelectName + 'MutiTable' || 'MutiTable'"
      :data="getListApi ? data : tableData"
      :row-key="tableParams.mutiSelectUniqueKey || ''"
      v-loading="loading"
      :border="border"
      :height="height"
      :highlight-current-row="true"
      :stripe="true"
      @sort-change="sortChange"
      @selection-change="handleSelect"
      :size="size"
      :header-cell-style="headerStyle"
    >
      <el-table-column
        v-if="tableParams.mutiSelectUniqueKey && tableParams.mutiSelectName"
        type="selection"
        width="55"
        :selectable="checkSelectable"
        :reserve-selection="true"
      >
      </el-table-column>
      <el-table-column
        v-if="hasNum"
        align="center"
        width="60px"
        label="序号"
        type='index'
        :index="index=>index+(page[this.pageConf.pageNum] - 1) * page[this.pageConf.pageSize] + 1"
      ></el-table-column>
      <el-table-column
        v-for="(item, index) in tableColumns"
        :key="index"
        :label="item.label"
        :prop="item.prop"
        :width="item.width"
        :min-width="item.minWidth"
        :align="item.align||'center'"
        :sortable="item.sortable"
      >
        <template slot-scope="scope">
          <slot
            v-if="item.addHtml"
            :name="item.addHtml"
            :scope="scope.row"
            :prop="item.prop"
            :width="item.width"
          ></slot>
          <template v-else-if="item.formatter">
            <span
              class="line-ellipsis"
              :style="{'-webkit-line-clamp': item.line ? item.line : ''}"
              :title="item.formatter(scope.row)"
              v-text="item.formatter(scope.row)"
            ></span>
          </template>
          <template v-else-if="getListApi">
            <span
              class="line-ellipsis"
              :style="{'-webkit-line-clamp': item.line ? item.line : ''}"
              :title="scope.row[item.prop]"
              v-text="scope.row[item.prop]"
            ></span>
          </template>
          <template v-else>
            <span
              class="line-ellipsis"
              :style="{'-webkit-line-clamp': item.line ? item.line : ''}"
              :title="tableData[scope.$index][item.prop]"
              v-text="tableData[scope.$index][item.prop]"
            ></span>
          </template>
          <template v-if="item.addSymbol">
            <span v-text="item.addSymbol"></span>
          </template>
        </template>
      </el-table-column>
      <el-table-column
        v-if="tableParams.tableOption"
        label="操作"
        :align="tableParams.tableOption.align||'center'"
        :width="tableParams.tableOption.width||'200'"
        fixed="right"
      >
        <template slot-scope="scope">
          <slot
            name="auth-button"
            :scope="scope.row"
          ></slot>
        </template>
      </el-table-column>
       <div slot="empty" class="emptyBg">
        <img  class="noDataImg" :src="noDataImg" v-if=noDataImg alt="" /> 
        <p>{{noDataText}}</p> 
          <slot name="additionalContent"></slot> 
      </div>
    </el-table>
    <el-pagination
      v-if="hasPage"
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page[this.pageConf.pageNum]"
      :page-sizes="[10,20,30,40]"
      :page-size="page[this.pageConf.pageSize]"
      :total="getListApi ? total : tableTotal"
      :layout="layout"
    >
    </el-pagination>
  </div>
</template>

<script>
export default {
  name: 'd2-table',
  data() {
    return {
      loading: false,
      data: [],
      // 页码
      page: {},
      total: 10,
      // 多选数组
      mutiSelectArr: {},
      tableColumns: []
    }
  },

  props: {
    size: {
      type: String,
      default: 'medium'
    },
     height:{
      type:Number
    },
    noDataImg:{
       type:String,
       default:''
    },
    noDataText:{
      type:String,
      default:'暂无数据'
    },
    border: {
      type: Boolean,
      default: false
    },
    headerStyle: {
      type: Object,
      default: () => {}
    },
    // 搜索条件
    searchForm: {
      type: Object,
      default: () => { }
    },
    // 是否定制表头
    isCustomHead: {
      type: Boolean,
      default: false
    },
    // 表头展示
    customHeadShowList: {
      type: Array,
      default: () => []
    },
    // 表格数据-外部传入
    tableData: {
      type: Array,
      default: () => []
    },
    tableTotal: {
      type: Number,
      default: 0
    },
    // 获取列表接口
    getListApi: {
      type: Function,
      default: null
    },
    // 搜索前操作
    beforeSearchFn: {
      type: Function,
      default: () => {
        return new Promise((resolve, reject) => {
          resolve()
        })
      }
    },
    // 分页,默认有
    hasPage: {
      type: Boolean,
      default: true
    },
    pageConf: {
      type: Object,
      default: () => ({ pageSize: 'pageSize', pageNum: 'pageNum', totalCount: 'totalCount' })
    },
    // 分页组件配置
    layout: {
      type: String,
      default: 'sizes,total,->,prev,pager,next,jumper'
    },
    // 序号,默认有
    hasNum: {
      type: Boolean,
      default: true
    },
    tableParams: {
      type: Object,
      default: () => {
        return {
          // 多选-判断来自哪个页面
          // mutiSelectName: {
          //   type: String,
          //   default: ''
          // },
          // 多选-唯一标识-行数据的key
          // mutiSelectUniqueKey: {
          //     type: String,
          //     default: ''
          // },
          // 设置禁止选中的行数据
           checkSelectKey: {
            type: String,
            default: "",
          },
          // 设置禁止选中的值
           checkSelectVal: {
            type: Array,
            default: () => [],
          },
          // 后端返回列表参数名
          listName: {
            type: String,
            default: ''
          },
          // 表格操作,默认没有
          // tableOption: {
          //   type: Object,
          //   default: null
          // },
          // 表格栏
          tableColumns: {
            type: Array,
            default: () => []
          }
        }
      }
    }
  },

  watch: {
    customHeadShowList(val) {
      this.isCustomHead && this.findShowHeadCol()
    }
  },

  mounted() {
    this.isCustomHead ? this.findShowHeadCol() : (this.tableColumns = this.tableParams.tableColumns)
    this.page = {}
    this.page[this.pageConf.pageSize] = 10
    this.page[this.pageConf.pageNum] = 1
    this.getListApi && this.refreshTable()
  },

  methods: {
    findShowHeadCol() {
      this.tableColumns = []
      this.tableParams.tableColumns.forEach(item => {
        let isCustomCol = item.hasOwnProperty('isCustomCol')
        if (!isCustomCol || (isCustomCol && this.customHeadShowList.includes(item.label))) {
          this.tableColumns.push(item)
        }
      })
    },
    // 设置禁止选中的行数据
    checkSelectable(row) {
      if (this.tableParams.checkSelectKey && this.tableParams.checkSelectVal.length>0) {
        return  this.tableParams.checkSelectVal.indexOf(row[this.tableParams.checkSelectKey]) == -1;
      }else {
        return true;
      }
    },
    // 排序
    sortChange({ column, prop, order }) {
      this.$emit('sortChange', { column, prop, order })
    },

    // 多选
    handleSelect(val) {
      this.mutiSelectArr[this.tableParams.mutiSelectName] = val
    },

    // 清空多选
    clearMutiSelect() {
      this.mutiSelectArr[this.tableParams.mutiSelectName] = []
      this.$refs[this.tableParams.mutiSelectName + 'MutiTable'].clearSelection()
    },

    refreshTable(isResetPageNum = true, isClearMuti = true) {
      this.beforeSearchFn().then(res => {
        // 清空多选
        isClearMuti && this.clearMutiSelect()
        this.loading = true
        isResetPageNum && (this.page[this.pageConf.pageNum] = 1)
        var params = {
          ...this.page,
          ...this.searchForm
        }
        this.getListApi(params).then(res => {
          this.data = this.findVal(res, this.tableParams.listName)
          this.total = this.findVal(res, this.pageConf.totalCount)
          this.loading = false
        }).catch(() => {
          this.loading = false
        })
      })
    },

    // 找返回值
    findVal(res, rankStr) {
      var rankList = rankStr.split('.')
      if (!rankStr) {
        return res
      }
      return this.findVal(res[rankList[0]], rankList.splice(1).join('.'))
    },

    // 每页条数
    handleSizeChange(value) {
      this.page[this.pageConf.pageSize] = value
      this.getListApi ? this.refreshTable(false, false) : this.$emit('refreshTable', this.page)
    },

    // 页码
    handleCurrentChange(value) {
      this.page[this.pageConf.pageNum] = value
      this.getListApi ? this.refreshTable(false, false) : this.$emit('refreshTable', this.page)
    }
  }
}

</script>
<style>
.a-click {
  cursor: pointer;
}
.el-table .cell {
  line-height: 14px;
}
.glsx-table p {
  margin: 10px 0;
}
.glsx-table .line-ellipsis {
  display: -webkit-box;
  width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  /* -webkit-line-clamp: 2; */
  -webkit-box-orient: vertical;
}
.el-table-column--selection .cell {
  padding-left: 14px!important;
  padding-right: 14px!important;
}
.glsx-table .emptyBg {
   display: inline-block;
   /* margin-top: 150px; */
   height: 100%;
}
.glsx-table .emptyBg div{
   margin: -20px 0 10px;
   height: 100%;
}
</style>