How to bind checkbox value to Knockout observableArray on an object?

Josh

I have something like this:

var ScheduleDay = function(day, times) {
    var self = this;
    self.day = ko.observable(day || "");
    self.times = ko.observableArray(times || []);
};

function viewModel() {
    var self = this;
    self.schedule = ko.observableArray([
        new ScheduleDay("Monday"),
        new ScheduleDay("Tuesday"),
        new ScheduleDay("Wednesday"),
        new ScheduleDay("Thursday"),
        new ScheduleDay("Friday"),
        new ScheduleDay("Saturday"),
        new ScheduleDay("Sunday")
    ])
}

ko.applyBindings(new viewModel());

<input id="mondaySchedule" type="checkbox" data-bind="checked ???">

I have 3 checkboxes for each day of the week: morning, afternoon, and evening. I would like a model that is something like: schedule:{Monday:["morning","afternoon"], Tuesday: ["afternoon"], etc.}

Andrejs Kuzmins

As you have specified your question in comments, I suggest to take a look at this simple markup, that will do what you want:

<ul data-bind="foreach: schedule">
    <li>
        <strong data-bind="text: day"></strong>
        <input type="checkbox" data-bind="checked: times" value='morning' /> Morning
        <input type="checkbox" data-bind="checked: times" value='afternoon' /> Afternoon
        <input type="checkbox" data-bind="checked: times" value='evening' /> Evening
    </li>
</ul>

Working example: http://jsfiddle.net/qjzbossb/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to link checkbox and text value using knockout?

From Dev

Can not remove object from knockout observableArray

From Dev

knockout.js observableArray computed value

From Dev

Knockout add CSS class depending on value in observableArray

From Dev

Knockout observableArray not updating all properties on checked value

From Dev

Knockout observableArray not being populated by inherited datepicker value

From Dev

How to bind json string in observableArray?

From Dev

How to bind json string in observableArray?

From Dev

How to bind observableArray by index to the element

From Dev

How do I access an object by index in a knockout observablearray? Unable to process binding

From Dev

How to bind checkbox values to a null object in VueJS?

From Dev

Knockout, how to subscribe to every change in observableArray

From Dev

How to remove a piece of an observableArray inside a loop with knockout?

From Dev

How to iterate an Knockout ObservableArray? (length 0)

From Dev

Knockout, how to subscribe to every change in observableArray

From Dev

How to bind single checkbox value with AngularJS Controller

From Dev

How to bind single checkbox value with AngularJS Controller

From Dev

How to change checkbox checked value in Knockout.js?

From Dev

Checkbox will not stay checked with the data-bind in Knockout

From Dev

How to bind value summernote editor in mvc using knockout js (MVVM)

From Dev

How to bind the selected value from jQuery datepicker to a Knockout Observable

From Dev

How to bind the selected value from jQuery datepicker to a Knockout Observable

From Dev

How to bind value summernote editor in mvc using knockout js (MVVM)

From Dev

How to bind last index value of an observable array in knockout js

From Dev

MS Access: Possible to bind checkbox value to an attribute of object instance?

From Dev

bind url to checkbox value

From Dev

Knockout -> Bind element to Value of Select

From Dev

Knockout: Can't get specific value from observableArray

From Dev

Knockout dropdown select list binding array of name/value pairs to observablearray

Related Related

  1. 1

    How to link checkbox and text value using knockout?

  2. 2

    Can not remove object from knockout observableArray

  3. 3

    knockout.js observableArray computed value

  4. 4

    Knockout add CSS class depending on value in observableArray

  5. 5

    Knockout observableArray not updating all properties on checked value

  6. 6

    Knockout observableArray not being populated by inherited datepicker value

  7. 7

    How to bind json string in observableArray?

  8. 8

    How to bind json string in observableArray?

  9. 9

    How to bind observableArray by index to the element

  10. 10

    How do I access an object by index in a knockout observablearray? Unable to process binding

  11. 11

    How to bind checkbox values to a null object in VueJS?

  12. 12

    Knockout, how to subscribe to every change in observableArray

  13. 13

    How to remove a piece of an observableArray inside a loop with knockout?

  14. 14

    How to iterate an Knockout ObservableArray? (length 0)

  15. 15

    Knockout, how to subscribe to every change in observableArray

  16. 16

    How to bind single checkbox value with AngularJS Controller

  17. 17

    How to bind single checkbox value with AngularJS Controller

  18. 18

    How to change checkbox checked value in Knockout.js?

  19. 19

    Checkbox will not stay checked with the data-bind in Knockout

  20. 20

    How to bind value summernote editor in mvc using knockout js (MVVM)

  21. 21

    How to bind the selected value from jQuery datepicker to a Knockout Observable

  22. 22

    How to bind the selected value from jQuery datepicker to a Knockout Observable

  23. 23

    How to bind value summernote editor in mvc using knockout js (MVVM)

  24. 24

    How to bind last index value of an observable array in knockout js

  25. 25

    MS Access: Possible to bind checkbox value to an attribute of object instance?

  26. 26

    bind url to checkbox value

  27. 27

    Knockout -> Bind element to Value of Select

  28. 28

    Knockout: Can't get specific value from observableArray

  29. 29

    Knockout dropdown select list binding array of name/value pairs to observablearray

HotTag

Archive