我想在車手模板中使用自動編號1,2,3來填充路線的模型,而不是像圖中那樣使用自動生成的數據庫標識,因爲當記錄被刪除時,這些問題會變得很糟糕,問題是@index在句柄中不再受支持。Ember.js - 在表格(車把模板)中填充模型時使用自動索引
我的路線:
App.PostsRoute = Ember.Route.extend({
model: function() {
return this.store.find('post');
}
);
我的控制器:
App.PostsController = Ember.ObjectController.extend({
actions: {
delete: function(post) {
post.deleteRecord();
post.get('isDeleted'); // => true
post.save(); // => DELETE to /posts/1
},
adNewPost: function() {
this.transitionToRoute('new');
}
}
});
我的模型和RESTAdapter
App.Post = DS.Model.extend({
postId: DS.attr('string'),
title: DS.attr('string'),
author: DS.attr('string'),
body: DS.attr('string')
});
App.ApplicationAdapter = DS.RESTAdapter.extend({
namespace: 'emberpostsrest/api'
});
我的模板:
<script type="text/x-handlebars" id="posts">
<h1>Posts List</h1>
<button {{action 'addNewPost' }}>Add New Posts</button>
<p></p>
<table border="1">
<tr>
<th scope="col">Index</th>
<th scope="col">Title</th>
<th scope="col">Author</th>
<th scope="col">Body</th>
<th scope="col">Delete</th>
</tr>
{{#each model}}
<tr>
<td>
{{#link-to 'post' this}}
{{id}} //<---- HOW TO ADD AN AUTO INDEX HELPER HERE (1,2,3, ETC
// AND NOT USING MODEL'S ID
{{/link-to}}
</td>
<td>{{title}}</td>
<td>{{author}}</td>
<td>{{body}}</td>
<td><a href="" {{action 'delete' this}}>X</a></td>
</tr>
{{/each}}
</table>
<div>
{{outlet}}
</div>
使用答案修訂的建議:d
的CSS
table {
counter-reset: id;
}
.id-column:before {
counter-increment: id;
content: counter(id);
}
模板
<script type="text/x-handlebars" id="posts">
<h1>Posts List</h1>
<button {{action 'addNewPost' }}>Add New Posts</button>
<p></p>
<table border="1">
<tr>
<th scope="col">Index</th>
<th scope="col">Title</th>
<th scope="col">Author</th>
<th scope="col">Body</th>
<th scope="col">Delete</th>
</tr>
{{#each}}
<tr>
<td class="id-column">
</td>
<td>
{{#link-to 'post' this}}
{{title}}
{{/link-to}}
</td>
<td>{{author}}</td>
<td>{{body}}</td>
<td><a href="" {{action 'delete' this}}>X</a></td>
</tr>
{{/each}}
</table>
<div>
{{outlet}}
</div>
更新的瀏覽器輸出
男人,這讓我更加困惑:D。爲什麼Ember作爲一個很棒的框架忽略了那些非常簡單的@index在車把中。我認爲安伯得到了一切。 – 2014-08-29 13:39:03
你真棒,敬禮:D。表格中所有關於自動索引的問題的最佳答案和最簡單的答案 – 2014-08-29 14:00:24