【问题标题】:Vue Tables 2 - Custom filtersVue 表 2 - 自定义过滤器
【发布时间】:2017-06-06 08:56:03
【问题描述】:

我正在尝试将此 https://github.com/matfish2/vue-tables-2 与 Vue 2.1.8 一起使用。

而且它工作得很好,但是我需要使用自定义过滤器来根据它们的值等来格式化一些字段。

在选项中我有这个:

customFilters: [
{
  name:'count',
  callback: function(row, query) {
    console.log('see me?'); // Not firing this
    return row.count[0] == query;
}
}
]

在文档中说我必须这样做:

Using the event bus:

Event.$emit('vue-tables.filter::count', query);

但我不知道该放在哪里?我尝试了很多地方。例如在我的 axios 成功回调中,但什么都没有。

我想这是非常基本的,我应该知道这一点,但我不知道。所以如果有人能告诉我把那个活动巴士工作人员放在哪里,那就太棒了!

【问题讨论】:

    标签: javascript vue.js vuejs2 vue-tables-2


    【解决方案1】:

    文档可能会更好地描述这一点。有点难理解。

    你需要导入 vue-tables-2 的命名导出Event,这样你就有了表的事件总线,并在你的自定义点击处理程序中发出自定义事件。

    在演示中,它可用于全局对象。在 ES6 中,您将按照文档中的说明将其导入 import {ServerTable, ClientTable, Event} from 'vue-tables-2';

    请查看下方或fiddle 中的字母过滤器演示。

    该演示类似于 vue-tables-1 演示小提琴,您可以找到 here

    // Vue.use(VueTables)
    const ClientTable = VueTables.ClientTable
    const Event = VueTables.Event // import eventbus
    
    console.log(VueTables);
    Vue.use(ClientTable)
    
    new Vue({
      el: "#people",
      methods: {
        applyFilter(letter) {
          this.selectedLetter = letter;
          Event.$emit('vue-tables.filter::alphabet', letter);
        }
      },
      data() {
        return {
          letters: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'],
          selectedLetter: '',
          columns: ['id', 'name', 'age'],
          tableData: [{
            id: 1,
            name: "John",
            age: "20"
          }, {
            id: 2,
            name: "Jane",
            age: "24"
          }, {
            id: 3,
            name: "Susan",
            age: "16"
          }, {
            id: 4,
            name: "Chris",
            age: "55"
          }, {
            id: 5,
            name: "Dan",
            age: "40"
          }],
          options: {
            // see the options API
            customFilters: [{
              name: 'alphabet',
              callback: function(row, query) {
                return row.name[0] == query;
              }
            }]
          }
        }
      }
    });
    #people {
      text-align: center;
      width: 95%;
      margin: 0 auto;
    }
    h2 {
      margin-bottom: 30px;
    }
    th,
    td {
      text-align: left;
    }
    th:nth-child(n+2),
    td:nth-child(n+2) {
      text-align: center;
    }
    thead tr:nth-child(2) th {
      font-weight: normal;
    }
    .VueTables__sort-icon {
      margin-left: 10px;
    }
    .VueTables__dropdown-pagination {
      margin-left: 10px;
    }
    .VueTables__highlight {
      background: yellow;
      font-weight: normal;
    }
    .VueTables__sortable {
      cursor: pointer;
    }
    .VueTables__date-filter {
      border: 1px solid #ccc;
      padding: 6px;
      border-radius: 4px;
      cursor: pointer;
    }
    .VueTables__filter-placeholder {
      color: #aaa;
    }
    .VueTables__list-filter {
      width: 120px;
    }
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.10/vue.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue-tables-2@1.4.70/dist/vue-tables-2.min.js"></script>
    <div id="people">
      <button @click="applyFilter(letter)" class="btn btn-default" v-for="letter in letters" :class="{active: letter==selectedLetter}">
        {{letter}}
      </button>
      <button  class="btn btn-default" @click="applyFilter('')">
      clear
      </button>
      <v-client-table :data="tableData" :columns="columns" :options="options"></v-client-table>
    </div>

    【讨论】:

      【解决方案2】:

      我发现这节课对我有最大的帮助。 https://github.com/ratiw/vuetable-2-tutorial/wiki/lesson-13

      总结: 您应该使用 vue-events 包发出事件或使用 Vuex 计算属性(推荐)。 您想在 vuetable 上使用 :append-params="moreParams",这是 vuetable2 的一个功能,将附加到 api-url 以及分页值(与这些参数分开)。 我正在使用 Vuex,所以我将 moreParams 作为 vuetable 的计算属性。它使用 this.$store.getters.moreParams 这是我的 Vuex getter,因为我有多个搜索字段。这是对来自输入字段处理程序的 Vuex 提交的反应。

          computed: {
            moreParams() {
              return this.$store.getters.moreParams
            },
          },
      

      否则,您可以使用 $store.stage.property。我有一个关于 moreParams 的手表,它使用新查询刷新表:

          watch: {
            moreParams(newVal, oldVal) {
              this.$nextTick(() => {
                this.$refs.vuetable.refresh()
              })
            },
          },
      

      【讨论】:

        【解决方案3】:

        隐藏默认过滤器和每页选择框并定义一个新过滤器“manual_agent”。

                optionsTable: {
                    customFilters: ['manual_agent'],
                    filterable: false,
                    perPageValues: []
                },
        

        隐藏是因为没有“插槽”选项可以在默认过滤器和每页选择之间添加新的自定义过滤器,并且默认过滤器也没有太大的响应性。下面的例子是服务器表的实现。

        全局用于自定义过滤器的方法:

        toggleFilter: function(filterName, $event) {
                        this.$refs.serverTableRef.setPage(1);
                        setTimeout(function () {
                            let searchItem = '';
                            if (typeof $event === 'string') { searchItem = $event; } else { searchItem = $event.target.value; }
                            let table = this.$refs.serverTableRef;
                            table.customQueries[filterName] = searchItem;
                            table.getData();
                        }.bind(this), 1000);
                    }
        

        为此,我们必须在我们的 v-server 表上定义 ref 名称,如下所示:

        <v-server-table ref="serverTableRef"
        

        现在在模板中新增自定义选择框过滤器(v-model 只指向数据中定义的自定义变量)

        <select name="typeoftrip" v-model="agentFilter" @change="toggleFilter('manual_agent', agentFilter)">
        

        自定义过滤器将替换我们通过禁用它而丢失的默认过滤器。 (它使用了“查询”名称,所以我们使用相同的名称)

        <input name="typeoftrip" v-model="mainQuery" v-on:keyup="toggleFilter('query', mainQuery)">
        

        以及我们自己的每页选择的新自定义选择

                    <select v-model="limitFilter"  @change="$refs.serverTableRef.setLimit($event.target.value)" >
                        <option value="10">10</option>
                        <option value="25">25</option>
                        <option value="50">50</option>
                    </select>
        

        【讨论】:

          【解决方案4】:

          Updated working fiddle 适合任何想玩的人。我仍然不确定如何使它与嵌套的单页组件一起使用。

          customFilters: [{
            name: 'alphabet',
            callback: function(row, query) {
              return row.name[0] == query;
            }
          }]
          

          【讨论】:

            猜你喜欢
            • 2020-06-24
            • 1970-01-01
            • 2018-09-16
            • 2020-10-24
            • 2017-06-11
            • 1970-01-01
            • 2017-11-22
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多