0

我刚刚开始了解Mustache,但我不明白我该怎么做:

  • 我有这个JSON对象:

     var columnsDefinition = {
    
        "columns": [
              {
                  "header": 'First name',
                  "values": ['John', 'Jack', 'Abe', 'Adelle', 'Jim', 'Andrew', 'Matthew'],
                  "type": 'text'
              },
              {
                  "header": 'Last name',
                  "values": ['Carpenter', 'Reaper', 'Lincoln', 'Aidan', 'Raynor', 'Doe'],
                  "type": 'text'
              },
              {
                  "header": 'Profession',
                  "values": ['butcher', 'doctor', 'farmer', '', 'pilot', 'singer', ''],
                  "type": 'select'
              },
              {
                  "header": 'Employed',
                  "values": [true, false, true, true, false],
                  "type": 'checkbox'
              }
        ],
        "search": true
    };
    
  • 我想创建这个

这是我尝试过的,但我不知道如何放在 values每一列,而不是每一行。

    this.testDiv = $("#testDiv");


    this.header = "{{#columns}}<th>{{header}}</th>{{/columns}}";

    this.body = "{{#columns}}<tr>{{#values}}<td>{{.}}</td><{{/values}}/tr>{{/columns}}";


    this.html = Mustache.to_html(this.header, this.columnsDefinition);

    this.html2 = Mustache.to_html(this.body, this.columnsDefinition);

    $("#testDiv table thead tr").append(this.html);

    $("#testDiv table tbody").append(this.html2);

如何创建上面的表格?谢谢你。

4

1 回答 1

1

我不相信这可以开箱即用。

如果您无法修改当前拥有的 JSON 对象,则最好生成另一个 JSON 对象,该对象将旋转数据并将该对象提供给 Mustache。

于 2013-07-25T23:15:22.673 回答