表格的表头,表体根据后端返回的数据驱动,还有排序

25 篇文章 0 订阅
13 篇文章 0 订阅

1,今天的需求是改掉之前的表格的写法,之前是用el-table-column一列一列的写,现在直接循环表头来显示各列;
2,el-table里面的样式部分在之前的笔记,表格通用样式里面有;
3,:data='patientInfo’这个数据是表体的数据里面,比如结构为【{Name: “陈XX”
Place: “1146481”}】; v-for="title in Head"这个Head的结构为 【{ColumnCode: “Name”
ColumnName: “姓名”}】; <el-table-column 里绑定了:prop=“title.ColumnCode” :label=“title.ColumnName” ;显示的表头列是ColumnName的数据;列绑定的内容则是对应的Name的数据;‘
4,关于:min-width="AutoWidth(title)"是封装了方法,根据传入的值设定最小宽度,代码贴在下面;
5,:sortable=“title.IsSort==1 ? ‘custom’ : false” 这个是列排序,排序的方法写在了@sort-change="sortChange"这里;

<el-table
      :data="patientInfo"
      border
      class="eltable"
      height="96%"
      :header-cell-class-name="headerClassName"
      :header-cell-style="{
        'text-align': 'center',
        background: '#e8eefc',
        color: '#333333',
        'font-size': '14px',
        'font-family': 'PingFang SC',
        'font-weight': '700',
        'border-color': '#CFDBFB',
      }"
      :cell-style="{ 'border-color': '#CFDBFB', padding: '0px' }"
      stripe
      style="width: 98%"
      :row-class-name="tableRowClassName"
      :row-style="{ height: '36px' }"
      @row-dblclick="goPDetails"
      :resizable="true"
      @sort-change="sortChange"
    >
      <template v-for="title in Head" v-loading="loading">
        <el-table-column 
          :key="title.ID" :min-width="AutoWidth(title)"
          :prop="title.ColumnCode" :label="title.ColumnName" align="center"
          show-overflow-tooltip :sortable="title.IsSort==1 ? 'custom' : false">
          <template slot-scope="scope">
	          <span v-if="scope.row.Name.length < 4 && title.ColumnCode == 'Name'"
	              class="patientInfoDiagnosis"
	              >{{ scope.row.Name }}
	          </span>
	          <span v-else-if="scope.row.Bed && title.ColumnCode == 'Bed'"
	              style="
	                color: #2b5ff7;
	                font-size: 14px;
	                font-family: PingFang SC;
	                font-weight: 400;
	              "
	              >{{ scope.row.Bed }}
	           </span>
	           <div v-else>
	              {{ scope.row[title.ColumnCode] }}
	            </div>
        </template>
     </el-table-column>
   </template>
// 排序
    sortChange(column, prop, order) {
      let SortWay = '';
      console.log(column, "999999999",prop,order);
      if (column == null || column.column == null) return;
      // if (this.$refs.table) this.$refs.table.clearSort() //清除排序
      if (column.order == "ascending") {
        //升序
        SortWay = 0;
      } else {
        //倒序
        SortWay = 1;
      }
      //这是后端要的排序的字段
      let SortInfo = {
        SortCode:column.prop,
        SortDirection:SortWay
      }
      //去触发排序的接口,我这里是在父组件
      this.$emit("SortInfo", SortInfo);
    },

6,以上就是循环表格和排序了,
7,以下是封装的列的最小宽度

//引入
import OtherSheetTableWidth from "@/api/OtherSheetTableWidth";

AutoWidth(title) {
 console.log(545,title)
  return OtherSheetTableWidth(title);
},

在OtherSheetTableWidth.js 里

const AutoWidth = function (title) {
  // console.log(title)
  switch (title.ColumnCode) {
    case "Name":
      return '50'
      break;
       default:
      break;
  }
}
export default AutoWidth

END 哈哈哈哈

可以通过动态生成表头的方式来实现根据后端返回数据动态渲染多级表头具体的实现步骤如下: 1. 获取后端返回表头数据,可以通过 API 接口获取。 2. 遍历表头数据,生成对应的对象。可以使用 Element UI 表格组件中的 column 属性来实现。 3. 如果当前对象有子,那么递归生成子对象。 4. 将生成的对象数组作为表格组件的 columns 属性进行渲染。 以下是一个示例代码: ```vue <template> <el-table :data="tableData" :columns="tableColumns" border ></el-table> </template> <script> export default { data() { return { tableData: [], tableColumns: [], }; }, methods: { // 获取后端返回表头数据 async fetchTableHeader() { const res = await fetch('/api/table/header'); const headerData = await res.json(); this.tableColumns = this.generateColumns(headerData); }, // 递归生成对象 generateColumns(headerData) { return headerData.map((item) => { const column = { label: item.label, prop: item.prop, }; if (item.children && item.children.length) { column.children = this.generateColumns(item.children); } return column; }); }, // 获取后端返回表格数据 async fetchTableData() { const res = await fetch('/api/table/data'); this.tableData = await res.json(); }, }, mounted() { this.fetchTableHeader(); this.fetchTableData(); }, }; </script> ``` 在这个示例代码中,通过 fetchTableHeader 方法获取后端返回表头数据,并使用 generateColumns 方法生成对应的对象,如果当前对象有子,那么会递归生成子对象。最后将生成的对象数组作为表格组件的 columns 属性进行渲染。 需要注意的是,由于表格数据是异步获取的,因此需要在获取表头数据表格数据之后再进行渲染。在这个示例代码中,通过 mounted 钩子函数来依次调用 fetchTableHeader 和 fetchTableData 方法获取数据,并在获取数据完成后渲染表格组件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值