log:

背景:前端表格根据接口返回的参数进行动态生成行和列

前端:vue+elementUI

举例:接口返回参数:

[
  {
    "created": "Thu, 04 Mar 2021 14:27:29 GMT", 
    "id": 39, 
    "service_id": 25, 
    "version": "V1.0.0"
  }, 
  {
    "created": "Thu, 04 Mar 2021 16:45:45 GMT", 
    "id": 40, 
    "service_id": 27, 
    "version": "V1.0.0"
  }, 
  {
    "created": "Fri, 05 Mar 2021 11:03:56 GMT", 
    "id": 41, 
    "service_id": 28, 
    "version": "V1.0.0"
  }, 
  {
    "created": "Fri, 05 Mar 2021 15:14:17 GMT", 
    "id": 42, 
    "service_id": 26, 
    "version": "1.0.0"
  }
]
 

表格参数说明: 

el-table-column 

  :prop关联的是表头的值 

  :label关联的是表头的文本

titleData: 这里就是表头数据[{value: 'platcode', label: "平台"}]

tableColumns: 表格数据[{platcode: '平台A'}, {platcode: '平台B'},  {platcode: '平台C'}]

<el-table :data="tableColumns" style="width: 100%">

     <el-table-column v-for="(item,key) in titleData" :key="key" :prop="item.value"

      :label="item.label"></el-table-column>

</el-table>

参数对应关系:

代码如下:

<el-row :gutter="15">
   <el-col :span="24">
      <el-table class="tableClass" :data="formData.result" border height='350' width='500'>
          <el-table-column v-for="key in formData.listkey" :key="key" :prop="key"
                :label="key"></el-table-column>
          </el-table>
    </el-col>
</el-row>





data() {
    return {
      formData: {
        result: [],
        listkey:[],
      },
        }
methods: {
    batchSql(){
      this.formData.listkey=[]
      this.formData.result=[]
      let reSql = this.formData.sql.split("\n");
      let prot = parseInt(this.formData.prot)
      this.$axios
      .post('/batchSql/',
      { "workspaceEnv":"rests",
        "tenant":this.formData.tenant,
        "dblink":{"host":this.formData.host,"port":prot,"user":this.formData.user,"password":this.formData.password,"database":this.formData.database},
        "sql":reSql,
        "operate":this.formData.operate
        })
        .then((res)=>{
          this.formData.result =res.data 
          //提取表头字段====
          for (let i = 0, l = this.formData.result.length; i < l; i++) {
            for (let key in this.formData.result[i]) {
              if (this.formData.listkey.indexOf(key)==-1){
                  this.formData.listkey.push(key)
              }else{
                console.log("============xxxx===========")
              }
            }
          }
        console.log(this.formData.listkey,"==============",this.formData.result);   
        })
    }, 
}
}

实现效果:

效果一

效果二

 

GitHub 加速计划 / eleme / element
54.06 K
14.63 K
下载
A Vue.js 2.0 UI Toolkit for Web
最近提交(Master分支:3 个月前 )
c345bb45 7 个月前
a07f3a59 * Update transition.md * Update table.md * Update transition.md * Update table.md * Update transition.md * Update table.md * Update table.md * Update transition.md * Update popover.md 7 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐