Solved

How to check the checkbox based on array value I get

Posted on 2014-02-21
10
1,519 Views
Last Modified: 2014-02-24
I'm doing knockout js. I have an array list coming from the database by making an ajax call and saving all the values in knockout observable array.

I'm looping through the array. Based on a value I want to check or uncheck the checkbox. Below is how I'm doing but this does not seems to be working. I can see values for roleid exists in the array but the checkbox is not checked if the value of roleid is true. What am i doing wrong here.

For example I'm doing this-

<tbody data-bind="foreach:$root.test">
    <tr>		
        <div><input type="checkbox" value="1" data-bind="checked: roleid == 1"/></div> 
    </tr>
</tbody>

Open in new window

0
Comment
Question by:bujjigadu
  • 4
  • 3
  • 2
10 Comments
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 39877974
A couple of things I notice

1. by adding the attribute checked - irrespective of what value you set it to it is going to be checked

2. I suspect that knockout is not going to evaluate roleid == 1 it will probably create something like this

<input type="checkbox" data-bind="checked: roleid==1">

Open in new window


A non checked box must not have the checked attribute - irrespective of value.

Here is a sample of how to link a checkbox to your model

<input type="checkbox" data-bind="checked: roleid" />

Open in new window

Knockout View
function AppViewModel() {
    this.roleid = ko.observable(1);
}

// Activates knockout.js
ko.applyBindings(new AppViewModel());

Open in new window

0
 

Author Comment

by:bujjigadu
ID: 39878266
Since the roleid values are already in an array and looping through my array which is 'test' in my case and trying to accomplish if the roleid value == 1 then check checkbox otherwise just display checkbox.

I tried to put the above mention function but checbox is checked all the time.

For example from the array I want to accomplish as

for each $root.test
if roleid == 1 then
check the checkbox
else
checbox


So basically I have three checkboxes and I'm trying to check them based on the value I get for roleid. Any suggestions please
0
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 39878834
Can you post more of your code - difficult to see what is going on based on what has been posted.

The reason for the failure based on code snippet posted was because of the roledid==1 not being evaluated - can we see your model and other code.
0
 
LVL 9

Expert Comment

by:Sar1973
ID: 39879465
Try:
var myArray = document.getElementById("ArrayName");
for (i = 0; i < myArray.length; i++) {
	if (myArray[i]=="yourValue") {
		document.getElementById("myCheck").checked=true;
	} else {
		document.getElementById("myCheck").checked=false;
	}
}

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:bujjigadu
ID: 39880741
I'm attaching my two code files here to look at it.

1) UserPermissions - EECopy - This file has the checkbox at the bottom and where I'm looping through the array. One note is the array gets build based on the value I change in Entity dropdown box (Please look for the comment <!--Display Entity Dropdown-->)
2) UserPermissionsModel - EECopy.js - This file has the viewmodel and calling my stored procedure to build the array.

Please let me know if it is not clear.
UserPermissionsModel---EECopy.js
UserPermissions---EECopy.txt
0
 

Author Comment

by:bujjigadu
ID: 39882566
Julian, any update on this?

Thanks
0
 
LVL 52

Accepted Solution

by:
Julian Hansen earned 300 total points
ID: 39882728
Working on it ...
1
 
LVL 9

Expert Comment

by:Sar1973
ID: 39882821
Shouldn't you then replace my example  "yourValue" with the dropdown value? Assign also an onchange event to the dropdown.
0
 

Author Comment

by:bujjigadu
ID: 39883438
I got it working like this

<div><input type="checkbox" value="4" name = "Admin" data-bind="checked: roleid() ==1"/></div>

the key is to make roleid ===1 to roleid() ===1 since the array is returning as an object.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now