23

可以用 Mustache.js 做到这一点吗?

var data = {"val":"3"},
    template = '<select>' +
                    '<option value="1">1</option>' +
                    '<option value="2">2</option>' +
                    '<option value="3">3</option>' +
                '</select>';

var html = Mustache.to_html(template, data);

$(html).appendTo('body');
4

4 回答 4

34

val属性不起作用,因为 a从具有该属性<select>的 s 中获取其值。我对 Mustache 不是很熟悉,但这应该可行:<option>selected

// snip...        
var html = Mustache.to_html(template, data);
$(html)
    .find('option[value=3]').attr('selected', true)
    .end().appendTo('body');

我认为您使用的模板不是惯用的 Mustache——它的粒度太粗了;你实际上并没有模板化任何东西。像这样的东西可能更像小胡子:

var template = '<select>{{#options}}' +
                   '<option value="{{val}}" {{#sel}}selected{{/sel}}>' + 
                       '{{txt}}' + 
                   '</option>' + 
               '{{/options}}</select>',

    data = {options: [
        {val: 1, txt: 'uno'},
        {val: 2, txt: 'dos'},
        {val: 3, txt: 'tres', sel: true}
    ]};

var html = Mustache.to_html(template, data);

$(html).appendTo('body');

演示 →

于 2011-04-01T21:28:48.540 回答
13

如果您不想在之后修改数组或 DOM 树,您可以使用一个函数:

var selval=3; // select 3
var template = '<select>{{#options}}' +
                   '<option value="{{val}}" {{selected}}>{{txt}}</option>' + 
               '{{/options}}</select>';
var data={
    options: [
        {val: 1, txt: 'one'},
        {val: 2, txt: 'two'},
        {val: 3, txt: 'three'}
    ],
    selected: function() {
         if (this.val==selval) return "selected";
         return "";
    }
};


var html = Mustache.to_html(template, data);
于 2013-08-17T21:59:49.193 回答
3

我使用这个技巧来保持简单:

buildOptions = function(object) {
    for (var i in object) {
        object[i + '=' + object[i]] = true;
    }
    return object;
}

通过这种方式,您可以转换这种数据:

{
    field1: 'abc',
    field2: 'xyz' 
}

使用这种胡须模板:

<select name="field1">
    <option {{#field1=abc}}selected{{/field1=abc}}>abc</option>
    <option {{#field1=def}}selected{{/field1=def}}>def</option>
    <option {{#field1=ghi}}selected{{/field1=ghi}}>ghi</option>
</select>
<select name="field2">
    <option {{#field2=uvw}}selected{{/field2=uvw}}>uvw</option>
    <option {{#field2=xyz}}selected{{/field2=xyz}}>xyz</option>
</select>

像这样:

html = Mustache.to_html(template, buildOptions(data));

Which still really easy to read! The only caveat is that you can't have a '.' in your values.

于 2014-07-06T13:18:24.503 回答
2

我有点晚了,我知道,仅供参考:

<script>

var templates = {
    'dropbox': '{{#options}}{{>option}}{{/options}}',
    'option': '<option value="{{value}}"{{#selected}} selected="selected"{{/selected}}>{{text}}</option>'
};

var data = {
    'options': [
        { 'value': '1', 'text': 'One' },
        { 'value': '2', 'text': 'Two', 'selected': true },
        { 'value': '3', 'text': 'Three' }
    ]
};

$('#number').append(Mustache.render(templates.dropbox, data, templates));

</script>

<select id='number' name='number'>
    <option value="0">Choose a number</option>
</select>
于 2013-06-25T21:10:18.597 回答