2013-07-15 56 views
0

我剛開始學習Mustache,但我不明白我怎麼可以這樣做:與鬍鬚JS創建表

  • 我有這個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

這是我嘗試過,但我不知道如何在每列上放置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); 

如何創建上表?謝謝。

回答

1

我不認爲這可以開箱即用。

如果您無法修改您當前擁有的JSON對象,那麼最好生成另一個JSON對象,該對象將數據透視並將其提供給Mustache。