Defining a preselection

28 views
Skip to first unread message

Bastian Krones

unread,
Mar 22, 2013, 9:43:03 AM3/22/13
to knock...@googlegroups.com
Hello,
i want to list some tasks in which each task a new dropdown appears with customers.
i solved it to get 2 models receiving 2 JSON requests.

Knockout:
function Task(data) {
            this.ID = ko.observable(data.ID);
            this.Description = ko.observable(data.Description);
            this.ClientName = ko.observable(data.ClientName);
            this.ClientFK = ko.observable(data.ClientFK);
            this.CustomerName = ko.observable(data.CustomerName);
            this.CustomerFK = ko.observable(data.CustomerFK);
        }

        function Customer(data) {
            this.CompanyName = ko.observable(data.CompanyName);
            this.CustomerID = ko.observable(data.CustomerID);
            this.AnzahlTasks = ko.observable(data.AnzahlTasks);
        }

        function TaskListViewModel() {
            // Data
            var self = this;
            self.tasks = ko.observableArray([]);
            self.customers = ko.observableArray([]);

            $.getJSON("/api/CustomerList", function (allData) {
                var mappedCustomer = $.map(allData, function (item) { return new Customer(item) });
                self.customers(mappedCustomer);
            });

            // Load initial state from server, convert it to Task instances, then populate self.tasks
            $.getJSON("/api/Task", function (allData) {
                var mappedTasks = $.map(allData, function (item) { return new Task(item) });
                self.tasks(mappedTasks);
            });           
        }
        ko.applyBindings(new TaskListViewModel());

View:
    <li>
        <select data-bind="options: $root.customers, optionsText: 'CompanyName', optionsCaption: 'Choose...'"></select>
        <input data-bind="value: CustomerName" /><br />
        <input data-bind="value: ClientFK" /><br />
    </li>

So far so good. I am just not able to get a preselect. Every task should have it's onw preselected customer of the list "customerlist".

How do i solve that?



Greg Merideth

unread,
Mar 22, 2013, 2:35:00 PM3/22/13
to knock...@googlegroups.com
You're not assigning a value to the select list, I'm assuming this is part of a foreach display.
Reply all
Reply to author
Forward
0 new messages