Javascript 使用mustachejs创建表

Javascript 使用mustachejs创建表,javascript,jquery,html,mustache,Javascript,Jquery,Html,Mustache,我刚刚开始学习胡须,但我不明白如何才能做到这一点: 我有一个JSON对象: var columnsDefinition = { "columns": [ { "header": 'First name', "values": ['John', 'Jack', 'Abe', 'Adelle', 'Jim', 'Andrew', 'Matthew'], "type": 'text'

我刚刚开始学习
胡须
,但我不明白如何才能做到这一点:

  • 我有一个
    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
    };
    
  • 我想创建:

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

    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);
this.testDiv=$(“#testDiv”);
this.header=“{{{#columns}}{{{header}}{{/columns}}”;
this.body=“{{{{}列}}{{{{}}{{.}}{{/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);

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

我不相信这是现成的

如果您无法修改当前拥有的JSON对象,那么最好生成另一个JSON对象,该对象将以数据为轴心,并将该对象提供给Mustach