2011-03-20 33 views
1

我有這應該生成一個HTML表的多維JSON。但就目前而言,唯一被寫入的東西是tr,沒有td。JSON與jQuery模板

我知道那裏已經有類似的問題title,但是用法有很大的不同。

從我可以告訴不同的是,我使用的模板方法寫我的模板,而不是宣稱每一個作爲自己的腳本...

這是解析(JSON)陣列


[ 
    [ 
     { 
     "name": "Morning meeting.", 
     "description": "The morning business meeting hosted at Apple.", 
     "image": "302632084.png", 
     "rating": "1.5", 
     "category": "4" 
     }, 
     { 
     "name": "Winning", 
     "description": "", 
     "image": "321752348.png", 
     "rating": "5.0", 
     "category": "3" 
     }, 
     { 
     "name": "1234566890abcdefghijklmnopqrstuvwxyz", 
     "description": "", 
     "image": "316892896.png", 
     "rating": "3.0", 
     "category": "16" 
     } 
    ], 
    [ 
     { 
     "name": "Kristian Lunch", 
     "description": "Having a lunch break.", 
     "image": "320196094.png", 
     "rating": "3.0", 
     "category": "8" 
     }, 
     { 
     "name": "Dropping the kids off at the pool.", 
     "description": "A daly lesson on taking the kids to the pool.", 
     "image": "302658031.png", 
     "rating": "5.0", 
     "category": "4" 
     }, 
     { 
     "name": "Dropping the kids off at the pool.", 
     "description": "A daly lesson on taking the kids to the pool.", 
     "image": "302658031.png", 
     "rating": "5.0", 
     "category": "4" 
     } 
    ] 
] 

這是我的方法是應該寫入數據


EventsView.prototype.writeGrid = function(events) 
{ 
    var gridRows = "<td class='gridRow'>${name}</td>";  
    $.template("gridRows", gridRows); 
    var markup = "<tr class='gridCol'>{{events, gridRows}}</tr>"; 
    $.template("gridCols", markup); 
    $.tmpl("gridCols", events) 
     .appendTo("#gridBody"); 
} 

截至目前,這是產生

的HTML

<tbody id="gridBody"> 
    <tr class="gridCol"> 
    </tr> 
    <tr class="gridCol"> 
    </tr> 
</tbody> 

這是我想要的HTML ...


<tbody id="gridBody"> 
    <tr class="gridCol"> 
     <td class="gridRow">Morning meeting.</td> 
     <td class="gridRow">Winning</td> 
     <td class="gridRow">1234566890abcdefghijklmnopqrstuvwxyz</td> 
    </tr> 
    <tr class="gridCol"> 
     <td class="gridRow">Kristian Lunch</td> 
     <td class="gridRow">Dropping the kids off at the pool.</td> 
     <td class="gridRow">Dropping the kids off at the pool.</td> 
    </tr> 
</tbody> 

回答

1

JSON是你的朋友,更整潔比多維數組...

http://www.json.org/js.html

https://github.com/douglascrockford/JSON-js

+0

我我正在使用json .... – Peter 2011-03-20 23:00:07

+0

你使用json2.js嗎?如果是這樣,你可以請一個jsFiddle,我會爲你找找嗎?我很高興能夠幫助解決這個問題,不久前我做了一個類似的模板系統... :-) – Mitch 2011-03-20 23:34:58

+0

我不太確定我使用的是什麼json。我把它貼在jsfiddle上..感謝您的幫助! http://jsfiddle.net/YHQAa/ – Peter 2011-03-21 00:07:58