【发布时间】:2018-09-14 22:40:09
【问题描述】:
这与我在这里关于 nousliders 的第一个问题非常相关:How to update div in Meteor without helper?(这个标题选得不好,因为它不是为了避免帮助)
Jankapunkt 提供的答案效果很好,例如我可以有 4 个滑块,并且重新排序不会丢失像这样的 min/max 滑块状态:
现在我希望一些元素是非滑块,例如将 1 更改为下拉菜单:
但是当我单击切换按钮时,1 个滑块消失(移动到下拉点的那个),并且我在控制台中收到错误:
Exception in template helper: Error: noUiSlider (11.1.0): create requires a single element, got: undefined
我不明白为什么添加 if/else 会有什么不同...滑块助手正在等待准备就绪 {{#if ready}}...{{/if}} 所以它应该可以工作?任何人都明白为什么不?以及如何解决?
模板 onCreated 现在看起来像这样:
Template.MyTemplate.onCreated(function() {
const sliders = [{
id: 'slider-a',
prio: 1,
type: "NumberRange",
options: {
start: [0, 100],
range: {
'min': [0],
'max': [100]
},
connect: true
}
}, {
id: 'slider-b',
prio: 2,
type: "NumberRange",
options: {
start: [0, 100],
range: {
'min': [0],
'max': [100]
},
connect: true
}
}, {
id: 'dropdown-c',
prio: 3,
type: "Dropdown"
}, {
id: 'slider-d',
prio: 4,
type: "NumberRange",
options: {
start: [0, 100],
range: {
'min': [0],
'max': [100]
},
connect: true
}
}, ]
const instance = this
instance.state = new ReactiveDict()
instance.state.set('values', {}) // mapping values by sliderId
instance.state.set('sliders', sliders)
})
模板现在看起来像这样,有一个 if else 语句来显示 Dropdown 或 NumberRange(滑块):
<template name="MyTemplate">
{{#each sliders}}
<div class="range">
<div>
<span>id:</span>
<span>{{this.id}}</span>
</div>
{{#if $eq type 'Dropdown'}}
<select id="{{this.id}}" style="width: 200px;">
<option value="">a</option>
<option value="">b</option>
<option value="">c</option>
</select>
{{else if $eq type 'NumberRange'}}
<div id="{{this.id}}">
{{#if ready}}{{slider this}}{{/if}}
</div>
{{/if}}
{{#with values this.id}}
<div>
<span>values: </span>
<span>{{this}}</span>
</div>
{{/with}}
</div>
{{/each}}
<button class="test">Switch Sliders</button>
</template>
【问题讨论】:
标签: javascript meteor nouislider