2014-06-13 65 views
0

的值的子項集合在最近幾天我閱讀了大量文章,快速入門和教程,但是當向可觀察數組添加項目時,它們或多或少都會將空項目,並讓用戶在創建後填寫剩下的內容,但是我想反過來。將項目添加到表單值爲

下面的淘汰賽出價工作,直到下拉列表填充和按鈕被禁用如果textarea是空的,但這是我到目前爲止。

我的觀點的模型和數據我用的是在這裏找到:Child collection mapping not firing 我不知道我是否應該在單獨的線程他們在這裏複製,或讓他們的可讀性

我想才達到是增加一個新的可觀察的項目從我的收藏「文本」與下拉列表中的語言和點擊按鈕時從textarea的文本。

下面這部分內容是引導模式內容的一小部分,它綁定到我的根視圖模型上集合的「selectedItem」。

<div class="modal fade" data-backdrop="static" data-bind="showModal:selectedItem, with:selectedItem"> 
    <!-- Here is code for enumerating from collection "Texts", but i removed it for readability, and put focus on the "Add part" below --> 
       <div class="form-group"> 
        <label class="col-md-3 control-label col-md-offset-2">Language</label> 
        <div class="col-md-6"> 
         <select data-bind="options: $root.AvailableLanguages, optionsText:'Name', optionsValue:'Id'" class="form-control"></select> 
        </div> 
       </div> 
       <div class="form-group"> 
        <label class="col-md-3 control-label col-md-offset-2">Translation</label> 
        <div class="col-md-6"> 
         <textarea class="form-control" data-bind='value: $root.itemToAdd, valueUpdate: "afterkeydown"'></textarea> 
        </div> 
       </div> 
       <div class="form-group"> 
        <label class="col-md-3 control-label col-md-offset-2"><button class="btn btn-primary btn-sm" data-bind="click: addItem, enable: $root.itemToAdd().length > 0">Add</button></label> 
       </div> 
    </div> 
+0

您可以創建一個展示的jsfiddle你不能去上班? – 4imble

+0

http://jsfiddle.net/cSE5X/2/ - 我從來沒有使用JSFiddle,所以我不知道要在那裏添加什麼,但那是一個基本上我今天有。 如果我綁定addValues在窗體標籤上提交它不會被解僱,並且頁面被張貼,但是如果我綁定它來點擊按鈕它會被解僱,但是如果我使用alert(ko.toJSON(element )),我在「selectedItem」中獲取模型,而不是從我的表單中獲取模型。 – Pochen

+0

對不起,我仍然無法弄清楚你正在嘗試做什麼。在最簡單的層面上,你想達到什麼目標?剝去任何無關緊要的東西,並告訴我確切的問題。這聽起來像你只是想點擊一個按鈕,並添加一個新的項目有一個集合有兩個值,一個從下拉菜單,另一個從文本框,這是正確的? – 4imble

回答

0

我已將您的意見納入考慮併產生了這個小提琴。我希望它有幫助。

請注意模型如何跟蹤您關心的兩個項目,即下拉菜單和文本框。

然後,當您單擊Add Item時,它會將一個新項目添加到可觀察陣列(基於這兩件事情)。

淘汰賽爲我做了所有的魔術,我所做的一切都是設置了一些綁定。

Fiddle

的js

var viewModel = function() { 
    var self = this; 

    self.CurrentDropDownItem = ko.observable(""); 
    self.CurrentTextBoxItem = ko.observable("4imble"); 

    self.Greetings = ko.observableArray([new GreetingItem("Hello", "RandomPerson")]); 

    self.SaveNewGreeting = function(){ 
      self.Greetings.push(new GreetingItem(self.CurrentDropDownItem(), self.CurrentTextBoxItem())); 
    } 

    return { 
     CurrentDropDownItem: CurrentDropDownItem, 
     CurrentTextBoxItem: CurrentTextBoxItem, 
     Greetings: Greetings, 
     SaveNewGreeting: SaveNewGreeting 
    } 
} 

function GreetingItem(greeting, name){ 
    var self = this; 
    self.greeting = greeting; 
    self.name = name;  
} 

ko.applyBindings(viewModel); 

的Html

<div> 
    <select data-bind="value: CurrentDropDownItem"> 
     <option>Hello</option> 
     <option>Goodbye</option> 
     <option>How you doing?</option> 
    </select>  

    <input type="text" data-bind="value: CurrentTextBoxItem, valueUpdate: 'afterkeydown'"></input> 
    <button data-bind="click: SaveNewGreeting">Add Item</button> 
</div> 

<div> 

<h3>Dynamically Generated List</h3> 
    This is the list of items added so far (1st added at run time): 
    <div data-bind="foreach: Greetings"> 
     <b data-bind="text: greeting"></b>, 
     <b data-bind="text: name"></b><br /> 
    </div> 

<h3>Debug Status Messages</h3> 
    This is the value currently bound to drop down: 
     <b data-bind="text: CurrentDropDownItem"></b><br /> 
    This is the value currently bound to the text box: 
     <b data-bind="text: CurrentTextBoxItem, valueUpdate: 'afterkeydown'"></b> 
</div> 
+0

謝謝!那是我尋找的一個很好的開始。我甚至將它與「with」-data綁定結合起來,以便能夠編輯以及添加。現在我已經更好地掌握了我應該如何將自己的思想包裹起來! – Pochen