简体   繁体   中英

How to select the list based on its dynamic value

I am new in knockout.i want that when i click on edit button based on the value the dropdown should be selected.

Here is My Full Code...I am trying to Edit the Record...

Here is My HTML Code

Here is my JS code...

function item(id, name) { this.id = ko.observable(id);

    this.name1 = ko.observable(name);

}

function CompanyViewModel() {

var self = this; var Sort = "desc";

    self.id = ko.observable("");
    self.name = ko.observable().extend

({ required: true }); self.bloombergcode = ko.observable().extend({ required: true });

    self.Id = ko.observable();

    self.sector1 = [new item(1, "Banking"), new item(2, "Non-Banking")];

    self.country = ko.observableArray([]);

    self.sectorid = ko.observable().extend({ required: true });

    self.isincode = ko.observable();
enter code here
    self.address = ko.observable();

    this.validationModel = ko.validatedObservable({
        name: self.name,
        bloombergcode: self.bloombergcode,
        sectorid: self.sectorid

    });



    //        self.items = ko.observableArray([]);
    var company =
{
    id: self.id,
    name: self.name,
    bloombergcode: self.bloombergcode,
    sectorid: self.sectorid,
    isincode: self.isincode,
    sector1: self.sector1,
    address: self.address
};

    self.company = ko.observable();
    self.companies = ko.observableArray();


    $.ajax({
        url: '@Url.Action("CompanyId", "Company")',
        cache: false,
        type: 'GET',
        contentType: 'application/json; charset=utf-8',
        data: {},
        success: function (data) {

            //self.companies(data);
            self.company(data); 

            self.id(data.id);

            self.name(data.name);

            self.sectorid = ko.observable(new item(data.sectorid, ""));

            self.isincode(data.isincode);

            self.address(data.address);

            self.sectorid(self.sector1[1].id);

           // self.sectorid(self.sector1[1]);


            self.bloombergcode(data.bloombergcode)
        }
    });

}

var viewModel=new CompanyViewModel(); ko.applyBindings(viewModel);

Since you supplied the optionsValue parameter, you need to set sectored to the id value. In other words:

self.sectorid(self.sector[1].id());

Also, your binding statement is incorrect. You're missing quotes around id on the optionsValue parameter. Such as:

<select id="sector" data-bind="options:sector1 , validationElement: sectorid, valueUpdate: 'afterkeydown', optionsValue:'id', optionsCaption: '........Select........', value: $root.sectorid, optionsText: 'name1' "></select>        

Hope that helps!

PS For clarification, without supplying the optionsValue , knockout will use the actual object as the value. In that case, the code you had originally, self.sectorid(self.sector[1]) would have worked as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM