element的table表格组件,动态获取表头和数据

2 篇文章 0 订阅
2 篇文章 0 订阅

1、新建table组件(tableThead.vue),考虑存在的所有列(序号、单选框、多选框、常规normal只显示内容、常规normal 存在模板样式、常规normal 存在工具条)。
下面是tableThead.vue组件的内容:

<template>
  <el-table class="tb-edit" highlight-current-row :data="tables.tableData"
            :pageCode="tables.pageCode" :checkBox="tables.checkBox"
            style="width: 100%" :height="tables.height?tables.height:null"
            @row-click="showRadioRow"
            @selection-change="handleSelectionChange">
    <template v-for="(col ,index) in cols">
      <!--序号index-->
      <el-table-column v-if="col.type==='index'" :type="col.type" :label="col.label" :width="col.width" :fixed="col.fixed==''?null:col.fixed" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <!--多选框selection-->
      <el-table-column v-if="col.type==='selection'" :type="col.type" :width="col.width" :fixed="col.fixed==''?null:col.fixed" align="center" show-overflow-tooltip></el-table-column>
      <!--单选框radio-->
      <el-table-column v-if="col.type==='radio'" :type="col.type" :width="col.width" :fixed="col.fixed==''?null:col.fixed" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-radio class="radio"  v-model="radio"  :label="scope.$index">&nbsp;</el-radio>
        </template>
      </el-table-column>
      <!--常规normal 不存在工具条和模板样式,只显示当前内容-->
      <el-table-column v-if="col.type==='normal' && !col.toolbar && !col.templet" :prop="col.prop" :label="col.label"
                       :width="col.width" :fixed="col.fixed==''?null:col.fixed" :toolbar="col.toolbar" align="center"
                       show-overflow-tooltip>
      </el-table-column>
      <!--常规normal 存在模板样式,内容根据templet更改样式-->
      <el-table-column v-if="col.type==='normal' && col.templet" :prop="col.prop" :label="col.label" :width="col.width" :fixed="col.fixed==''?null:col.fixed" :toolbar="col.toolbar" align="center" show-overflow-tooltip>
        <!--数据库字段templet和父组件传递的tables数据中templet都存在
        slot-scope="scope":这条数据的内容
         -->
        <template v-if="col.templet && tables.templet"  slot-scope="scope">
        <!-- 父组件tableType(0:可改变字体颜色,1:可改变字体颜色和存在点击事件,2:el-switch点击事件,3:带小圆点的状态可改变字体颜色和存在点击事件)  -->
         <el-button type="text" v-if="bar.tableType==0"  :class="bar.class" :style="{color:bar.color}"
                     v-for="(bar ,index) in tables.templet(col.prop,scope.row)"  :key='index'>{{bar.name}}</el-button>
          <el-button type="text" v-if="bar.tableType==1" @click="bar.tableClick(scope.row.id)" :class="bar.class" :style="{color:bar.color}"
                     v-for="(bar ,index) in tables.templet(col.prop,scope.row)"  :key='index'>{{bar.name}}</el-button>
          <!--开关样式变成了radio的样式-->
          <el-switch v-if="bar.tableType==2" class="switchStyle" active-color="var(--activecolor)" inactive-color="#D5D5D5"
                     :active-value="bar.switchs.active" :inactive-value="bar.switchs.inactive"
                     :active-text="bar.switchs.activetext" :inactive-text="bar.switchs.inactivetext" v-model="scope.row.allowBorrow"
                     v-for="(bar ,index) in tables.templet(col.prop,scope.row)" @change="if(bar.tableClick)bar.tableClick(scope.row)" :key='index'>
          </el-switch>
          <el-button  type="text" v-if="bar.tableType==3" @click="if(bar.tableClick)bar.tableClick" :class="bar.class" :style="{color:bar.color}"
                     v-for="(bar ,index) in tables.templet(col.prop,scope.row)"  :key='index'>
            <span class="circleDot" :class="bar.circleColor"></span>
            {{bar.name}}</el-button>
        </template>
      </el-table-column>
      <!--常规normal 存在工具条-->
      <el-table-column v-if="col.type==='normal' && col.toolbar" :prop="col.prop" :label="col.label" :width="col.width" :fixed="col.fixed==''?null:col.fixed" :toolbar="col.toolbar" align="center" show-overflow-tooltip>
        <template v-if="col.toolbar"  slot-scope="scope">
              <el-button size="small" type="text" @click="bar.tableClick(scope.row.id,scope.row,$event)" :disabled="bar.disabled ? true : false" :class="bar.class" v-if='tables.toolbar.judge' :style="{color:bar.color}"
                         v-for="(bar ,index) in tables.toolbar.judge(scope.row)"  :key='index'>{{bar.name}}</el-button>
        </template>
      </el-table-column>
      <el-table-column v-if="col.type==='sort'" :prop="col.prop" sortable :label="col.label" :width="col.width" :fixed="col.fixed==''?null:col.fixed" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-tag type="primary">{{ scope.row.type }}</el-tag>
        </template>
      </el-table-column>
    </template>
  </el-table>
</template>
<script>
    import ElButton from "../../../node_modules/element-ui/packages/button/src/button.vue";
    export default {
      components: {ElButton},
      name: "TableThead",
        props:{
          tables:{type:Object,required:true},
          page:{type:Object,required:true},
          handleSelectionChange:{type:Function,required:true},
          showRadioRow:{type:Function,required:true}
        },
        data() {
            return {
              parent:'',
              cols: [],
              radio:'',
            };
        },
        mounted() {
          this.init();
        },
        methods: {
          init(){
            //查询表格的表头(两种情况:数据库查询表头信息,调用table组件的时候带上表头数据cols)
            this.parent=this.tables.parent;
            if(this.tables.pageCode){
              this.$api.commonInterface.tableCols({
                pageCode: this.tables.pageCode,
              }).then(res => {
                console.log(res.rows)
                let cols=res.rows;
                if(this.tables.checkBox)this.cols.push({type:"selection",width:"55",fixed: "left"});
                if(this.tables.radio)this.cols.push({type:"radio",width:"55",fixed: "left"});
                this.cols.push({type:"index",label: "序号",width:"55",fixed: "left"});
                for(let col of cols){
                  //hide:0显示,1隐藏
                  if(col.hide===0){
                    let coljson={
                      label: col.title, prop: col.field, type: "normal",
                      width:col.width,
                      toolbar:col.toolbar,
                      templet:col.templet,
                      fixed: col.fixed
                    };
                    this.cols.push(coljson);
                  }
                }
              });
            }else if(!this.tables.pageCode && this.tables.cols){
              let cols=this.tables.cols;
              if(this.tables.checkBox)this.cols.push({type:"selection",width:"55",fixed: "left"});
              if(this.tables.radio)this.cols.push({type:"radio",width:"55",fixed: "left"});
              this.cols.push({type:"index",label: "序号",width:"55",fixed: "left"});
              for(let col of cols){
                //hide:0显示,1隐藏
                if(col.hide===0){
                  let coljson={
                    label: col.title, prop: col.field, type: "normal",
                    width:col.width,
                    toolbar:col.toolbar,
                    templet:col.templet,
                    fixed: col.fixed
                  };
                  this.cols.push(coljson);
                }
              }
            }
          },
        }
    };
</script>
<style scoped>
  //定义table表格的样式
</style>

2、引入组件tableThead.vue

import TableThead from "@/components/common/tableThead";
export default {
  name: "manage", 
  components: {
    TableThead
  },
  data() {
    return {}
  }
 }

3、调用组件,tables是表格的参数,page是表格分页的参数(pagination分页组件
),showRadioRow是单选事件,handleSelectionChange是多选事件。(参数必须存在)

<table-thead :tables="tables" :page="page" :showRadioRow='showRadioRow' :handleSelectionChange='handleSelectionChange'></table-thead>

tables的数据格式如下:

tables:{
             tableData: [],//表格数据
             pageCode:"",//表头
             cols:[], //pageCode存在的情况,cols不写。cols的格式:[{hide: 0,title: "ID",field: "id",width: "",toolbar: "",templet: "",fixed: ""},]。
             checkBox:true,//是否存在多选框(true:是,false:否)
             radio:true,//是否存在单选框(true:是,false:否)
             height: '',//表格高度
             templet:function (val,msg) {  //表格数据的模板样式
               var _this = this;
               if (val === ""){
               //tableType(0:可改变字体颜色,1:可改变字体颜色和存在点击事件,2:el-switch点击事件,3:带小圆点的状态可改变字体颜色和存在点击事件)
                 //class参数可传可不传,可以改变样式。color可传可不传,改变字体颜色。tableClick点击事件
                 //tableType为2的时候,switchs必须有
                 return [{name:'',tableType:"1",class:"",color:"",switchs:{
                   checkname:"",
                   active:"是",activetext:"是",
                   inactive:"否",inactivetext:"否"
                 },tableClick:function (res) {
                   console.log(msg.id)
                 }}]
               }
             }.bind(this),
             toolbar: { //表格工具条的事件
               judge:function (msg) {
                 let _this=this;
               }.bind(this) //有需要时绑定父组件的this
             }
           },
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
element 表格是一种可以使用前端框架 Element UI 创建的数据展示组件。它可以通过与后端进行交互,从后端获取数据并展示在页面上。 为了从后端获取数据,我们需要使用合适的后端技术,例如 Node.js、Java 或者 Python 等等。后端技术可以提供数据接口,允许前端通过 HTTP 请求发送获取数据的请求。 一般情况下,我们会使用借助第三方库,例如 axios 或者 Fetch API,在前端代码中发送请求到后端。我们可以通过发送GET、POST、PUT 或者 DELETE 等不同类型的 HTTP 请求,来获取、添加、更新或者删除数据。 当请求发送到后端时,后端会根据请求的内容,执行相应的操作。在获取数据的情况下,后端可以连接数据库、读取文件或者其他数据源,找到相应的数据,并返回给前端。 前端接收到后端返回的数据后,可以通过 element 表格组件的 prop 属性中的 data 属性,将数据传入表格组件element 表格会根据传入的数据,自动生成表格,并在页面上展示数据。 除了数据element 表格还可以通过 prop 属性的方式,传入一些其他的配置选项,例如表头信息、每列的宽度、操作按钮等等。这样,我们可以在前端页面上,自定义表格的显示样式和功能。 综上所述,element 表格从后端获取数据的过程包括前端发送请求到后端,后端处理请求并找到相应的数据,将数据返回给前端,前端通过 prop 属性传入数据并渲染表格。这样,我们就可以在页面上展示从后端获取数据了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值