2013-03-05 43 views
2

我使用的是多選的看法:ember.js Ember.Select多=真與預選值

{{view Ember.Select 
    multiple="true" 
    contentBinding="App.filtersProductController" 
    selectionBinding="App.filtersController.products" 
    optionLabelPath="content.fullName" 
    optionValuePath="content.id" 
    isVisibleBinding="App.filtersController.productListBox"}} 

是否有可能在「選擇」框預選多個值,並以編程方式更改選定的值?背景:我想將三個「選擇」框設置的不同組合保存爲書籤。加載書籤時,我必須設置「選擇」框的值。
謝謝

回答

6

是的。在您的控制器中,您必須創建一個屬性以在使用Ember.Select時保持所選的一個或多個值。

在下面的代碼我設置問候語爲選擇框的內容,在列出的問候(檢查ApplicationRoute)控制器,我也有一個屬性叫做selectedItems這我結合Select和如果視圖加載時沒有選擇任何項目,我使用其他一些屬性來過濾要預先選擇的值(1和3)。

這將渲染一個多選框,其ID爲1或3的項標記爲選定項。你可以在這裏看到的來源:http://jsfiddle.net/schawaska/Y8P4m/

把手:

<script type="text/x-handlebars"> 
    <h1>Test</h1> 
    {{view Ember.Select 
      multiple="true" 
      selectionBinding="controller.selectedItems" 
      contentBinding="controller" 
      optionLabelPath="content.text" 
      optionValuePath="content.id"}} 
</script> 

的JavaScript:

window.App = Ember.Application.create(); 

App.Store = DS.Store.extend({ 
    revision: 11, 
    adapter: 'DS.FixtureAdapter' 
}); 

App.Greeting = DS.Model.extend({ 
    text: DS.attr('string'), 
    when: DS.attr('date'), 
    selected: false, 
    isSelected: function() { 
     return this.get('selected'); 
    }.property('selected') 
}); 

App.ApplicationController = Em.ArrayController.extend({ 
    preselected: function() { 
     return this.get('content').filter(function(greeting) { 
      return greeting.get('id') == 1 || 
        greeting.get('id') == 3; 
     }); 
    }.property('[email protected]'), 
    selectedItems: function() { 
     if(this.get('selected.length') <= 0) { 
      return this.get('preselected'); 
     } else { 
      return this.get('selected'); 
     } 
    }.property('selected', 'preselected'), 
    selected: function() { 
     return this.get('content').filter(function(greeting) { 
      return greeting.get('isSelected'); 
     }) 
    }.property('[email protected]') 
}); 

App.Greeting.FIXTURES = [ 
    {id: 1, text: 'First', when: '3/4/2013 2:44:52 PM'}, 
    {id: 2, text: 'Second', when: '3/4/2013 2:44:52 PM'}, 
    {id: 3, text: 'Third', when: '3/4/2013 2:44:52 PM'}, 
    {id: 4, text: 'Fourth', when: '3/4/2013 3:44:52 PM'} 
]; 

App.ApplicationRoute = Em.Route.extend({ 
    setupController: function(controller) { 
     controller.set('model', App.Greeting.find()); 
    } 
}); 
+1

太棒了!非常感謝你!!!所以,多重選擇的作品。但是現在我意識到我對簡單(不是多個)元素有同樣的問題。我想以編程方式更改所選元素,但它始終顯示第一個元素,而不管給定的值如何。你能舉個例子嗎?再次感謝。我將列出我的代碼,當整個案件正在工作。 – user1984778 2013-03-06 15:59:05

+0

我已經更新了小提琴,用'selectionBinding'添加了一個選擇。請注意,對於這兩個示例,都可能有些事情需要處理。這是一個概念性的例子,顯示你在哪裏添加代碼來做這樣的事情。 – MilkyWayJoe 2013-03-06 16:44:53

1

我已經創造了單,多 「選擇」 元素一個完整的例子。您可以設置默認值並以編程方式或使用「選擇」GUI元素更改選定值。控制器代碼:

// class for single selects 
App.SingleSelectFilterController = Ember.ArrayController.extend({ 
    selection: null, 
    active: true, 
    update: function(id) { 
    this.set("selection", id); 
    }, 
    getSelectedId: function() { 
    return this.get("selection"); 
    } 
}); 


// class for multi selects 
App.MultiSelectFilterController = Ember.ArrayController.extend({ 
    selection: null, 
    active: true, 
    update: function(selectionIds) { 
    // Workaround: Reinitializing "content". How to do it well? 
    var contentCopy = []; 
    for(i = 0; i < this.get("content").length; i++) { 
     contentCopy.push(this.get("content")[i]); 
    } 
    this.set("content", contentCopy); 
    this.set("selection", selectionIds); 
    }, 
    selected: function() { 
    var me = this; 
    return this.get('content').filter(function(item) { 
     for(i = 0; i < me.get("selection").length; i++) { 
     if(me.get("selection")[i] === item.get('id')) { return true; } 
     } 
     return false; 
    }); 
    }.property('[email protected]'), 
    getSelectedIds: function() { 
    var ids = []; 
    for(i = 0; i < this.get("selected").length; i++) { 
     ids.push(this.get("selected")[i].get("id")); 
    } 
    return ids; 
    } 
}); 


// create single and multi select controllers 
App.metricController = App.SingleSelectFilterController.create(); 
App.metricController.set("content", App.filterData.get("metrics")); 
App.metricController.set("selection", "views"); // set default value for single select element 
App.platformController = App.MultiSelectFilterController.create(); 
App.platformController.set("content", App.filterData.get("platforms")); 
App.platformController.set("selection", ["plat-black"]); // set default value for multi select element 

而完整的例子:
http://jsfiddle.net/7R7tb/2/

感謝MilkyWayJoe他的幫助!

也許有人知道如何解決該解決方法(請參閱上面的代碼註釋)?

相關問題