elementUI表格顯示數據


elementUI官方文檔里的表格數據都是寫死的,不是循環來的,現在精簡一下。

1.定義tableData

tableData: [
        { date: "2010-1-2", name: "嘻嘻", address: "廣州天河東圃" },
        { date: "2010-1-2", name: "嘻嘻", address: "廣州天河東圃" },
        { date: "2010-1-2", name: "嘻嘻", address: "廣州天河東圃" }
      ],

2.定義tableCol,里面需要包含prop,label,width. 如果沒有width屬性,那么會占用剩下的寬度

 tableCol: [
        { prop: "date", label: "日期", width: 180 },
        { prop: "name", label: "姓名", width: 200 },
        { prop: "address", label: "地址" }
      ]

3.控制操作列的顯示隱藏。操作列,只能寫死在el-column里

 showOper:true,

 

完整例子:

<template>
  <div>
    <el-table :data="tableData" style="width: 100%" border>
      <el-table-column
        v-for="(item,i) in tableCol"
        :key="i"
        :prop="item.prop"
        :label="item.label"
        :width="item.width"
      ></el-table-column>
       <el-table-column label="操作" v-if="showOper">
      <template slot-scope="scope">
        <el-button
          size="mini"
          @click="handleEdit(scope.$index, scope.row)">編輯</el-button>
        <el-button
          size="mini"
          type="danger"
          @click="handleDelete(scope.$index, scope.row)">刪除</el-button>
      </template>
    </el-table-column>
    </el-table>
  </div>
</template>
<script>
export default {
  name: "HelloWorld",
  data() {
    return {
      showOper:true,
      tableData: [
        { date: "2010-1-2", name: "嘻嘻", address: "廣州天河東圃" },
        { date: "2010-1-2", name: "嘻嘻", address: "廣州天河東圃" },
        { date: "2010-1-2", name: "嘻嘻", address: "廣州天河東圃" }
      ],
      tableCol: [
        { prop: "date", label: "日期", width: 180 },
        { prop: "name", label: "姓名", width: 200 },
        { prop: "address", label: "地址" }
      ]
    };
  },
  methods: {
      handleEdit(index, row) {
        console.log(index, row);
      },
      handleDelete(index, row) {
        console.log(index, row);
      }
  },
};
</script>
<style lang="css" scoped>
</style>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM