Monday, October 11, 2021

Getting selected value from dropdown list in Knockout.js

 Set the whole city object as value for selectedCity. You can also add a computed observable for retrieving text.

function ViewModel() {
    var self = this;
    self.Cities = ko.observableArray([]);
    self.selectedCity = ko.observable();

    self.selectCity = function (city) {
        self.selectedCity(city);
    };

    self.selectedCityNameRu = ko.pureComputed(function () {
        var selectedCity = self.selectedCity();
        return selectedCity ? selectedCity.CityNameRu : '';
    }, self);

Then in your html bind to selectedCityNameRu

<span data-bind="text: selectedCityNameRu"></span>

No comments:

Post a Comment

Get max value for identity column without a table scan

  You can use   IDENT_CURRENT   to look up the last identity value to be inserted, e.g. IDENT_CURRENT( 'MyTable' ) However, be caut...