【问题标题】:Element UI table with dynamic columns具有动态列的元素 UI 表
【发布时间】:2019-07-13 16:05:32
【问题描述】:

我正在寻找一个使用 Element UI 表格组件而无需对所有列进行硬编码的示例。我见过的所有示例,包括 official Element UI table docs 都显示了模板中指定的每一列。

我正在尝试做这样的事情。在我的旧表格组件中,这为我提供了所有列和带有delete 按钮的额外结束列。

<template>
  <div v-if="tableData.length > 0">
    <b-table striped hover v-bind:items="tableData" :fields=" keys.concat(['actions']) ">
      <template slot="actions" slot-scope="row">
        <b-btn size="sm" @click.stop="tableClick(row.item)" class="mr-1">
          Delete
        </b-btn>
      </template>
    </b-table>
  </div>
</template>

相比之下,Element UI Table 示例都使用了多个重复的el-table-column 标签。由于在运行时加载不同列的数据,我不能使用这种方法。

  <template>
    <el-table
      :data="tableData"
      style="width: 100%">
      <el-table-column
        prop="date"
        label="Date"
        width="180">
      </el-table-column>
      <el-table-column
        prop="name"
        label="Name"
        width="180">
      </el-table-column>
      <el-table-column
        prop="address"
        label="Address">
      </el-table-column>
    </el-table>
  </template>

我是一个初学者,正在努力理解如何使用 el-table 实现我的目标。

【问题讨论】:

    标签: vue.js vuejs2 element-ui


    【解决方案1】:

    您可以将列作为具有所需属性的对象数组,并在模板中使用v-for 对其进行迭代:

    <template>
        <el-table
              :data="tableData"
              style="width: 100%">
            <el-table-column v-for="column in columns" 
                             :key="column.label"
                             :prop="column.prop"
                             :label="column.label"
                             :formatter="column.formatter"
                             :min-width="column.minWidth">
            </el-table-column>
            <el-table-column fixed="right">
                <template slot-scope="scope">
                  ... your delete button or whatever here...
                </template>
        </el-table-column>
        </el-table>
    </template>
    

    然后你从某个地方获取你的列,它们可能在数据中,例如:

    data() {
        return {
          columns: [
            {
              prop: 'date',
              label: 'Date',
              minWidth: 180px
            },
            {
              prop: 'name',
              label: 'Name',
              minWidth: 180px
            },
            {
              prop: 'address',
              label: 'Address',
              formatter: (row, col, cell, index) => this.addressFormatter(cell),  //example of a formatter
            },
          ],
        };
    },
    

    【讨论】:

      猜你喜欢
      • 2021-11-26
      • 1970-01-01
      • 1970-01-01
      • 2013-07-16
      • 2019-06-02
      • 1970-01-01
      • 2012-04-13
      • 2014-12-08
      • 1970-01-01
      相关资源
      最近更新 更多