我有electoralUnits列表,如果在其他名为StateResult的集合中添加了electoralUnit的id,我需要在该列表中禁用一些。
路线:
router.get('/', (req, res) => {
StateResult.find({})
.then(stateResults => {
ElectoralUnit.find({})
.then(electoralUnits => {
StateList.find({})
.then(stateLists => {
res.render('home/results/stateResults', {
stateResults: stateResults,
electoralUnits: electoralUnits,
stateLists: stateLists
});
});
});
});
});
现在,我用if helper:
在把手中尝试了这个<select name="electoralUnit" multiple class="form-control" id="exampleFormControlSelect2" size="40">
{{#each electoralUnits}}
<option value="{{id}}" {{#each stateResults}} {{#if this}} disabled {{/if}} {{/each}}>{{name}}</option>
{{/each}}
</select>
以及其中的很多变体,例如:
<option value="{{id}}" {{#each stateResults}} {{#if electoralUnit}} disabled {{/if}} {{/each}}>{{name}}</option>
但没有任何作用。我哪里错了?
这是来自mongodb的数据: electoralunits集合
{"_id":"5ab906612f30fe23dc592591","town":"5ab903952e9dc70408a81e32","name":"1. МЗ Аеродром - Дом Здравља","__v":0,"electoralNumber":4200,"safeVoter":360,"date":"2018-04-25T15:19:37.900Z"}
stateresults collection
{"_id":"5ac4e01d46fa2b21280bd981","electoralUnit":"5ab906612f30fe23dc592591","allVotes":100,"validVotes":90,"invalidVotes":10,"partyVotes":[50,10,10,10,5,5],"__v":0}
我尝试用@doowb用自定义把手帮助器解释我:
includes: function(arr, prop, val, options) {
const matches = arr.map(item => item[prop]).includes(val);
if (matches) {
return options.fn(this);
} else {
return options.inverse(this);
}
}
<select name="electoralUnit" multiple class="form-control" id="exampleFormControlSelect2" size="40">
{{#each electoralUnits}}
<option value="{{this._id}}" {{#includes ../stateResults "electoralUnit" this._id}} hidden {{/includes}}>{{name}}</option>
{{/each}}
</select>
但这也行不通。
答案 0 :(得分:1)
如果您显示stateResults
,electoralUnits
和stateLists
对象的样子会有所帮助,但我认为您的问题在于了解您目前正在尝试哪种“深度”从中访问数据。在快速中间件中,您将所有这些对象传递给对象根目录下的模板:
{
stateResults: [], // assuming this is an array
electoralUnits: [], // assuming this is an array
stateLists: [] // assuming this is an array
}
在{{#each stateResults}}
中,您需要使用父指定符:{{#each ../stateResults}}
,因为第一个{{#each}}
块创建了一个新的深度。此外,数据对象的逻辑似乎不正确,因为您不需要向disabled
标记添加多个option
属性。如果您发布实际数据对象的外观,我将使用其他信息编辑此答案。
编辑:获取更多信息后,我认为使用{{pluck}}
和{{inArray}}
帮助器以及如下所示的子表达式将实现您的目标。如果../stateResults
确实有效,请尝试@root.stateResults
(我仍然假设返回的集合是那些mongodb对象的数组):
<select name="electoralUnit" multiple class="form-control" id="exampleFormControlSelect2" size="40">
{{#each electoralUnits}}
<option value="{{this._id}}" {{#inArray (pluck ../stateResults "electoralUnit") this._id}} disabled {{/inArray}}>{{name}}</option>
{{/each}}
</select>
如果您不想使用handlebars-helpers
库(在下面的评论中指出),您可以创建自己的帮助者:
Handlebars.registerHelper('includes', function(arr, prop, val, options) {
const matches = arr.map(item => item[prop]).includes(val);
if (matches) {
return options.fn(this);
} else {
return options.inverse(this);
});
然后你可以像这样使用那个助手:
<select name="electoralUnit" multiple class="form-control" id="exampleFormControlSelect2" size="40">
{{#each electoralUnits}}
<option value="{{this._id}}" {{#includes ../stateResults "electoralUnit" this._id}} disabled {{/includes}}>{{name}}</option>
{{/each}}
</select>