autoTable.vue 4.46 KB
<!-- 传入接口名称,自动调用接口,并更新表格数据 -->
<template>
  <div>
    <el-form
      :inline="true"
      :model="searchForm"
    >
      <el-form-item label="id:">
        <el-input v-model="searchForm.id"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          size="small"
          type="primary"
          @click="handleSearch"
        >查询</el-button>
        <el-button @click="handleCustomCol">定制列</el-button>
        <el-button @click="handleOption">
          批量操作
        </el-button>
      </el-form-item>
    </el-form>
    <glsx-table
      ref="tableRef"
      :searchForm="searchForm"
      :isCustomHead="isCustomHead"
      :customHeadShowList="tableHeadShowList"
      :tableParams="tableParams"
      :getListApi="getListApi"
      :pageConf="pageConf"
      :beforeSearchFn="beforeSearchFn"
      @sortChange="sortChange"
    >
      <span
        slot="showImg"
        slot-scope="scope"
      >
        <img
          style="width: 100px; height: 100px;"
          :src="scope.scope.coverPicture"
        >
      </span>
      <span
        slot="auth-button"
        slot-scope="scope"
        :data="scope.scope"
      >
        <el-button
          type="text"
          @click="handleAdd(scope.scope)"
        >新增</el-button>
      </span>
    </glsx-table>
    <glsx-dialog
      title="定制列"
      :isVisible="isHeadVisible"
      @closeDialog="handleHeadClose"
      @confirmDialog="handleHeadConfirm"
    >
      <el-checkbox-group v-model="headShowList">
        <el-checkbox
          v-for="(item, index) in tableParams.tableColumns"
          :key="index"
          :label="item.label"
          :disabled="!item.hasOwnProperty('isCustomCol')"
        ></el-checkbox>
      </el-checkbox-group>
    </glsx-dialog>
  </div>
</template>

<script>
import { getListApi } from '../api/test'
// import d2Table from '../components/d2-table'
import glsxDialog from '../components/glsx-dialog'
export default {
  data() {
    return {
      searchForm: {
        id: ''
      },
      // 定制列-弹窗
      isHeadVisible: false,
      headShowList: [],
      // 表格组件
      getListApi: getListApi,
      isCustomHead: true,
      tableHeadShowList: [],
      pageConf: { pageSize: 'pageSize', pageNum: 'pageNum', totalCount: 'total' },
      // 表格参数
      tableParams: {
        listName: 'list',
        // 行数据的key
        mutiSelectUniqueKey: 'id',
        // 多选ref的名称
        mutiSelectName: 'list',
        tableOption: true,
        tableColumns: [{
          label: 'id',
          prop: 'id',
          // width: '80px'
        }, {
          label: '标题',
          prop: 'title',
          line: 1,
          isCustomCol: true,
          sortable: true
        }, {
          label: '状态',
          prop: 'status',
          formatter: cellValue => {
            return {
              0: '待审核',
              1: '通过',
              2: '失败'
            }[cellValue.status]
          }
        }, {
          label: '图片',
          isCustomCol: true,
          prop: 'coverPicture',
          addHtml: 'showImg'
        }]
      }
    }
  },

  components: {
    // d2Table,
    glsxDialog
  },

  methods: {
    // 刷新表格前的回调函数
    beforeSearchFn() {
      return new Promise(resolve => {
        this.$message('点击查询,可在此格式化搜索项数据')
        resolve()
      })
    },
    // 弹窗
    handleHeadClose() {
      this.headShowList = this.tableHeadShowList
      this.isHeadVisible = !this.isHeadVisible
    },
    handleCustomCol() {
      this.handleHeadClose()
    },
    handleHeadConfirm() {
      this.tableHeadShowList = this.headShowList
      this.handleHeadClose()
    },
    // 刷新表格
    handleSearch() {
      this.$refs.tableRef.refreshTable()
    },
    sortChange({ column, prop, order }) {
      // 添加-排序相关参数
      this.searchForm.order = order
      this.searchForm.sortAttr = order ? prop : null
      this.handleSearch()
    },
    // 新增
    handleAdd(row) {
      this.$notify({
        title: '提示',
        message: '点击了id为' + row.id + '的数据'
      })
      this.handleSearch()
    },
    // 获取批量数据
    handleOption() {
      this.$notify({
        title: '提示',
        message: '当前选中的数据的id有' + this.$refs.tableRef.mutiSelectArr[this.tableParams.mutiSelectName].map(item => item.id).join(',')
      })
    }
  }
}

</script>
<style scoped>
</style>