Vue Tables 2 - 自定义过滤器

新手上路,请多包涵

我正在尝试将此 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 成功回调中但没有。

我想这是非常基本的,我应该知道这一点,但我不知道。因此,如果有人能告诉我将活动巴士放在哪里,工作人员会很棒!

原文由 Verba 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 533
2 个回答

文档可以更好地描述这一点。有点难懂。

您需要导入 vue-tables-2 的命名导出 Event ,因此您拥有表的事件总线并在自定义点击处理程序中发出自定义事件。

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

请查看下面或此 小提琴 中的字母过滤器演示。

该演示类似于您可以在 此处 找到的 vue-tables-1 演示小提琴。

 // 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>

原文由 AWolf 发布,翻译遵循 CC BY-SA 4.0 许可协议

为任何想玩的人 更新了工作小提琴。我仍然不确定如何让它与嵌套的单页组件一起使用。

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

原文由 Kirk Ross 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题