2014-12-03 83 views
0

目前我所擁有的是差不多的工作。我正在使用以下內容:如何通過骨幹中的多個選擇選項進行過濾

this.collection.reset(jobArray, {silent: true}); 

var filterDepartment = this.filterDepartment; 
var filterCategory = this.filterCategory; 
var filterLocation = this.filterLocation; 

var filteredResult = this.collection.where({ 
    department: filterDepartment, 
    category: filterCategory, 
    location: filterLocation 
}); 

this.collection.reset(filteredResult); 

如果我選擇部門,類別和位置,此功能非常有用。但是,如果我只想按部門+位置過濾,將類別作爲默認的「全部」值,則應在所有類別中返回已過濾/選定的部門和位置。此外,如果我選擇一個類別,則選擇「全部」,然後只能按位置搜索。

我有辦法做到這一點,但它是很多代碼,當然有更簡單的方法嗎?

回答

0

如何

this.collection.reset(jobArray, {silent: true}); 

var filterDepartment = this.filterDepartment; 
var filterCategory = this.filterCategory; 
var filterLocation = this.filterLocation; 

var filter = {}; 
if (filterDepartment) 
    filter['department'] = filterDepartment 

if (filterCategory) 
    filter['category'] = filterCategory; 

if (filterLocation) 
    filter['location'] = filterLocation; 

console.log(filter); // Dump the filter 

var filteredResult = this.collection.where(filter); 

this.collection.reset(filteredResult); 
+0

這是主要工作,謝謝!現在唯一的問題是,一旦我選擇了一個選項,然後選擇默認的「全部」選項,一切都隱藏了。我試着將你的代碼改爲: if((filterDepartment)&&(filterDepartment!='all')) – emily 2014-12-03 22:53:50

+0

if(filterDepartment && filterDepartment!='all')'對我來說聽起來正確。你確定它是「全部」(而不是全部或全部)嗎?也嘗試在分配過濾器後轉儲過濾器,並查看它是否正確(更新後的問題)。 – 2014-12-03 23:14:40

+0

它正在工作,但我必須在條件語句中包裝整個事情。謝謝你的幫助! [這是最終的代碼](http://stackoverflow.com/a/27283544/965940) – emily 2014-12-03 23:23:29

0

使用Khang Nguen代碼並添加條件語句解決,最終的代碼如下:

if ((!this.filterDepartment) && (!this.filterCategory) && (!this.filterLocation)) { 
    this.collection.reset(jobArray); 
} else { 
    this.collection.reset(jobArray, {silent: true}); 

    var filterDepartment = this.filterDepartment; 
    var filterCategory = this.filterCategory; 
    var filterLocation = this.filterLocation; 

    var filter = {}; 
    if (filterDepartment) { 
     filter['department'] = filterDepartment; 
    } 

    if (filterCategory) { 
     filter['category'] = filterCategory; 
    } 

    if (filterLocation) { 
     filter['location'] = filterLocation; 
    } 

    var filteredResult = this.collection.where(filter); 

    this.collection.reset(filteredResult); 
} 

也從默認的選擇選項中刪除了「所有」值。