2012-07-06 105 views
1

我該如何做?我試圖用我在這裏根據代碼構建的擴展器來實現它:https://github.com/jimrhoskins/knockout.localStorage使用localStorage存儲值加載Knockout Observable

它將值保存到localStorage就好了,但是在重新加載頁面時,Knockout似乎忽略了設置值並使用第一個選項覆蓋它來自陣列。

<select data-bind="value: pageSize, options: search_filter_data.page_sizes, optionsText: 'label'"></select> 

page_sizes: [{ 
     value: 10, 
     label: '10 Per Page' 
    }, { 
     value: 25, 
     label: '25 Per Page' 
    }, { 
     value: 50, 
     label: '50 Per Page' 
    }, { 
     value: 9999, 
     label: 'All' 
    }] 
+1

,你能否告訴我們'pageSize'的代碼?當你運行它們時,他們的例子是否適合你? – philipvr 2012-07-06 02:31:36

回答

0

使用optionsValue屬性就像這個例子:

例子:http://jsfiddle.net/gurkavcu/kA8W3/

<select data-bind="value: pageSize, options: page_sizes, 
     optionsText: 'label', optionsValue: 'value' "></select> 

function ViewModel() { 

    this.page_sizes = [{  
     value: 10, 
     label: '10 Per Page' 
    }, {   
     value: 25, 
      label: '25 Per Page' 
    }, { 
     value: 50, 
     label: '50 Per Page' 
    }, {  
     value: 9999, 
     label: 'All' 
    }]; 

    this.pageSize = ko.observable(25); 

}; 

$(function() { 

    var viewModel = new ViewModel(); 
    ko.applyBindings(viewModel); 


})​