2014-04-01 45 views
0

我正試圖實現一個小功能。我有包含文本框的行,並且所有值的下拉列表將以服務器端的數組形式出現。我可以將所有數據綁定到客戶端,除了下拉列表中的選定值(它總是指向下拉列表中的第一個)。請向我展示如何將選定值設置爲活動狀態,當數據從服務器端填充時。從JSON中選擇索引預填充下拉值

<select class="ddText" id="selType" data-bind="options:typeDropDown, optionsText: 'Value', optionsValue: 'Value',value : Type,attr: { name: 'age',id : 'str_'+$index()}, uniqueName:true" "></select> 

我已經嘗試將屬性添加到上面的代碼中的值綁定。像這樣的(價值:Type.Value - 而與得到錯誤創造新行從JSON工作時填入,「無法找到該類型的屬性值」

Dropdowns and JSON Objects and Binding the selected value

因爲我有創建動態行的功能,我在創建新行時出錯。

我需要重建我的數組嗎?

更新

function ViewModel() { 
    var self = this; 

    //Available types - which will come from serverside 
    self.typeDropDown = ko.observableArray(InitialData); 

    self.typeValue = ko.observable(); 

//Explicitly Adding new Row 
    self.Inputs = ko.observableArray([new Item(self.typeDropDown[0], '', '', '')]); 

    self.removeRow = function (Item) { 
     self.Inputs.remove(Item); 
    }, 
    self.addNewRow = function() { 
     //push will add a new element to the container without modifying much in DOM 
     self.Inputs.push(new Item(self.typeDropDown[0], '', '', '')); 
    } 
+0

請發佈您的viewmodel。 – Tuan

+0

@Tuan:更新了我的問題 – codebot

+0

我還需要看看您是如何定義Item和InitialData的。 – Tuan

回答

0

的值綁定到類型,但你沒有在你的視圖模型函數定義的類型屬性。

self.Type = ko.observable("NASA1"); 

到您的視圖模型:

如果添加像它應該工作。

查看此例JSFiddle

+0

謝謝,我會盡力讓你知道。 – codebot

+0

超級感謝@Tuan工作。 – codebot