Why aren't the values of the dropdowns bound?

53 views
Skip to first unread message

happyc...@gmail.com

unread,
Sep 10, 2017, 9:38:34 AM9/10/17
to KnockoutJS

The following minimal sample binds a grid.

View

    <div id="divDecision">
     
<div id="divDecisionBinding" data-bind="template: { name: 'tmplDecision' }">/div>
     
<script id="tmplDecision" type="text/x-jquery-tmpl">
       
<table id="tblDecision">
         
<thead>
           
<tr>
             
<th>Candidate</th>
              <th>Decision</
th>
           
</tr>
          </
thead>
         
<tbody data-bind="foreach:decisionList" id="tbList">
           
<tr>
             
<td data-bind="text: candidate"></td>
             
<td>
               
<select data-bind="attr: { id: 'cmbDecision' + ':' + $index(), name: 'cmbDecision' + ':' + $index()}, options: viewModelDecision.decisionLookup, value: 'decision', optionsText: 'decision_desc', optionsCaption: 'Please select'"></select>
             
</td>
            </
tr>
         
</tbody>
        </
table>
     
</script>
   
</div>


viewModelDecision (2 members: decisionLookup and decisionList)

    decisionLookup
   
0 : {decision: "N", decision_desc: "No need"}
   
1 : {decision: "A", decision_desc: "Approved"}
   
2 : {decision: "R", decision_desc: "Rejected"}
    decisionList
   
0 : {candidate: "000000001", decision: "A" }
   
1 : {candidate: "000000002", decision: "N" }


Script
   
ko.applyBindings(viewModelDecision, document.getElementById("divDecision"));



Findings

 1. The field candidate is successfully bound
 2. The options of the dropdownlist `cmbDecision:X` are successfully bound
 3. The values of the dropdownlist `cmbDecision:X` are not bound - the
    caption "`Please select`" is selected by default.

Question

Why aren't the values dropdown list selected by default?

Gunnar Liljas

unread,
Sep 10, 2017, 9:44:59 AM9/10/17
to knock...@googlegroups.com
You need optionsValue='decision' too.

Also, options: viewModelDecision.decisionLookup is better replaced by $parent.decisionLookup, so you don't have to refer to the viewmodel by instance name.

/G

--
You received this message because you are subscribed to the Google Groups "KnockoutJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to knockoutjs+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gunnar Liljas

unread,
Sep 10, 2017, 9:46:20 AM9/10/17
to knock...@googlegroups.com
...and value = decision, not 'decision'

happyc...@gmail.com

unread,
Sep 10, 2017, 10:02:37 AM9/10/17
to KnockoutJS
Thank you so much, Gunnar! It worked.

Have a great day.

To unsubscribe from this group and stop receiving emails from it, send an email to knockoutjs+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages