Solved

How to check the checkbox based on array value I get

Posted on 2014-02-21
10
1,583 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
10 Comments
 
LVL 57

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 57

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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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
 

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 57

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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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…

688 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