2013-10-16 57 views
1

我使用敲擊頁面網格,如this example中所示。 library動態創建一個網格表,我想稍微改變它點擊標題的文本後使用排序功能。將排序功能添加到動態創建的網格

而不是

<th data-bind="text: headerText "> </th> 

我想看到這一點:

<th> 
    <a data-bind="click: MyPage.sort, text: headerText" ></a> 
</th> 

但它不工作。如果我叫click: console.log(MyPage.sort)它寫道:「不確定」,並click: console.log(MyPage.sort())後:

Object function MyPage() { 
    // body of function 
    ... 
    self.sort = function(item, event) { 
     console.log('qwerty') 
     ... 
    } 
    ... } has no method 'sort' 

有沒有辦法在這個例子中所說的「排序」?或者還有其他的方式(除了改變knockout.simpleGrid.1.3.js)嗎?

回答

1

這裏有一個解決方案...

結合後 - 讓與jQuery的標題和鏈接剛纔的onclick事件視圖模型。

var headers = $('.ko-grid').find('th'); 
headers[0].onclick = function() { viewModel.sortByName(); }; 
headers[1].onclick = function() { viewModel.sortBySales(); }; 
headers[2].onclick = function() { viewModel.sortByPrice(); }; 

工作示例...

http://jsfiddle.net/2JxZA/1/