Note that there are some explanatory texts on larger screens.

plurals
  1. POKnockout two way Binding for Dropdrown
    primarykey
    data
    text
    <p>I am facing the issue while binding the value for dropdown from a observable array.</p> <ul> <li><p>Model</p> <pre><code>function question(item) { var self = this; self.questionId = ko.observable(item.questionId || ""); self.position = ko.observable(item.position || ""); self.position.subscribe(function (data) { // Some Logic }, this, 'beforeChange'); api.position.subscribe(function (data) { // Some Logic } } </code></pre></li> <li><p>View Model</p> <pre><code>function viewmodel(item) { var self = this; self.questionList = ko.observableArray([]); self.questionEntity = ko.observable(new question(item)); // initially Item will be blank self.addQuestion = function(data) { data.position(self.questionList().length + 1); self.questionList.push(data); } } </code></pre></li> <li><p>View</p> <pre><code>&lt;div&gt; question Id : &lt;input type="text" data-bind="value: questionEntity().questionId " &gt; &lt;button data-bind='click: $root.addquestion'&gt;Add Question&lt;/button&gt; &lt;/div&gt; &lt;table&gt; &lt;tbody data-bind='foreach: questionList '&gt; question Id : &lt;span data-bind='text:questionId' &gt;&lt;/span&gt; position : &lt;select data-bind='options: $root.positionList,optionsText: "id",optionsValue: "id" ,value: position'&gt;&lt;/select&gt; &lt;/tbody&gt; &lt;/table&gt; </code></pre></li> <li><p>Problem</p></li> </ul> <p>I have one popup div(<code>addQuestion</code>) and I will add the question. and I am keep on adding that question to my observable Array(<code>questionList</code>)</p> <p>When i add First question my value in observable array is </p> <pre><code>questionId | Position 1 | 1 </code></pre> <p>When I add second record</p> <pre><code>questionId | Position 1 | 1 2 | 2 </code></pre> <p>When I Add thrid record, all the previous records POSITION value(value is binded to dropdown) is being reset to 1 except last record</p> <pre><code>questionId | Position 1 | 1 2 | 1 3 | 3 </code></pre> <p>fourth record</p> <pre><code>questionId | Position 1 | 1 2 | 1 3 | 1 4 | 4 </code></pre> <p>after debugging I found out its problem with two way binding with Dropdown </p> <pre><code>&lt;select data-bind='options: $root.positionList,optionsText: "id",optionsValue: "id" ,value: position'&gt;&lt;/select&gt; </code></pre> <p>in above code if change the binding(single way) like below it works fine</p> <pre><code>&lt;select data-bind='options: $root.positionList,optionsText: "id",optionsValue: "id" ,value: position()'&gt;&lt;/select&gt; </code></pre> <p>But I need to subscribe an event for the dropdown.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload