select all checkbox pagination help required - checkbox

I currently have a report with pagination that displays 10 records Per page.
Within this report, I also have a checkbox column for every record. Based on this,
I want to incorporate a "Check All" feature, so based on my scenario which displays 10 records, when I press the "Check All" checkbox, I would like to check all the visible records (10 at a time) in that page (pageno=3) and after deleting those 10 records, the page should be redirected to the same page (filename.php) with same page number(pageno=3).
www.example.com/filename.php?pageno=3

Using some framework like jQuery will make your life a lot easier. Suppose following is structure of your records:
<table id="report">
<tr><td> <input type="checkbox" id="tr1" /></td><td>..</td><td>...</td></tr>
<tr><td> <input type="checkbox" id="tr2" /></td><td>..</td><td>...</td></tr>
<tr><td> <input type="checkbox" id="tr3" /></td><td>..</td><td>...</td></tr>
<tr><td> <input type="checkbox" id="tr4" /></td><td>..</td><td>...</td></tr>
<tr><td> <input type="checkbox" id="tr5" /></td><td>..</td><td>...</td></tr>
</table>
<input type="checkbox" id="chkAll"/> Select All.
The following code (using jquery) will do the needful:
<script type="text/javascript">
$(document).ready(function(){
$("#chkAll").change(function(){
if($("#chkAll").is(":checked")){
$("#report tr td:first-child").find("input:checkbox")
.attr("checked","checked");
}else{
$("#report tr td:first-child").find("input:checkbox")
.attr("checked","");
}
});
});
</script>
EDIT:- based on your code, try replacing your boxes_checkall function with this code;
function boxes_checkall(a,b)
{
var cbs=a.getElementsByTagName('input');
for(var i=0;i<cbs.length;i++)
{
if(cbs[i].type.toLowerCase()=='checkbox')
{
cbs[i].checked = b==1;
}
}
}
</script>

This could be solved using Javascript.
How do you define the names of the checkboxes?
You could do a for loop to change the status of all checkboxes that are shown at the moment.
If you're using a javascript toolkit/framework like jQuery this is very easy.
For instance you could give the class .page-[NUM] to all the checkboxes on a page and then use:
$(".page-[NUM]").each(function()
{
this.checked = checked_status;
});
Or if you use the same name for each checkbox on a page, try:
$("input[#name=thename]").each(function()
{
this.checked = checked_status;
});
where "thename" would be the name of your checkboxes on that page.

Related

Changing single cell color with ngClass

I'm having a hard time here with ngClass as I'm new to angular. I've got a table set up that generates rows dynamically from a database using ng-repeat, with span and input elements that hide/show based on editing/not editing the cell (imagine microsoft excel).
<tr>
<th class="row_head">Row Title</th>
<td ng-click="edit = true" ng-repeat="item1 in JSONobject | orderBy: '_id'" >
<span ng-hide="edit">{{ item1.key.value1 }}</span>
<input ng-show="edit" class="editing-cell" ng-model="item1.key.value1" ng-blur="edit = false; " ng-enter="saveData(item1._id, item1.key); edit = false" type="text" />
</td>
</tr>
<tr>
<th class="row_head">Row Title</th>
<td ng-click="edit = true" ng-repeat="item2 in JSONobject | orderBy: '_id'" >
<span ng-hide="edit">{{ item2.key.value2 }}</span>
<input ng-show="edit" class="editing-cell" ng-model="item2.key.value2" ng-blur="edit = false; " ng-enter="saveData(item2._id, item2.key); edit = false" type="text" />
</td>
</tr>
and so on.
I want to set it so on blur (data not saved) the cell turns red, and alternatively on save - turns green.
The issue i'm running into with ngClass is if i just go with:
ng-class="{'saved': saved, 'notSaved': notSaved}"
It changes the background color of EVERY cell, rather than just the cell edited.
I do not have this issue with ng-hide and ng-show, even though they appear to be a similar scenario with changing the boolean value of 'edit'. They still stick to their specific cell.
Any help would be appreciated.
You should look into how form styling works.
The following classes are added to, or removed from, input fields:
ng-untouched The field has not been touched yet
ng-touched The field has been touched
ng-pristine The field has not been modified yet
ng-dirty The field has been modified
ng-valid The field content is valid
ng-invalid The field content is not valid
ng-valid-key One key for each validation. Example: ng-valid-required, useful when there are more than one thing that must be validated
ng-invalid-key Example: ng-invalid-required
The following classes are added to, or removed from, forms:
ng-pristine No fields has not been modified yet
ng-dirty One or more fields has been modified
ng-valid The form content is valid
ng-invalid The form content is not valid
ng-valid-key One key for each validation. Example: ng-valid-required, useful when there are more than one thing that must be validated
ng-invalid-key Example: ng-invalid-required
The classes are removed if the value they represent is false.
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<style>
form.ng-pristine {
background-color:lightblue;
}
form.ng-dirty {
background-color:pink;
}
</style>
<body ng-app="">
<form name="myForm">
<p>Try writing in the input field:</p>
<input name="myName" ng-model="myName" required>
<p>The form gets a "ng-dirty" class when the form has been modified, and will therefore turn pink.</p>
</form>
</body>
</html>
My issue was not knowing enough about custom directives. I've got it figured out now with
.directive('isSaved', function(){
return {
link: function(scope, elem, attr) {
scope.notSaved = function(){
attr.$set('class', 'notSaved');
};
scope.saved = function(){
attr.$set('class', 'saved');
};
}
};
});

Marionette ItemView and radio Buttons

I have data being returned from a server with a status flag. And on each ItemView I have some radio Buttons as follows:
<td><input type='radio' name='<%- id %>-typeRecipe' value='0'></td>
<td><input type='radio' name='<%- id %>-typeRecipe' value='2'></td>
<td><input type='radio' name='<%- id %>-typeRecipe' value='1'></td>
As each ItemView is being created I want to check if the status returned by the server is the same value as the input value ie either 0,1, or 2 and if it is set that element to selected.
Recipe = Marionette.ItemView.extend({
ui: {
comm: 'input[type=radio]'
},
onBeforeRender: function () {
for (var i = 0; i < this.ui.comm.length; i++) {
if(parseInt(this.ui.comm[i].value, 10) === this.model.get('status')) {
this.ui.comm[i].$el.prop('selected')
}
}
}
However, when I view the interface none of the radio buttons have a check in them.
When I debug the code I can see that the line this.ui.comm[i].$el.prop('selected) is being executed so I'm wondering why doesn't it display set the element to selected? BTW I tried using that function on the onRender event also but the same result
I disagree with your approach, instead of having a onBeforeRender function whose whole function is to check the "state" and set it to checked. I would move that "logic" to the template and not have it handled in the view.
Your template would look something like:
<td><input type='radio' name='<%- id %>-typeRecipe' <% if (status == '0') { %>checked='checked'<% } %>value='0'></td>
<td><input type='radio' name='<%- id %>-typeRecipe' <% if (status == '2') { %>checked='checked'<% } %>value='2'></td>
<td><input type='radio' name='<%- id %>-typeRecipe' <% if (status == '1') { %>checked='checked'<% } %>value='1'></td>
and finally your render'er?? lol. would just look something like:
this.$el.html(this.template(this.model.toJSON()));
This again is "my opinion" and you can handle it as you see fit, but i like it because that sort of mundane logic clutters the views and just leaves the person behind you reading more code than he/she has too. And ... i think this approach is more "elegant" because that loop you have going on can be avoided completely.
It is the checked property for radio inputs, not selected. Also you will want to set the second argument to true, to actually set value for that property, or you're just getting it otherwise.
$('input.selectMe').prop('checked', true);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='radio' name='typeRecipe' value='0'>
<input type='radio' name='typeRecipe' value='2' class="selectMe">
<input type='radio' name='typeRecipe' value='1'>

How to create custom filter in angular js

I have table with rows filled up using ng-repeat. I also have an input box to search in the table. I don't want to use in build filter. I am confused with how to write a custom filter which will search in the search box and return the table rows. Also, I need search such that it match's first 4 letters if not the display whatever matches in the string. For example Search box say's "a124" then it should return all the table rows having the a124 as the first four characters if not then the default case would be search anywhere in the string.
<tr ng-repeat ="data in fetchedData></tr>
<th>{{data.name}}</th>
<th>{{data.id}}</th>
<th>{{data.receivedTime}}</th>
Input box
<input class="form-control" type="text" placeholder="Search..." ng-change="filterColumn()">
Controller code (I am confused what should go in )
$scope.filterColumn = function {
}
You don't need a custom filter for this. The normal angular filter should do fine.
Change your input to bind to search:
<input class="form-control" type="text" placeholder="Search..." ng-model="searchText">
And use that search filter to filter your ng-repeat
<tr ng-repeat ="data in fetchedData | filter:searchText"></tr>
Ok so this the solution which worked for me!! I got help from many AngularJS blog posts. Thanks to all of them. `
$scope.filter = function () {
var query = $scope.query.toLowerCase();
$scope.details = $filter('filter')($scope.data, function(entry) {
var concatString = '';
angular.forEach(entry,function(key ,value){
concatString += key + " ";
});
var pattern = new RegExp(query, 'i');
return (pattern.test(concatString));
});
};`
HTML code:<input ng-model="query" id="filter" class="form-control" type="text" placeholder="Search" ng-change="filter()">
<tr ng-repeat ="data in details"></tr>
#Priya i can see in your code, you used ng-change to call the filter method. This looks very normal way of calling a method from ancient javascript methodology.
Im suggesting you to use the way how angularjs will be using it.
For Example,
<input type="text" ng-model="letter">
<ul>
<li ng-repeat="friend in person.friends | startsWithLetter:letter">
{{ friend }}
</li>
</ul>
Here you see the ng-model="letter", this name is bind with filter called startsWithLetter. So whenever there is a change in input box then the filter will be triggered.
Here i wrote an article to create custom filters using angularjs in various types.
This will be useful for others who read this Q&A section in future.

Angular Scope data is not refreshing in Select box in IE 9, and it choked in it even when we are removing it from

Hello I am using the below code to change the Select box values, this code is perfectly working as expected in Chrome and Mozila, but I having issue in IE 9 Browser only.
Problem:
1 .>freeSelect Method should remove all the values from the selected option, but when I run this html in IE 9, even though it remove it from select box, but when you click on the select box to see any option are there or not, it will show it, but you can not select, I do not know the reason.
2.> addValue method will add the option value in the selected box, but here what is happening, this select box also showing the old values, which you can not select it.
This is my script below:
var phonecatApp = angular.module('phonecatApp', []);
phonecatApp.controller('PhoneListCtrl', function($scope) {
$scope.mydata = [
{
id:"0",
dataTxt:"1",
selectOption: [{optionss:"--Please Select--"},{optionss:"val11"},{optionss:"val12"}],
selectedData:"--Please Select--",
},
{ id:"1",
dataTxt:"2",
selectOption: [{optionss:"--Please Select--"},{optionss:"val21"},{optionss:"val22"}],
selectedData:"--Please Select--",
}
];
$scope.freeSelect=function(id){
console.log(JSON.stringify($scope.mydata));
alert($scope.mydata[id].selectedData)
$scope.mydata[id].selectOption=[];
$scope.mydata[id].selectedData="";
console.log(JSON.stringify($scope.mydata));
// $scope.$apply($scope.mydata);
}
$scope.addValue=function(id){
console.log(JSON.stringify($scope.mydata));
$scope.mydata[id].selectOption=[{optionss:"--Please Select--"}, {optionss:"val11"}];
$scope.mydata[id].selectedData="--Please Select--";
console.log(JSON.stringify($scope.mydata));
}
$scope.getData=function(id){
alert($scope.mydata[id].selectedData)
}
<table class="table" data-ng-controller="PhoneListCtrl" id="tbl" style="margin-top:-30px; width: 1380px">
<tr><th>Data</th><th>Data2</th><th>button</th></tr>
<tr data-ng-repeat="record in mydata" id="{{'tr'+record.id}}" style="color:#FF0000;">
<td><input type="text" data-ng-model="record.dataTxt"/></td>
<td>
<select class="form-control input-sm" data-ng-model="record.selectedData" data-ng-change="getData(record.id)" id="{{record.id}}">
<option data-ng-repeat="options in record.selectOption" value="{{options.optionss}}">{{options.optionss}}</option>
</select>
</td><td>
<input type="button" data-ng-click="freeSelect(record.id)" value="myData"/>
<input type=button data-ng-click="addValue(record.id)" value="addValue"/>
</td></tr>
</table>

ng-submit will not allow duplicate values

Why does Angular not allow duplicate data to be submitted? I am using ng-list to write all points scored using a simple controller.
For example - If a player inputs '10' into my app and then another player also does the same the ng-submit will stop functioning completely. I haven't specified anything else to allow this to happen. How can I change this default behaviour so users are allowed to post duplicate values?
Controller:
bowlingApp.controller('bowlPoints', function ($scope){
$scope.bowlPoints = [];
$scope.addBowlPoints = function() {
$scope.bowlPoints.push($scope.enteredPoints);
};
});
HTML
<form ng-submit="addBowlPoints()" role="form">
<div class="form-group">
<label>Player Score</label>
<input type="text" class="form-control" ng-model="enteredPoints">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<tr>
<ul>
<td>Player </td>
<td ng-repeat="points in bowlPoints ">{{points}}</td>
</ul>
</tr>
ng-submit works fine, in your example it adds value to array bowlPoints, but you just can not to see it.
To see an array and that ng-submit works fine you can type {{bowlPoints}} in your HTML file.
But if you want to see all data in table and if you want your table will to update and angular dynamically add your columns - you need to add track by in your ng-repeat
<td ng-repeat="points in bowlPoints track by $index ">{{points}}</td>
Demo: http://plnkr.co/edit/oFMm9Jqk034IRvS1qOCO?p=preview
I believe submit only does something if the form is dirty. Since the first submit will have cleared the dirty state, it won't do anything. You could try manually setting the dirty state of the form. Failing that, you could use ng-click instead and do the form submission yourself.

Resources