jsrender 使用已选择的选项填充多个选择框
jsrender populate selectbox multiple with already selected options
我想通过 Jsrender 填充 select 框。
代码正确填充了 select 框,但默认情况下我无法 select 几个值。我为此创建了一个 Jsrender 辅助函数,但我认为循环内无法访问其他值,所以这就是该函数不起作用的原因。
这里是link到jsfiddle
我的 html 是
<div class="select_placeholder"></div>
<script id="selectbox" type="text/x-jsrender">
<select multiple="multiple" name="">
{{for cats }}
{{include tmpl="#selectbox_options"/}}
{{/for}}
</select>
</script>
<script id="selectbox_options" type="text/x-jsrender" >
<option value="{{:term_id}}" {{>~selected(term_id, selected)}} >{{:name}}</option>
</script>
我的 javascript 是
$.views.helpers({
selected: function(needle,haystack) {
console.log(haystack);
if(typeof haystack === "undefined" || haystack === null) {
return;
}
var count=haystack.length;
for(var i=0;i<count;i++){
if(haystack[i]===needle){return 'selected="selected"'; }
}
return;
}
});
var cates = [
{'term_id' : 1, 'name' : 'category 1'},
{'term_id' : 2, 'name' : 'category 2'},
{'term_id' : 3, 'name' : 'category 3'},
{'term_id' : 4, 'name' : 'category 4'},
{'term_id' : 5, 'name' : 'category 5'},
];
var selected = [{'term_id' : 3}, {'term_id' : 4}];
$( ".select_placeholder" ).html(
$( "#selectbox" ).render({cats : cates,selected:selected})
);
尝试合并数据以供查看https://jsfiddle.net/fksj52c6/5/
var cates = [
{'term_id' : 1, 'name' : 'category 1'},
{'term_id' : 2, 'name' : 'category 2'},
{'term_id' : 3, 'name' : 'category 3'},
{'term_id' : 4, 'name' : 'category 4'},
{'term_id' : 5, 'name' : 'category 5'},
];
var selected = [{'term_id' : 3}, {'term_id' : 4}];
for(var i = 0; i< selected.length; i++) {
for(var j = 0; j < cates.length; j++)
if(selected[i].term_id === cats[j].term_id) {
cats[j].selected = true;
}
}
$( ".select_placeholder" ).html(
$( "#selectbox" ).render({cats : cates})
);
html
<div class="select_placeholder"></div>
<script id="selectbox" type="text/x-jsrender">
<select multiple="multiple" name="">
{{for cats }}
<option value="{{:term_id}}" {{if selected}}selected="selected"{{/if}} >{{:name}}</option>
{{/for}}
</select>
</script>
我想通过 Jsrender 填充 select 框。
代码正确填充了 select 框,但默认情况下我无法 select 几个值。我为此创建了一个 Jsrender 辅助函数,但我认为循环内无法访问其他值,所以这就是该函数不起作用的原因。
这里是link到jsfiddle
我的 html 是
<div class="select_placeholder"></div>
<script id="selectbox" type="text/x-jsrender">
<select multiple="multiple" name="">
{{for cats }}
{{include tmpl="#selectbox_options"/}}
{{/for}}
</select>
</script>
<script id="selectbox_options" type="text/x-jsrender" >
<option value="{{:term_id}}" {{>~selected(term_id, selected)}} >{{:name}}</option>
</script>
我的 javascript 是
$.views.helpers({
selected: function(needle,haystack) {
console.log(haystack);
if(typeof haystack === "undefined" || haystack === null) {
return;
}
var count=haystack.length;
for(var i=0;i<count;i++){
if(haystack[i]===needle){return 'selected="selected"'; }
}
return;
}
});
var cates = [
{'term_id' : 1, 'name' : 'category 1'},
{'term_id' : 2, 'name' : 'category 2'},
{'term_id' : 3, 'name' : 'category 3'},
{'term_id' : 4, 'name' : 'category 4'},
{'term_id' : 5, 'name' : 'category 5'},
];
var selected = [{'term_id' : 3}, {'term_id' : 4}];
$( ".select_placeholder" ).html(
$( "#selectbox" ).render({cats : cates,selected:selected})
);
尝试合并数据以供查看https://jsfiddle.net/fksj52c6/5/
var cates = [
{'term_id' : 1, 'name' : 'category 1'},
{'term_id' : 2, 'name' : 'category 2'},
{'term_id' : 3, 'name' : 'category 3'},
{'term_id' : 4, 'name' : 'category 4'},
{'term_id' : 5, 'name' : 'category 5'},
];
var selected = [{'term_id' : 3}, {'term_id' : 4}];
for(var i = 0; i< selected.length; i++) {
for(var j = 0; j < cates.length; j++)
if(selected[i].term_id === cats[j].term_id) {
cats[j].selected = true;
}
}
$( ".select_placeholder" ).html(
$( "#selectbox" ).render({cats : cates})
);
html
<div class="select_placeholder"></div>
<script id="selectbox" type="text/x-jsrender">
<select multiple="multiple" name="">
{{for cats }}
<option value="{{:term_id}}" {{if selected}}selected="selected"{{/if}} >{{:name}}</option>
{{/for}}
</select>
</script>