我正在使用Ember從API中檢索JSON數據以插入到表中。但是,我的記錄顯示在一個單獨的tr元素中,而不是每個記錄顯示在單獨的tr元素中。有誰知道如何設置?我一直在看Ember文檔。我使用Ember 0.9.8.1。如何用Ember 0.9.8.1重新格式化tr元素?
HTML(在JADE):
script(type="text/x-handlebars")
{{#view Cashier.transactionRowView}}
table
thead
tr
th Date/Time
th User ID
th Amount
th Date/Time
th User ID
th Amount
{{#each Cashier.transactionsController}}
<td>{{datetime}}</td>
<td>{{userId}}</td>
<td>{{amount}}</td>
<td>{{console.datetime}}</td>
<td>{{console.userId}}</td>
<td>{{console.amount}}</td>
{{/each}}
{{/view}}
APP的CoffeeScript:
Cashier = Ember.Application.create(
ready: ->
console.log "Cashier app loaded"
)
MODEL的CoffeeScript:
Cashier.Transaction = Ember.Object.extend(
id: null
datetime: null
type: null
userId: null
amount: null
)
VIEW的CoffeeScript:
Cashier.transactionRowView = Em.View.extend({
tagName: 'tr'
templateName: 'cashier-transactions'
});
控制器的CoffeeScript:
Cashier.transactionsController = Ember.ArrayController.create(
content: []
resourceUrl: 'http://localhost:8080/prepaid/cashiertransactions'
loadTransactions: ->
self = this
url = this.resourceUrl
$.getJSON url,
cashierId: "[email protected]"
period: "3"
, (data) ->
transactions = data.transactions
$(transactions).each (index, value) ->
t = Cashier.Transaction.create(
id : value.id
datetime : value.datetime
type : value.type
userId : value.userId
amount : value.amount
)
self.pushObject Cashier.Transaction.create(t)
)
樣品JSON從服務器:
{
"status": "OK",
"transactions": [
{
"amount": 22,
"datetime": 1348137916873,
"id": "CSH37916873",
"type": "TOP-UP: [email protected]; PAYMENT METHOD: undefined",
"userId": "[email protected]"
},
{
"amount": 222,
"datetime": 1348142501961,
"id": "CSH42501961",
"type": "TOP-UP: [email protected]; PAYMENT METHOD: undefined",
"userId": "[email protected]"
},
{
"amount": 48,
"datetime": 1348550184793,
"id": "CSH50184793",
"type": "TOP-UP: [email protected]; PAYMENT METHOD: undefined",
"userId": "[email protected]"
},
{
"amount": 20,
"datetime": 1348550386661,
"id": "CSH50386661",
"type": "TOP-UP: [email protected]; PAYMENT METHOD: undefined",
"userId": "[email protected]"
},
{
"amount": 1800000003000,
"datetime": 1348657215712,
"id": "CSH57215712",
"type": "DEDUCT: [email protected] - 3GB Data Plan",
"userId": "[email protected]"
}
]
}
This works。謝謝。我對你的答案做了一個小修改,以避免重複的tbody元素。 –
我也不需要在我的視圖中用這個定義tagName和templateName。 –