Vue自定义表格组件

考虑到最近在使用element框架开发管理后台,用到表格组件的次数比较多,简单做了一个数据展示型的表格组件。

表格列提供4种数据处理展示,文本(默认)、图片、匹配文本、匹配文本按钮,做一下解释。
字段类型必填备注
propString后台返回的列字段(所有数据处理必填)
showImageBooleanstyle字段自定义样式
optionsObject[Object]匹配文本,格式如{1:{title:'文案1',type:'primary'}}
onActionFunction用于匹配按钮事件传参
permissionBoolean用于提供匹配按钮权限

1.<DataTable>组件代码

<template>
  <div class="w-match m-t-1">

    <!-- 表格 -->
    <el-table
      :data="dataList"
      border
      fit
      highlight-current-row>

      <el-table-column
        label="序号"
        width="70"
        align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column v-for="item in columns" :width="item.width" :prop="item.prop" :label="item.label"
                       :key="item.prop">
        <template slot-scope="scope">
            <span v-if="!item.permission && item.options">
              {{
                item.options[scope.row[item.prop]] && item.options[scope.row[item.prop]]['title'] || scope.row[item.prop]
              }}
            </span>
          <el-image :preview-src-list=[scope.row[item.prop]] v-else-if="item.showImage" :src="scope.row[item.prop]"
                    :style="item.style" class="w-match" alt=""></el-image>
          <el-button :type="item.options[scope.row[item.prop]].type"
                     size="mini" icon="el-icon-edit"
                     v-else-if="item.permission && item.onAction"
                     @click="item.onAction(scope.row)">
            {{ item.options[scope.row[item.prop]].title }}
          </el-button>
          <span v-else>{{ scope.row[item.prop] }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="200" v-if="actions && actions.length>0">
        <template slot-scope="scope">
          <el-button :type="item.type" :size="item.size||'mini'" v-for="item in actions" :key="item.name"
                     @click="item.onAction(scope.row)"
                     v-if="item.permission != undefined ? item.permission : true">{{ item.title }}
          </el-button>
          <!--            <el-button type="danger" size="mini" @click="removeDataById(scope.row.id)"-->
          <!--                       v-if="hasPerm('activity.delete')">删除-->
          <!--            </el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="total, sizes, prev, pager, next, jumper"
      @current-change="getList"
      :page-sizes="[10, 20, 30, 40, 50, 100]"
      @size-change="handleSizeChange"
    />
  </div>
</template>

<script>
export default {
  name: "DataTable",
  props: {
    dataList: {
      //数据源
      type: Array,
      default: [],
      required: true
    },
    total: {
      //页大小
      type: Number,
      default: 0
    },
    columns: {
      //表格列
      type: Array,
      default: [],
      required: true
    },
    actions: {
      //表格按钮
      type: Array,
      default: [],
      required: true
    },
    onPageChange: {
      type: Function,
      default: null
    }
  },
  data() {
    return {
      page: 1,
      limit: 10,
    }
  },

  created() {
    this.getList()
  },
  methods: {
    getList(page = 1) {
      this.page = page
      this.$emit("onPageChange", this.page, this.limit);//告诉父组件页码发生变化
    },
    handleSizeChange(number) {
      this.page = 1
      this.limit = number
      this.$emit("onPageChange", this.page, this.limit);//告诉父组件页大小发生变化
    },
  }
}
</script>

<style scoped>

</style>

2.页面引用方式

<template>
  <div class="app-container">
    <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item prop="status">
        <el-select v-model="dataQuery.status" clearable>
          <el-option label="上架" :value="1"></el-option>
          <el-option label="下架" :value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-button type="primary" icon="el-icon-search" @click="queryList()">查询</el-button>
      <el-button type="default" @click="resetData()">重置</el-button>
    </el-form>
    <div class="w-match m-t-1">
      <data-able
        ref="table"
        :columns="tableColumn"
        :actions="tableAction"
        :data-list="list"
        :total="total"
        @onPageChange="getList"/>
    </div>
  </div>
</template>
<script>
import api from '@/api'
import DataTable from '@/components/dataTable/index'

export default {
  name: "Page",
  components: {
    DataTable
  },
  data() {
    return {
      list: [],
      total: 0,
      page: 1,
      limit: 10,
      dataQuery: {},
      tableColumn: [
        {prop: "createTime", label: "创建时间"},
        {prop: "mobile", label: "手机号"},
        {prop: "memberTime", label: "会员时效"},
        {prop: "redeemTime", label: "兑换时间"},
        {
          prop: "status", label: "是否兑换", width: 60,
          options: {1: {title: '是', type: 'success'}, 0: {title: '否', type: 'warning'}},
          onAction:this.updateByStatus(row,rowIndex)
          permission:this.hasPerm('demo.status')
        },
      ],
      tableAction: [
        {
               title: '编辑', type: 'primary',
               permission: this.hasPerm('demo.update'),
            onAction:(data)=>{console.log(data)}//执行编辑动作跳转
        }
      ]
    }
  },

  watch: {
    $route(to, from) {
      //路由变化方式,路由发生变化,方法就会执行
      this.getList()
    },
  },
  created() {
    this.getList()
  },
  methods: {
    getList(page=1, limit) {
      this.page = page
      this.limit = limit ? limit : this.limit
      //数据请求接口
      api.getList(this.dataQuery)
        .then(response => {
          //数据请求成功处理
          this.total = response.total
          this.list = response.data
        })
    },
    queryList() {
      this.$refs.table.getList()//重置表格为第一页
    },
    resetData() {
      this.dataQuery = {}
      this.$refs.table.getList()//重置表格为第一页
    },

  }
}
</script>

泉州牧码人
73 声望4 粉丝

前端工程师