2

我正在使用多选视图:

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

是否可以在“选择”框中预选多个值并以编程方式更改所选值?背景:我想将三个“选择”框设置的不同组合保存为书签。加载书签时,我必须设置“选择”框的值。
谢谢

4

2 回答 2

6

是的。在您的控制器中,您必须创建一个属性以在使用Ember.Select.

在下面的代码中,我将 Greetings 设置为选择框的内容,在列出这些 Greetings 的控制器中(检查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('content.@each'),
    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('content.@each')
});

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());
    }
});
于 2013-03-05T18:25:26.813 回答
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('content.@each'),
  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 的帮助!

也许有人知道如何解决这个问题(参见上面的代码注释)?

于 2013-03-07T21:57:24.890 回答