【问题标题】:Set filter options on vue-good-table programmatically以编程方式在 vue-good-table 上设置过滤器选项
【发布时间】:2019-10-06 12:23:15
【问题描述】:

如何以编程方式更改 vue-good-table 中 UI 过滤器元素(输入、下拉列表等)中显示的值?

例如,如果我调用: this.$set(this.table.columnsFilters, 'name', 'bob')

我希望 HTML 输入字段“名称”中的值显示 bob。

不幸的是,我上面描述的设置值不起作用

【问题讨论】:

  • 我不明白您所说的“我希望 HTML 输入字段 'name' 中的值显示 bob。相反,我的数据被 bob 过滤了……”你是什么意思指的是类似的占位符文本?
  • Jakobovski - 你能详细说明你的意思吗?
  • @MattOestreich 我添加了说明
  • 所以您想以编程方式更改列的当前过滤器?
  • @MattOestreich。是的,但是过滤器和 UI 中显示的值是有区别的,所以我特别提到了 UI 值。通常应该是相同的,但我发现有时过滤器值与 UI 中显示的值不同。

标签: vue.js vue-good-table


【解决方案1】:

编辑如果您使用的是2.16.0 或更高版本,您需要这样做:

CodePen for v2.16.0 +

// v2.16.0 or higher
...
changeFilter(field, newFilter) {
      let newCols = JSON.parse(JSON.stringify(this.columns));
      let found = newCols.find(c => {
        return c.field == field;
      });
      console.log(found);
      if (found) {
        if (found.hasOwnProperty("filterOptions")) {
            found.filterOptions.filterValue = newFilter;
            this.columns = newCols;
        } else {
          alert(`Column '${field}' does not have filtering configured!`);
        }
      } else {
        alert(`Field '${field}' does not exist!`);
      }
}

原始答案

如果我正确理解这一点,您想以编程方式为字段设置过滤器...这样的事情应该可以工作..单击按钮以编程方式更改过滤器...如果您将此行更改为任何有效名称,它将按该名称过滤...将“Bob”更改为有效名称...(如 Dan)...

<button 
  style="width:200px;" 
  @click.stop="changeFilter('name', 'Bob')"
>Click To Change Name Filter</button>

CodePen Mirror

const vm = new Vue({
  el: "#app",
  name: "my-component",
  data: {
    columns: [
      {
        label: "Name",
        field: "name",
        filterOptions: {
          enabled: true, // enable filter for this column
          placeholder: "Filter Name", // placeholder for filter input
          filterValue: "", // initial populated value for this filter
          filterDropdownItems: [], // dropdown (with selected values) instead of text input
          filterFn: this.columnFilterFn, //custom filter function that
          trigger: "enter" //only trigger on enter not on keyup
        }
      },
      {
        label: "Age",
        field: "age",
        type: "number"
      },
      {
        label: "Created On",
        field: "createdAt",
        type: "date",
        dateInputFormat: "YYYY-MM-DD",
        dateOutputFormat: "MMM Do YY"
      },
      {
        label: "Percent",
        field: "score",
        type: "percentage"
      }
    ],
    rows: [
      {
        id: 1,
        name: "John",
        age: 20,
        createdAt: "201-10-31:9: 35 am",
        score: 0.03343
      },
      {
        id: 2,
        name: "Jane",
        age: 24,
        createdAt: "2011-10-31",
        score: 0.03343
      },
      {
        id: 3,
        name: "Susan",
        age: 16,
        createdAt: "2011-10-30",
        score: 0.03343
      },
      {
        id: 4,
        name: "Bob",
        age: 55,
        createdAt: "2011-10-11",
        score: 0.03343
      },
      {
        id: 5,
        name: "Dan",
        age: 40,
        createdAt: "2011-10-21",
        score: 0.03343
      },
      {
        id: 6,
        name: "John",
        age: 20,
        createdAt: "2011-10-31",
        score: 0.03343
      }
    ]
  },
  methods: {
    clearFilter(field) {
      try {
        let found = this.columns.find((c) => {
          return c.field == field
        });
        found.filterOptions.filterValue = "";
      } catch {
        alert(`Unable to clear ${field} filter`)
      }
    },
    changeFilter(field, newFilter) {
      let found = this.columns.find((c) => {
        return c.field == field
      });
      if(found) {
        if(found.hasOwnProperty("filterOptions")) {
          if(found.filterOptions.hasOwnProperty("filterValue")) {
            found.filterOptions.filterValue = newFilter;
          } else {
            alert(`Column '${field}' does not have filterValue property!`)
          }          
        } else {
          alert(`Column '${field}' does not have filtering configured!`)
        }
      } else {
        alert(`Field '${field}' does not exist!`)
      }
    }
  }
});
<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.15.3/dist/vue-good-table.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/vue-good-table@2.12.2/dist/vue-good-table.css" rel="stylesheet"/>

<div id="app">
  <div>
    <div style="margin:10px 10px 10px 10px;">
      <button 
        style="width:200px;" 
        @click.stop="changeFilter('name', 'Bob')"
      >Click To Change Name Filter</button>
      <button 
        style="width:200px;" 
        @click.stop="clearFilter('name')"
      >Clear Name Filter</button>
    </div>
    <vue-good-table :columns="columns" :rows="rows" />
  </div>
</div>

【讨论】:

  • 在 vue-good-table 2.16.x 中不工作,你知道为什么吗?
  • @paul 这很烦人——我用新版本尝试了几件事,但无济于事。如果您在他们的 GitHub 存储库中提出问题,也许会有所帮助?可能是他们的错误?
  • @paul - 我已经用 v2.16.0+ 的有效解决方案更新了我的答案
【解决方案2】:

Vue 无法检测到正常的属性添加(例如this.myObject.newProperty = 'hi'

因此,使用this.$set(this.table.columnsFilters, 'name', 'bob') 代替您的代码。

More information about $set

【讨论】:

  • 这应该被标记为正确答案,这是一个工作示例:jsfiddle.net/aks9800/cyegzwL7
  • @xaksis ... sigh .... github.com/xaksis/vue-good-table/issues/… 在 2.16.0+ 中,您在代码中遇到的比较错误(您最初一再声称与您的代码无关)是防止这种情况....如果您将值设置为空字符串,则不必使用$set........仅在您删除错误之后。
【解决方案3】:

见:https://github.com/xaksis/vue-good-table/issues/475

版本 >= 2.17.5 的官方推荐方法:

this.$set(this.columns[foundIndex].filterOptions, 'filterValue', value);

如果您想使用 $route 查询参数来完成:

for (var key in this.$route.query){
    var field = key;
    let foundIndex = this.columns.findIndex((c) => {
      return c.field == field
    });

    if (foundIndex !== -1 ){
      this.$set(this.columns[foundIndex].filterOptions, 'filterValue', this.$route.query[key]);
    }
}

【讨论】:

    猜你喜欢
    • 2020-03-13
    • 1970-01-01
    • 2019-03-31
    • 1970-01-01
    • 2019-09-22
    • 2012-03-15
    • 1970-01-01
    • 2018-12-27
    • 1970-01-01
    相关资源
    最近更新 更多