index.vue 11 KB
<!-- 表格+分页 -->
<template>
  <div class="d2-table">
    <el-table :ref="tableParams.mutiSelectName + 'MutiTable' || 'mutiTable'"
              :data="getListApi ? data : tableData"
              :row-key="tableParams.mutiSelectUniqueKey || ''"
              v-loading="loading"
              border
              :highlight-current-row="true"
              @sort-change="sortChange"
              @selection-change="handleSelect"
              :tree-props="{children: 'upgradeGoodsList'}">
      <el-table-column v-if="tableParams.mutiSelectUniqueKey && tableParams.mutiSelectName"
                       type="selection"
                       width="55"
                       :reserve-selection="true">
      </el-table-column>
      <el-table-column v-if="hasNum"
                       align="center"
                       width="60px"
                       label="序号"
                       type='index'
                       :index="index=>index+(pagination.currentPage - 1) * pagination.pageSize + 1">
      </el-table-column>
      <el-table-column v-for="(item, index) in tableParams.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="header">
          <span>{{item.label}}</span>
          <table-head-search v-if="item.optionConfig"
                             :searchForm="headSearchForm"
                             :optionConfig="item.optionConfig"
                             :baseData="baseData"
                             @handleTableHeadChange="handleTableHeadChange"></table-head-search>
        </template>
        <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.tagLabel">
            <el-tag v-if="!item.tagName"
                    :type="tableParams.tag.type[scope.row[item.prop]]"
                    v-text="tableParams.tag.label[scope.row[item.prop]]">
            </el-tag>
            <el-tag v-else
                    :type="tableParams[item.tagName].type[scope.row[item.prop]]"
                    v-text="tableParams[item.tagName].label[scope.row[item.prop]]"></el-tag>
          </template>
          <template v-else-if="item.formatter">
            <span v-if="item.html"
                  v-html="item.formatter(scope.row)"></span>
            <template v-if="!item.html">
              <span :class="item.className?item.className(scope.row):''"></span>
              <span v-text="item.formatter(scope.row)"></span>
            </template>
          </template>
          <template v-else-if="getListApi">
            <span v-if="item.prefix">{{item.prefix}}</span>
            <span
                  v-if="item.parentProp">{{scope.row[item.parentProp] ? scope.row[item.parentProp][item.prop] : ''}}</span>
            <span v-else>{{scope.row[item.prop]}}</span>
          </template>
          <template v-else>
            <span 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="操作"
                       fixed="right"
                       :align="tableParams.tableOption.align||'center'"
                       :width="tableParams.tableOption.width||'auto'">
        <template slot-scope="scope">
          <slot name="auth-button"
                :scope="scope.row"></slot>
          <slot name="auth-button-other"
                :scope="scope.row"></slot>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination v-if="hasPage"
                   background
                   @size-change="handleSizeChange"
                   @current-change="handleCurrentChange"
                   :current-page="page.currentPage"
                   :page-sizes="[10,20,30,40]"
                   :page-size="page.pageSize"
                   :total="getListApi ? total : tableTotal"
                   :layout="layout">
    </el-pagination>
  </div>
</template>

<script>
import TableHeadSearch from './TableHeadSearch'
export default {
  name: 'd2-table',
  data() {
    return {
      loading: false,
      data: [],
      // 页码
      page: {
        pageSize: 20,
        currentPage: 1
      },
      total: 10,
      // 排序
      sortColum: '',
      sort: '',
      pagination: {},
      // 多选数组
      mutiSelectArr: {}
    }
  },

  components: {
    TableHeadSearch
  },

  props: {
    // 搜索条件
    searchForm: {
      type: Object,
      default: () => { }
    },
    // 表格头部搜索条件 - start
    headSearchForm: {
      type: Object,
      default: () => { }
    },
    // 选项列表Obj
    baseData: {
      type: Object,
      default: () => { }
    },
    // 表格头部搜索条件 - end
    // 表格数据-外部传入
    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
    },
    // 分页组件配置
    layout: {
      type: String,
      default: 'sizes,total,->,prev,pager,next,jumper'
    },
    // 序号,默认有
    hasNum: {
      type: Boolean,
      default: true
    },
    // 排序列表对象
    sortObj: {
      type: Object,
      default: () => { }
    },
    tableParams: {
      type: Object,
      default: () => {
        return {
          // 多选-判断来自哪个页面
          // mutiSelectName: {
          //   type: String,
          //   default: ''
          // },
          // 多选-唯一标识
          // mutiSelectUniqueKey: {
          //     type: String,
          //     default: ''
          // },
          // 搜索按钮id;new Date().getTime() + parseInt(Math.random() * 1000)
          // btnId: {
          //   type: String,
          //   default: ''
          // },
          // 后端返回列表参数名
          listName: {
            type: String,
            default: ''
          },
          // 标签-默认标签名为tag;若有多个,那么需要在表格栏的列中加“tagName”
          // tag: {
          //   type: Array,
          //   default: () => []
          // },
          // 表格操作,默认没有
          // tableOption: {
          //   type: Object,
          //   default: null
          // },
          // 表格栏
          tableColumns: {
            type: Array,
            default: () => []
          }
        }
      }
    }
  },

  mounted() {
    const btn = document.getElementById(this.tableParams.btnId)
    btn && btn.addEventListener('click', this.refreshEmit)
    this.getListApi && this.refreshTable()
  },

  methods: {

    // 排序
    sortChange({ column, prop, order }) {
      this.sortColum = order ? prop : ''
      this.sort = order
      this.refreshTable()
    },

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

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

    // 发布事件,以期查询条件改变后,页面图表需要刷新
    refreshEmit() {
      this.$emit('searchChange')
      this.refreshTable(true, true)
    },

    refreshTable(clearMuti = false, resetCurrentPage = false) {
      this.beforeSearchFn().then(res => {
        // console.log(res)
        // 清空多选
        clearMuti && this.clearMutiSelect()
        this.loading = true
        resetCurrentPage && (this.page.currentPage = 1)
        var params = {
          ...this.page,
          ...this.searchForm
        }
        if (this.sort) {
          // params.sortColum = this.sortColum
          // params.sort = this.sort === 'descending' ? 1 : 0
          params.orderByType = this.sortObj[this.sortColum][this.sort]
        }
        this.getListApi(params).then(res => {
          this.pagination = {
            currentPage: res.pageNum,
            pageSize: res.pageSize
          }
          // this.tableParams.needUnique && (res[this.tableParams.listName].forEach((item) => {
          //   item[this.tableParams.mutiSelectUniqueKey]
          // }))
          this.data = res[this.tableParams.listName]
          if (this.tableParams.needUnique) {
            let temp = 1
            this.data.forEach((item) => {
              item[this.tableParams.mutiSelectUniqueKey] = temp
              temp = temp + 1
              if (item.hasOwnProperty('upgradeGoodsList')) {
                item.upgradeGoodsList.forEach(subItem => {
                  subItem[this.tableParams.mutiSelectUniqueKey] = temp
                  temp = temp + 1
                })
              }
            })
          }
          this.total = res.total
          this.loading = false
        }).catch(() => {
          this.loading = false
        })
      })
    },

    // 表头搜索项-改变
    handleTableHeadChange(obj) {
      this.$emit('handleTableHeadChange', obj)
    },

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

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

</script>
<style lang="scss">
.a-click {
  cursor: pointer;
}
.el-table .cell {
  line-height: 18px;
}
.d2-table p {
  margin: 10px 0;
}
.d2-table .head-search {
  cursor: pointer;
}
@mixin spotBase {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 5px;
  display: inline-block;
}
.ashSpot {
  @include spotBase();
  background: #c6c8cc;
}
.greenSpot {
  @include spotBase();
  background: #2cca74;
}
.yellowSpot {
  @include spotBase();
  background: #fdb757;
}
.redSpot {
  @include spotBase();
  background: #eb4e4e;
}
.orgenSpot {
  @include spotBase();
  background: #eb4e4e;
}
</style>