Note that there are some explanatory texts on larger screens.

plurals
  1. POKnockout - Checking checkboxes according to observableArray
    primarykey
    data
    text
    <p>So I have a list of schedules that I need to display for a user and show which schedules he or she is currently on and give them the possibility to jump on and off said schedules.</p> <p>My viewmodel looks like this</p> <pre><code>self = this; self.shifts = ko.observableArray(); self.selectedShifts = ko.observableArray(); //I populate self.shifts here with a WEB API call //Run through each shift and check if current user is on it and set checked / not checked value for checkbox ko.utils.arrayForEach(self.shifts(), function(shift) { //Clear array self.usersOnShift([]); //Populate array with all users on the shift self.usersOnShift = ko.observableArray(WEB API CALL HERE); var userInShift = ko.utils.arrayFirst(self.usersOnShift(), function(user) { if (selectedUserId == user.ID) { return true; } }); if (userInShift) { self.selectedShifts.push(shift.ID); } }); ko.applyBindings(self); </code></pre> <p>My HTML looks like this</p> <pre><code> &lt;div class="simple_overlay" id="shiftOverlay"&gt; &lt;div class="details"&gt; &lt;div data-bind="foreach: shifts"&gt; &lt;div&gt;&lt;span class="staff-initials" data-bind="text:wardName"&gt; &lt;/span&gt;&lt;input type="checkbox" data-bind="value: ID, checked: $root.selectedShifts"/&gt; &lt;/div&gt; &lt;/div&gt; &lt;div&gt; &lt;a href="#" data-bind="click: updateUserOnShifts"&gt;Connect&lt;/a&gt; &lt;a href="#" data-bind="click: closeShiftDialog"&gt;Close&lt;/a&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>I can see that the value of the checkboxes are set correctly to the ID of the corresponding shifts. However a shift that I know the user in question is on is not checked and I know that the selectedShifts observableArray contains the value.</p> <p>Somehow the "checked: $root.selectedShifts" call / check is not working but I know that it contains the right value. What am I doing wrong? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload