Q:

syncing jquery ui select menu with knockout observable array

probably going about this the wrong way but I have an html table that is populated using a knockout observable array using foreach.  in each row I have a drop down.  I like the jquery ui select menu so I am using that.  unfortunately when you run the fiddle you will notice that when updating the drop down the corresponding  knockout selected value is not being updated.
here is the fiddle https://jsfiddle.net/8rw4ruze/3/
here is the html.
<table class="table  table-condensed table-responsive">
  <thead>
    <th>id</th>
    <th>animal</th>
    <th>Selected Value</th>
  </thead>
  <tbody data-bind="foreach: tableRows">
    <tr>
      <td data-bind="text: id"></td>
      <td>
        <select class="form-control" data-bind="options: recordList,
                     optionsText: 'desc',
                     optionsValue: 'val',
                     value: selectedVal,
                     attr: {id:  selectId}">

        </select>
      </td>
      <td data-bind="text: selectedVal"></td>
    </tr>
  </tbody>
</table>

and here is the javascript
function record(val, desc) {
  var self = this;
  this.val = ko.observable(val);
  this.desc = ko.observable(desc);
}

function tableRow(id, recordList) {
  var self = this;
  this.id = ko.observable(id);
  this.recordList = ko.observable(recordList)
  this.selectedVal = ko.observable('A');
  this.selectId = ko.computed(function() {
    return 'mySelect' + self.id()
  }, this);
}

function Model() {
  var self = this;
  this.records = ko.observableArray("");
  this.tableRows = ko.observableArray("");
}

var mymodel = new Model();

$(function() {

  mymodel.records.push(new record('A', 'ant'));
  mymodel.records.push(new record('B', 'bird'));
  mymodel.records.push(new record('C', 'cat'));
  mymodel.records.push(new record('D', 'dog'));
  mymodel.tableRows.push(new tableRow(1, mymodel.records()));
  mymodel.tableRows.push(new tableRow(2, mymodel.records()));
  mymodel.tableRows.push(new tableRow(3, mymodel.records()));
  mymodel.tableRows.push(new tableRow(4, mymodel.records()));

  ko.applyBindings(mymodel);

    for (var i = 0; i < 4; i++) {
    var id = '#mySelect' + (i + 1)
    $(id).selectmenu({
      width: 125,
      change: function(event, ui) {
        var newVal = $(this).val();
        mymodel.tableRows()[i].selectedVal(newVal);
      }
    });
  }

});

thanks all I went with a data attribute. I'd prefer to use the custom binding but I'm not smart enough to figure that out so I went with this.
for (var i = 0; i < 4; i++) {
    var id = '#mySelect' + (i + 1)
    $(id).selectmenu({
      width: 125,
      change: function(event, ui) {
        var newVal = $(this).val();
        var index = $(this).data( "index" );
        mymodel.tableRows()[index].selectedVal(newVal);
      }
    }).data( "index", i );
  }

here is the fiddle https://jsfiddle.net/8rw4ruze/7/
I think I got it working with the custom binding here it is
https://jsfiddle.net/8rw4ruze/8/

A:

Matt.k is correct that your value of i is not what you want it to be when it is used. One way around that is to use this loop instead of a simple for:
$('select').each(function (i, e) {
  e.selectmenu({
    width: 125,
    change: function(event, ui) {
      var newVal = $(this).val();
      mymodel.tableRows()[i].selectedVal(newVal);
    }
  });
});

This is a little fragile, as it relies on the i and the e to correspond correctly. A more robust (and typical in Knockout) approach is to use a binding handler, which would allow you to specify the binding and have Knockout go through the elements and make the necessary calls. Surprisingly, a little web-searching didn't turn one up for me.
I see there is a knockout-jqueryui project on github that might also give you a clean way of using the widgets you want.

