首先,nemesv 是对的,这可能不适合生产使用。
如果您仍想更改它,您可以覆盖它用于渲染网格的模板(更新的小提琴:http: //jsfiddle.net/fNhKp/4/)。
所以首先创建一个新模板并指定 simpleGridTemplate 绑定:
<div class='liveExample'>
<div data-bind='simpleGrid: gridViewModel, simpleGridTemplate:"custom_grid_template"'> </div>
</div>
<script type="text/javascript" id="custom_grid_template">
<table class="ko-grid" cellspacing="0">
<thead>
<tr data-bind="foreach: columns">
<th data-bind="text: headerText"></th>
</tr>
</thead>
<tbody data-bind="foreach: itemsOnCurrentPage">
<tr data-bind="foreach: $parent.columns">
<!--ko if: typeof rowText == 'object' && typeof rowText.action == 'function'-->
<td><button data-bind="click:rowText.action($parent)">action</button></td>
<!-- /ko -->
<!--ko ifnot: typeof rowText == 'object' && typeof rowText.action == 'function'-->
<td data-bind="text: typeof rowText == 'function' ? rowText($parent) : $parent[rowText] "></td>
<!--/ko-->
</tr>
</tbody>
</table>
</script>
然后像这样修改你的js:
var PagedGridModel = function(items) {
this.items = ko.observableArray(items);
this.gridViewModel = new ko.simpleGrid.viewModel({
data: this.items,
columns: [
{ headerText: "Item Name", rowText: "name" },
{ headerText: "Sales Count", rowText: "sales" },
{ headerText: "Price", rowText: function (item) { return "$" + item.price.toFixed(2) } },
{ headerText: "Add", rowText: "Add"},
{ headerText: "Action", rowText: {action: function(item){
return function(){
console.log(item)
}
}}}
],
pageSize: 4
});
};