【问题标题】:Vue Good Table filter a composite columnVue Good Table 过滤复合列
【发布时间】:2020-03-13 23:29:13
【问题描述】:

我有一个 vue-good-table,我想在其中稍微限制列并制作一些复合列,这些列由行的多个属性组成。现在的问题是,filterFn 采用列名来填充方法的数据参数。所以我只能过滤我决定用作列名的那个。 有没有办法像在 sortFn 中一样将整行对象提供给 filterFn?

模板:

<template v-if="props.column.field === 'flags'">
    <div v-if="props.row.isGlobal">Global</div>
    <div v-if="props.row.isMobile">Mobile</div>
</template>

列定义(我想显示设置了 Global 或 Mobile 标志的所有行):

{
    label: 'Flags',
    field: 'flags',
    sortable: false,
    filterOptions: {
        enabled: true,
        filterDropdownItems: ['Global', 'Mobile'],
        filterFn: this.flagFilter,
    },
},

函数(不工作,因为data 未定义):

public flagFilter(data, filterString) {
    if ('Global' === filterString) {
        return data.isGlobal;
    } else if ('Mobile' === filterString) {
        return data.isMobile;
    }
    return true;
}

【问题讨论】:

    标签: vue.js vue-good-table


    【解决方案1】:

    由于我认为没有办法将整行放入过滤器函数中,因此您可以将计算属性中的行映射到 正确 数据结构

    const vm = new Vue({
      el: "#app",
      name: "my-component",
      data: {
        columns: [{
          label: "Flags",
          field: "flags",
          sortable: false,
          filterOptions: {
            enabled: true,
            filterDropdownItems: ["Global", "Mobile"]
          }
        }],
        rows: [{
            isGlobal: true
          },
          {
            isMobile: true
          }
        ]
      },
      computed: {
        actualRows() {
          return this.rows.map((r) => {
            if (r.isMobile) {
              r.flags = "Mobile"
            }
    
            if (r.isGlobal) {
              r.flags = "Global"
            }
            return r
          })
        }
      }
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue-good-table@2.17.3/dist/vue-good-table.js"></script>
    
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/vue-good-table@2.17.3/dist/vue-good-table.css">
    
    <div id="app">
      <vue-good-table :columns="columns" :rows="actualRows">
      </vue-good-table>
    </div>

    【讨论】:

    • 好主意,虽然它会干扰 Typescript,但我不能只为我的对象添加一个属性,除非我在界面中定义它,这有点难看。但如果没有其他办法,我必须接受这种解决方法。
    猜你喜欢
    • 2019-03-31
    • 2019-10-06
    • 2019-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-22
    • 2019-05-21
    • 1970-01-01
    相关资源
    最近更新 更多