Get All Checked Child Items Kendo Grid - javascript

I have a Kendo grid that has Child elements as shown in the Image below. Is there a way to read the elements that are checked.

It depends on when you want to find out what checkboxes are selected, but essentially it will work this way.
You simply add a listener to a button or a common class among the checkboxes that looks at the checkboxes and returns the checked ones.
The example from Kendo: http://dojo.telerik.com/UhANu
Specifically,
$("#showSelection").on("click", function () {
var checked = [];
for(var i in checkedIds){
if(checkedIds[i]){
checked.push(i);
}
}
alert(checked);
});
I've changed the above to a .on() instead of .bind because it's what I'm more familiar with being the idiomatic way of doing listeners, but both technically work.
If you'd rather have value of the checkboxes save each time you change them it'd be something like this:
$(".checkbox").on("click", function () {
var checked = [];
for(var i in checkedIds){
if(checkedIds[i]){
checked.push(i);
}
}
$('#checked-boxes').val(checked);
});
and in your html create an element that holds the values:
<label for="checked-boxes">Checkboxes that have been selected:</label>
<input type="text" id="checked-boxes" name="checked-boxes">

Related

Dynamically how to make checkbox check/uncheck in angularjs controller using ng-click

I am new to Angular JS. When the user check/uncheck on a check box, I am calling a function in a controller using ng-click. I am passing $event to the function in controller. Using the $event, I am able to get the srcElement inside the controller function. Now I would like to set the previous check/uncheck value to the check box based on certain conditions.
$scope.isAccessChanged = function(event){
if (some condition) {
var elem = angular.element(event.srcElement);
/** here how to set the elem value back to whatever it was before.*/
}
};
Lets say you have check box like
<input ng-model="form.isSelected" type="checkbox">
All you need to do is:
$scope.form.isSelected = !$scope.form.isSelected;
Avoid DOM manipulation and limit jQuery use as much as possible in angular.
I recommend using jQuery only in directives to make it less of an available option.
Try this out:
<input type="checkbox" ng-model="foShizzle" ng-click="isAccessChanged()"/>
$scope.isAccessChanged = function(event){
if(some condition){
$scope.foShizzle = !$scope.foShizzle; // This will reverse the user's decision
}
}

How to select Parent Checkbox when a Child is selected inside ng-repeat

I am having a very hard time trying to figure out a solution for this. I have a checkbox group where the first checkbox is the parent and the following in that set will be child. I want the parent checkbox to be selected automatically if one of the child's checkbox is selected. Similarly parent needs to be get unchecked if no child is selected.
Here is my jsfiddle example: http://jsfiddle.net/Alien_time/PqTR7/3/
The main difficulty I am facing is because the checkboxes are created dynamically and it has dynamic ng-models for each. I have tried the following so far:
1) ng-checked: This doesnt work for me since ng-checked doesnt bind the value with ng-model. I need the ng-model of the parent to be updated as well since this is going to reflect in the main form.
2) JS solution: I thought js method will be the solution, but dont know how to add the js to controller as the ng-model is dynamically generated.
3) On other posts, there are some method that uses select all when parent is checked. But I couldnt find a solution for my approach since its the other way around where I only want the parent selected if one of the child is selected.
For my form, I need to have a different ng-model for each checkbox thats why I am using the name to create a dynamic ng-model name. But I just couldnt figure out how to select the parent checkbox if a child is selected in this dynamic list.
I have been stuck on this for 2 days and searched a lot on the net. Can you help me please?
HERE is the working solution based on your fiddle.
JS
$scope.select = function(index){
if(index === 0){
$scope.slaves.forEach(function(slave, ind){
$scope.slaves[ind].isChecked = $scope.slaves[0].isChecked;
});
}
else {
var anyChild = false;
for(var i = 1; i < $scope.slaves.length; i++){
anyChild = anyChild || $scope.slaves[i].isChecked;
}
$scope.slaves[0].isChecked = anyChild;
}
}
HTML
<div ng-repeat="slave in slaves">
<input type="checkbox" ng-model="slave.isChecked" ng-click="select($index)" />
{{slave.name}} - {{ slave.description }}
</div>
To tell the truth I do not find the solution elegant -- you would be better off by encapsulating the logic of it in a custom directive.
Moreover it would be probably better to express parent-child relation by:
var parent = {
... // parent data
childeren : [child_1, ... , child_N] // array of children
}
A solution to this problem is to add a watch.
Add the following in your controller
$scope.$watch('checkboxData', function (newValue, oldValue) {
var anyChecked = false;
// see if any are checked
$scope.slaves.reduce(function (pVal, cVal, idx, arr) {
if (newValue[cVal.name]) anyChecked = anyChecked || newValue[cVal.name];
});
// replace the parent 'checked' in the model
$scope.checkboxData['Parent'] = anyChecked;
}, true);
And add the following to your input elements.
ng-checked='checkboxData[slave.name]'

Jquery click event with binded event

Hi guys I am having a problem with Events. I have a checkbox list and I have a main check box that checks all boxes. When I clickEvent some of my checkbox list items it should add data-id attr to the "selected obj". So in my case when I press main check box to check all others every thing is ok (it simply clicks all other elements). but when i do that it empties my array. I mean if i uncheck it will be the way it supposed to be but checked (when uncheck it fills when i check it empties).
......
var selected = {};
var reload = function(){
selected = {};
$('.checkbox_all').unbind('click');
$('.table_checkbox').unbind('click');
$('.checkbox_all').bind('click', checkAll);
$('.table_checkbox').bind('click', checkMe);
}
var checkMe = function(e){
var checkbox = $(e.target);
var id = checkbox.attr('data-id');
//console.log(id);
if(checkbox.attr('checked')){
selected[id] = id;
}else{
if(selected[id]) delete selected[id];
}
console.log(selected);
}
var checkAll = function(e){
if($(e.target).attr('checked')){
$('.table_checkbox').each(function(){
if($(this).attr('checked') === false){
$(this).click();
}
});
}else{
$('.table_checkbox').each(function(){
if($(this).attr('checked') === true){
$(this).click();
}
});
}
//console.log(selected);
}
.......
HTML:
<tr><th class="table-header-check"><input type="checkbox" class="checkbox_all"/></th></tr>
<tr class=""><td><input type="checkbox" data-id="5" class="table_checkbox"></td></tr>
<tr class="alternate-row"><td><input type="checkbox" data-id="6" class="table_checkbox"</td></tr>
<tr class="alternate-row"><td><input type="checkbox" data-id="8"
....ETC\
My problem is that when i click .checkbox_all it should click on all .table_checkbox(that r cheched or uncheched)... it just clicks all checkboxes like a main checkbox... it works fine, but i have an event all other checkboxes if i click em i add some data to array when i unclick em it removes data from array.... so when im clicking checkboxes sepperatly they add /remove data to array properly... but when im clicking on main checkbox... it clicks on right checkboxes but the data array is empty when all checked and full when all unchecked... it must be the opposite way
Could you instead go for a cleaner solution, and generate selected on the fly? See here for an example (and a JSFiddle for everyone else): http://jsfiddle.net/turiyag/3AZ9C/
function selected() {
var ret = {};
$.each($(".table_checkbox"),function(index,checkbox) {
if($(checkbox).prop("checked")) {
ret[$(checkbox).prop("id")] = true;
}
});
return ret;
}
** EDIT: **
If you're looking to have an array that is added to and removed from, then this JSFiddle (http://jsfiddle.net/turiyag/pubGb/) will do the trick. Note that I use prop() instead of attr(), in most cases, especially this one, you should use prop() to get the value you want.
To work with your own code you need to understand the order of events. When you programmatically call click() on the checkbox the javascript (checkMe() for children) executes before the state of each child checkbox is changed (e.g., adding attribute 'checked'). It is because of this reason that the checkMe() function was adding and removing ids in the selected array in the reverse order. You can confirm this by adding the following debug line in the checkMe function:
console.log('Checked state of checkbox id:' + id + ' is: ' + checkbox.prop('checked'));
Case1: Clicking checkAll when it is Unchecked; it calls checkMe() for each child checkbox but finds the 'checked' attribute as undefined. So it executes the delete code. After executing checkMe the 'checked' attribute is added on the checkbox.
Case2: Clicking checkAll when it is Checked; the checkMe() function finds the 'checked' attribute previously added and fills the array. Later an event is probably fired to remove the 'checked' attribute.
I changed the following lines to quickly test this and seems to be working:
Bind checkMe on change event instead of click in reload function:
$('.table_checkbox').bind('change', checkMe);
Change the condition for unchecked children in checkAll function when the .checkbox_all is checked:
if($(this).prop('checked') === false) {/*call child click*/}
//Use prop instead of attr because it takes care of 'undefined' cases as well. If you want to keep using attr because you're on an older version of jquery then add something like:
typeof $(this).attr('checked') == 'undefined'
and also the condition when .checkbox_all is unchecked:
if($(this).prop('checked') === true) {/*call child click*/}
Hope this helps. Here's a jsbin to play with..

Multiple checkbox scripts intervene with each other

a nice member here helped me set up a multiple checkbox example that stores the data to be shown in a div. However, when I try to do multiple of these, they interlap with each other and show the same data in the divs even when I changed variables.
I set up a simple example here:
http://jsfiddle.net/pufamuf/vrpMc/4/
Thank you for your time everyone :)
That's because you're using the same selector in both event handlers: input[type="checkbox"]:checked
This will select all checked checkbox inputs in the page.
You should instead use input[name="car[]"]:checked and input[name="phone[]"]:checked
to select only the inputs with the given name, each time.
In both your functions, you're selecting all of the selected checkboxes. My fix (and someone else might have a better one) would be to add unique ids to the ul's surrounding the li's.
html:
<ul id='electronics'>
<li><input type="checkbox" name="phone[]" value="Nokia" />Nokia</li>
That way you can modify your $('#submit').click handler to something like this:
$('#submit').click(
function()
{
var htmls = "";
$('ul#electronics>li>input[type="checkbox"]:checked').each(
function()
{
htmls += $(this).val() + " ";
}
);
$('.here').html(htmls);
}
);
Check out http://api.jquery.com/child-selector/, http://api.jquery.com/id-selector/ for more info.
Basically, without this or a similar change, there's nothing distinguishing your list of car brands from your list of electronics brands, and your click handlers both consider all of the checked checkboxes.

drop down list changed

in my application I need to check if a drop down list has changed or not. can you please let me know how I should write it?
if (document.form.dropdownlist.???)
I don't know what to write instead of ????
You could give this dropdownlist an unique id:
<select name="foo" id="foo">
...
</select>
and then subscribe for the onchange event (make sure you make the subscription once the DOM is loaded, for example in the body onload method):
var ddl = document.getElementById('foo');
ddl.onchange = function() {
alert('the value has changed');
};
var count=3;
function check()
{
var obj =new Array();
obj=document.getElementById("list");
checkLength(obj.length);
}
function checkLength(len)
{
if(len>count)
{
alert("Drop down size changed");
}
}
Take three option elements in select element.
Call the method check() using onchange() to get the size of drop down list.
The variable count is used to store the drop down list size .
So that it can be used to check if drop down list is changed.

Categories