简体   繁体   English

通过vue.js中的完全匹配

[英]FilterBy exact match in vue.js

I have a list that I want to filter by an id value: 我有一个列表,希望通过id值进行过滤:

<li v-repeat="release.tracks | filterBy track_format in 'format_id'">

This works pretty nicely unless format_id is, eg, 12, at which point I see all items with track_format 1 and 2 as well. 除非format_id例如为12,否则这将很好地工作,这时我也看到了所有具有track_format 1和2的项目。

Is there a simple way of only showing items where the values constitute an exact match? 有一种简单的方法可以只显示值构成完全匹配的项目吗? I could abandon using numbers, but I feel like I'm going to carry on running into problems with formats like "LP" versus "Deluxe LP". 我可以放弃使用数字,但是我觉得我将继续遇到“ LP”和“ Deluxe LP”格式的问题。

Well, here's a way I just came up with, using a custom filter, that works (I'd still like to know if this kind of thing is the right sort of approach though) : 好吧,这是我使用自定义过滤器提出的一种可行的方法(我仍然想知道这种事情是否是正确的方法)

Vue.filter('matching_format_id', function(value, format_id) {
   return value.filter(function(item) {
       return item.format_id === parseInt(format_id);
   });
});

<li v-repeat="release.tracks | matching_format_id track_format">

ETA: Actually, that didn't work so well, changes to release tracks were not triggering any kind of update in the view. 预计到达时间:实际上,效果不是很好,发布轨道的更改并未触发视图中的任何更新。 Next tack, trying to filter with a computed property: 下一步,尝试使用计算的属性进行过滤:

computed: {
       filteredTracks: function() {
           return this.release.tracks.filter(function (track) {
               return track.format_id === parseInt(vm.track_format);
           });
       }
   },

<li v-repeat="filteredTracks">

Looks promising so far, but that's what I thought about the last idea... 到目前为止看起来很有希望,但这就是我对最后一个想法的看法...

I encountered a similar issue, and wrote a drop-in replacement that does exact matching: 我遇到了类似的问题,并编写了完全匹配的直接替换:

Vue.filter('exactFilterBy', function(array, needle, inKeyword, key) {
    return array.filter(function(item) {
        return item[key] == needle;
    });
});

Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM