javascript-如何通过复选框对模型的数组/内容实施多个过滤器
作者:互联网
我正在尝试在同一模型上实现多个过滤器.我要应用过滤器的属性是数组.
//Exam Model
App.Exam = DS.Model.extend({
name: DS.attr('string'),
description: DS.attr('string'),
courses : DS.hasMany('course',{ async: true }),
});
//Course Model
App.Course = DS.Model.extend({
name: DS.attr('string'),
description:DS.attr('string'),
professors: DS.attr(),
subjects: DS.attr(),
languages: DS.attr(),
exam: DS.belongsTo('exam', { async: true })
});
修改模型后,在ExamsExam路线中,提取要应用过滤器的数据.
App.ExamsExamRoute = Ember.Route.extend({
model: function(params) {
return this.store.find('exam', params.exam_id).then(function (exam) {
console.log("found single exam", exam);
return exam;
});
},
afterModel: function(model, transition){
var self = this;
var professorList = [];
var subjectList = [];
var languageList = [];
var promise = new Ember.RSVP.Promise(function(resolve, reject){
var courses = model.get('courses');
courses.forEach(function(course){
self.store.find('course', course.get('id')).then(function(course){
var profs = course.get('professors');
var subjects = course.get('subjects');
var languages = course.get('languages');
profs.forEach(function(prof) {
if (professorList.indexOf(prof) === -1) {
professorList.pushObject(prof);
}
});
subjects.forEach(function(subject) {
if (subjectList.indexOf(subject) === -1) {
subjectList.pushObject(subject);
}
});
languages.forEach(function(language) {
if (languageList.indexOf(language) === -1) {
languageList.pushObject(language);
}
});
});
});
var data = {
professorList: professorList,
subjectList: subjectList,
languageList: languageList
};
resolve(data);
});
promise.then(function(data) {
console.log(data);
model.set('professorNameList', data.professorList);
model.set('subjectList', data.subjectList);
model.set('languageList', data.languageList);
});
}
});
这是我的模板
<script type="text/x-handlebars" data-template-name="exams/exam">
<h2>Exam page</h2>
<div class="row">
<div class="col-md-3 well">
<ul class="list-group well">
{{#each course in model.languageList}}
<li class="">
<label>
{{input type='checkbox'}}
{{course}}
</label>
</li>
{{/each}}
</ul>
<ul class="list-group well">
{{#each prof in model.professorNameList}}
<li class="">
<label>
{{input type='checkbox'}}
{{prof}}
</label>
</li>
{{/each}}
</ul>
<ul class="list-group well">
{{#each subject in model.subjectList}}
<li class="">
<label>
{{input type='checkbox'}}
{{subject}}
</label>
</li>
{{/each}}
</ul>
</div>
<div class="col-md-9">
{{#each course in model.courses}}
<div class="well">
Course name - {{course.name}}<br>
Professors - {{course.professors}}<br>
Subjects - {{course.subjects}}
</div>
{{/each}}
</div>
</div>
</script>
现在,如何更改模型的内容,以便如果用户选择语言过滤器,则仅必须显示属于该所选语言的课程.
另外,如果用户选择语言和主题过滤器,则仅应显示与该条件匹配的过滤器.
关于通过ember中的复选框进行过滤的文档很少.
有人建议/指导我如何解决这个问题并获得一个干净的解决方案.
这是JS BIN DEMO,可以更好地说明我要实现的目标.
解决方法:
以@joostdevries的答案为基础…
将Every()与回调一起使用是一个很好的解决方案,但它“感觉”有些复杂.您要寻找的基本上是数组之间的交叉点.例如,模型中的普通教授既包含一系列选定的教授,也包含多个教授. Ember提供了这样的功能,称为…等待它……交集(请参阅here):).它返回一个包含两个数组共同元素的数组,如果没有共同元素,则返回一个空(长度为0)的数组.
这是使用相交方法的相同的filteredCourses属性.
filteredCourses: function() {
var selectedProfessors = this.get('selectedProfessors'),
selectedLanguages = this.get('selectedLanguages'),
selectedSubjects = this.get('selectedSubjects'),
courses = this.get('model.courses');
var intersectFn = Ember.EnumerableUtils.intersection;
return courses.filter(function(course) {
return intersectFn(course.get('professors') || [], selectedProfessors).length ||
intersectFn(course.get('languages') || [], selectedLanguages).length ||
intersectFn(course.get('subjects') || [], selectedSubjects).length;
});
}.property('selectedProfessors.length', 'selectedLanguages.length', 'selectedSubjects.length')
首先,我们将交集函数别名如下:
var intersectFn = Ember.EnumerableUtils.intersection;
此步骤纯粹是简单的操作-我只是不想每次都键入Ember.EnumerableUtils.intersection;相反,我只想输入intersectFn.然后,我仅使用函数来查看数组是否相交.如果满足,则结果数组的长度将大于0,其值为true;否则为0.否则-长度为0,其值为false.所有这一切的最后一个怪癖是,有时该属性将是未定义的,从而弄乱了相交方法.对于这种情况,我将数组设置为空.
因此,course.get(‘professors’)|| []表示,如果定义了教授的属性(数组),请使用它;否则-使用一个空数组.
工作解决方案here
标签:ember-js,ember-data,javascript 来源: https://codeday.me/bug/20191028/1954713.html