这是在someroute.js中添加路由。同时用教师模型填充主题模型。我只想携带老师姓名和学科名称。如何解决这个问题。
router.get('/add',function(req,res,next){
Subject.find({},{'subname': 1, _id: 0}).populate('teacher_id', { name: 1, _id:0}).exec(function (err,rtn) {
if(err) throw err;
console.log(typeof rtn);
console.log(rtn);
res.render('admin/timetable/time-add',{ title: 'Express', subj: rtn});
});
});
这是用于创建选择选项的添加页面JavaScript
var subject = [<%- subj %>];
console.log(subject);
addOptions('teacher_id', subject);
$("#teacher_id").on('change', function(){
var sub = $.grep( subject, function( n, i ) {
return n.teacher_id == $("#teacher_id").val();
});
addOptions('subname', sub);
});
function addOptions(id, arr){
$("#"+id).children().remove();
for(var idx in arr){
if(idx == 0) $("#"+id).append(new Option('Select '+((id == 'teacher_id')? "Teacher": "Subject"), ''));
if(idx == 0 || arr[idx][id] != arr[idx-1][id]){
$("#"+id).append(new Option(((id == 'name')? arr[idx][id]:arr[idx][id]),arr[idx][id]));
}
}
}