![](/img/trans.png)
[英]Javascript - How to search no matter upperCase or lowercase in Vue.js?
[英]Vue.js orderBy does not work properly with uppercase and lowercase
我正在使用Laravel 5.2 + Vuejs + Vueify開發一個應用程序,並且在表上列出了一個對象數組,但是有兩個問題。
1. orderBy可以用大寫和小寫分隔
orderBy與記錄分開應用,其中第一個字母為大寫和小寫!
2.過濾器領域的特殊字符
由於字母A上的重音,在過濾器字段上鍵入“ Água ”找不到“ Agua”結果,我想忽略該重音...這可能嗎?
JS文件
Vue.filter('pmlOrderBy', function(arr, sortKey, reverse) {
if (!sortKey) {
return arr;
}
var order = (reverse && reverse < 0) ? -1 : 1;
// sort on a copy to avoid mutating original array
return arr.slice().sort(function(a, b) {
if (sortKey !== '$key') {
if (Vue.util.isObject(a) && '$value' in a) a = a.$value;
if (Vue.util.isObject(b) && '$value' in b) b = b.$value;
}
a = Vue.util.isObject(a) ? Vue.parsers.path.getPath(a, sortKey) : a;
b = Vue.util.isObject(b) ? Vue.parsers.path.getPath(b, sortKey) : b;
a = a.toLowerCase();
b = b.toLowerCase();
// return a.localeCompare(b) * order;
return a === b ? 0 : a > b ? order : -order;
});
});
new Vue({
el: 'body',
data: {
record: {},
selected: [],
list: [{
name: 'Google',
id: 1,
}, {
name: 'Água',
id: 2,
}, {
name: 'Agua Branca',
id: 3,
}, {
name: 'first time',
id: 4,
}, {
name: 'boston',
id: 5,
}, {
name: 'Type',
id: 6,
}, {
name: 'Facebook',
id: 7,
}, ],
sortProperty: 'name',
sortDirection: 1,
},
methods: {
sort: function(property) {
this.sortProperty = property;
this.sortDirection = (this.sortDirection == 1) ? -1 : 1;
},
}
});
HTML文件
<div class="container">
<input type="text" v-model="textFilter" class="form-control" placeholder="Type to filter...">
</div>
<hr>
<div class="container">
<div class="alert alert-info">Click at the TH to sort</div>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th @click="sort('id')" style="cursor: pointer;">Id</th>
<th @click="sort('name')" style="cursor: pointer;">Name</th>
</tr>
</thead>
<tbody>
<tr v-for="r in list | filterBy textFilter | pmlOrderBy sortProperty sortDirection">
<td class="text-center" style="width:90px">{{ r.id }}</td>
<td>{{ r.name }}</td>
</tr>
</tbody>
</table>
</div>
====編輯====
當泰勒(Taylor)和瑞寧鏈(RainningChain)幫助我吼叫時,我們快到了!
我找到了這篇文章並更新了上面的代碼和JS Bin,但是現在的問題是:
任何人?
謝謝!
為此,您必須創建自己的過濾器。
擴展Vue的orderBy
過濾器,這將是對您兩個問題的實用解決方案。
// This was originally copied from the Vue source
// File: src/filters/array-filters.js
function orderByWords (arr, sortKey, reverse) {
arr = convertArray(arr)
if (!sortKey) {
return arr
}
var order = (reverse && reverse < 0) ? -1 : 1
// sort on a copy to avoid mutating original array
return arr.slice().sort(function (a, b) {
if (sortKey !== '$key') {
if (isObject(a) && '$value' in a) a = a.$value
if (isObject(b) && '$value' in b) b = b.$value
}
a = isObject(a) ? getPath(a, sortKey) : a
b = isObject(b) ? getPath(b, sortKey) : b
return a.localeCompare(b) * order
})
}
篩選器的內容如下所示: a.localeCompare(b)
String.prototype.localeCompare
方法比較兩個字符串,並根據初始字符串( a
)在比較字符串( b
)之前還是之后,返回一個整數值。
更新
事實證明,由於Number.prototype.localeCompare
不存在而Number.prototype.localeCompare
篩選器損壞了……誰知道?
因此,我們可以使用一些類型轉換技巧來使它適用於任何事物。
Vue.filter('pmlOrderBy', function (arr, sortKey, reverse) {
if (!sortKey) {
return arr;
}
var order = (reverse && reverse < 0) ? -1 : 1;
// sort on a copy to avoid mutating original array
return arr.slice().sort(function (a, b) {
if (sortKey !== '$key') {
if (Vue.util.isObject(a) && '$value' in a) a = a.$value;
if (Vue.util.isObject(b) && '$value' in b) b = b.$value;
}
a = Vue.util.isObject(a) ? Vue.parsers.path.getPath(a, sortKey) : a;
b = Vue.util.isObject(b) ? Vue.parsers.path.getPath(b, sortKey) : b;
return (''+a).localeCompare((''+b)) * order;
});
});
關鍵行是過濾器的最后一行。 (''+a).localeCompare
將a
強制轉換為String
,然后調用localeCompare
方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.