Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用视图中的复选框过滤Ember.js中ArrayController的内容集合?_Javascript_Ember.js_Ember Data - Fatal编程技术网

Javascript 如何使用视图中的复选框过滤Ember.js中ArrayController的内容集合?

Javascript 如何使用视图中的复选框过滤Ember.js中ArrayController的内容集合?,javascript,ember.js,ember-data,Javascript,Ember.js,Ember Data,我正在为余烬和余烬数据使用最新代码。我有rails作为后端,提供JSON,运行良好。我希望能够使用视图中的复选框筛选余烬模型的“活动”属性。如果选中该复选框,我希望该复选框仅显示活动数据=true。我不想从数组中删除数据,只想隐藏数据。这是我目前拥有的,但它不起作用 型号: App.Org = DS.Model.extend({ code: DS.attr('string', { defaultValue: 'N/A' }), name: DS.attr('string', {

我正在为余烬和余烬数据使用最新代码。我有rails作为后端,提供JSON,运行良好。我希望能够使用视图中的复选框筛选余烬模型的“活动”属性。如果选中该复选框,我希望该复选框仅显示活动数据=true。我不想从数组中删除数据,只想隐藏数据。这是我目前拥有的,但它不起作用

型号:

App.Org = DS.Model.extend({
    code: DS.attr('string', { defaultValue: 'N/A' }),
    name: DS.attr('string', { defaultValue: 'N/A' }),
    source: DS.attr('string', { defaultValue: 'N/A' }),
    status: DS.attr('string', { defaultValue: 'N/A' }),
    type: DS.attr('string', { defaultValue: 'N/A' }),
    note: DS.attr('string', { defaultValue: 'N/A' }),
    financial_Flag: DS.attr('string', { defaultValue: 'N/A' }),
    expense_Flag: DS.attr('string', { defaultValue: 'N/A' }),
    revenue_Flag: DS.attr('string', { defaultValue: 'N/A' }),
    created_At: DS.attr('string', { defaultValue: 'N/A' }),
    updated_At: DS.attr('string', { defaultValue: 'N/A' }),

    active: function() {
        var status = this.get('status');
        var active = (status === 0) ? false : true;
        console.log("status: " + status + " | active: " + active);
        return active;
    }.property('status')

}).reopenClass({
    collectionUrl: '/orgs',
    resourceUrl: '/orgs/%@',
    resourceName: 'org'
});
阵列控制器:

App.OrgsController = Em.ArrayController.extend({

    isEmpty: function() {
        console.log("############ App.OrgsController.isEmpty called");
        return this.get('length') === 0;
    }.property('length'),

    toggleActive: function(){
        console.log("############ App.OrgsController.isActive called");
        return this.filterProperty('active', value).forEach(this.removeObject, this);
    }.property('@each.active'),

    init: function() {
        this.set('content', App.store.findAll(App.Org));
    },

    refreshOrgs: function() {
        this.set('content', App.store.findAll(App.Org));
    },

    getInactiveOrgs: function(){
        this.set('content', App.store.find(App.Org, {status: "0"}));
    }
});
我认为:

<label>
    isActive
    {{view Ember.Checkbox checkedBinding="App.OrgsController.toggleActive" disabledBinding="App.OrgsController.isEmpty" }}
</label>

活跃的
{{view Ember.Checkbox checkedBinding=“App.OrgsController.toggleActive”disabledBinding=“App.OrgsController.isEmpty”}

这可以通过创建一个计算属性来实现,该属性基于绑定到复选框的布尔值返回整个集合或仅返回项目的子集。比如说,

App.Post = DS.Model.extend({
    text: DS.attr('string'),
    active: DS.attr('boolean')
});

App.IndexRoute = Ember.Route.extend({
    model: function() {
        return App.Post.find();
    }
});

App.IndexController = Ember.ArrayController.extend({
    hideInactive: false,

    filteredContent: function() {
        var content = this.get('content');

        if (!content || !this.get('hideInactive'))
            return content;

        return content.filter(function(item) {
            return item.get('active');
        });
    }.property('content.isLoaded', 'hideInactive')
});
您的模板应如下所示:

<script type="text/x-handlebars" data-template-name="index">
    <p>{{view Ember.Checkbox checkedBinding="hideInactive"}} Hide Inactive Posts</p>
    <br/>

    {{#each filteredContent}}
        <p>{{text}}</p>
    {{/each}}
</script>

{{view Ember.Checkbox checkedBinding=“hideInactive”}隐藏非活动帖子


{{{#每个filteredContent} {{text}}

{{/每个}}

如果将
active
定义为一个计算属性(如您的示例中所示),这也会起作用。

不确定它是否有用,但由于您使用的是余烬数据,可能您希望使用而不是控制器的filterProperty`?嗯……我来试一试。谢谢