Angularjs: Select all checkbox - angularjs

I want to implement select all checkbox (for multiple delete).
This is my View script:
<input type="checkbox" name="chk_all" id="chk_all" ng-click="checkAll(this)" /> Select All
<table>
<tr ng-repeat="user in allUsers">
<td><input type="checkbox" ng-model="list" name="list[]" class="chk_all" value="{{user.user_id}}" id="{{user.user_id}}" ng-checked="checkedAll" /></td>
<td>{{user.username}}</td>
<td>{{user.fname}}</td>
<td>{{user.lname}}</td>
<td>{{user.mobile}}</td>
<td>{{user.email}}</td>
<td><a title="Edit User" href="#/edit_user/{{user.user_id}}" ><span class="fa fa-pencil"></span></a> | <a title="Delete User" id="" style="cursor:pointer;" ng-click="delete_user(user.user_id)" ><span class="fa fa-trash-o"></span></a></td>
</tr>
</table>
On "chk_all" checkbox following all checkbox should checked or unchecked.
I want to delete selected users on button click. So how can i get selected user id's on http post method.
Script written as:
$scope.testFunction=function(params)
{
$http.post("../admin/users/testFunction",{'params' : params})
.success(function (data, status, headers, config)
{
console.log(data);
alert(1);
})
.error(function(data, status, headers, config){
});
}
controller function:
public function testFunction()
{
$data = json_decode(file_get_contents("php://input"));
print($data->params);
}
In testFunction (php function), how can I retrive selected user id's ?
Any solution?
Note: I have done it with javascript and php. Now application is going to convert into Angularjs, so it should be in angularjs.

You could give the object 'user' a property: 'checked', then the checkbox looks like: <input type="checkbox" name="list[]" class="chk_all" ng-model="user.checked" onclick="uncheck_select_all(this)" />. Then on your submit, you check for each user in allUsers whether user.checked == true and if so push that user to an array. Then put that array in your http post and handle deletion for each user in the array at the server side.
Update
In this plnkr I show what I meant with my answer + comments. This doesnt work if you simply copy-paste because I used some different namings. If you can't figure out the solution with this plunker, I'm afraid I did all I could. Note: You could replace the angular.forEach loops with regular for-loops for slightly faster performance.

Checkout ng-checked in the Angular Docs.
You can add an expression to every checkbox you want checked based on a $scope variable.
<input type="checkbox" name="chk_all" id="chk_all" ng-click="checkAll(this)" />
<input type="checkbox" name="chk_1" id="chk_1" ng-checked="checkedAll" />
<input type="checkbox" name="chk_2" id="chk_2" ng-checked="checkedAll" />
<input type="checkbox" name="chk_3" id="chk_3" ng-checked="checkedAll" />
In your controller you would have this:
$scope.checkAll = function() {
$scope.checkedAll = true;
}
This will set the $scope.checkedAll variable to true making the ng-checked expression true, which will check all checkboxes using that expression.
I changed the onchange attribute to ng-click as well.

Related

angularjs change view on ng-model change

<select ng-model="Listbox" ng-options="build for build in builds" ng-change="show(Listbox); showtable(Listbox);showummary(Listbox);"style="width: 500px" id="lbox"></select>
Here on change of option i am calling all 3 functions but i want them to be called depending on which radio button is checked.
<tr><td >
<input type="radio" checked onclick="showF();">Summary
</td>
<td >
<input type="radio" onclick="showS();">Detailed Summary
</td>
</tr>
If summary is selected then only call show and showtable otherwise call showsummary. I can put them in radio buttion tag but then change in select option won't be deteced.
You can modify your ngChange to trigger a single function, as below:
<select ng-model="Listbox" ng-options="build for build in builds" ng-change="checkShow()"style="width: 500px" id="lbox"></select>
Then you change your radio buttons:
<input type="radio" value="summary" ng-model="radioSelected">Summary
<input type="radio" value="detailedSummary" ng-model="radioSelected">Detailed Summary
Note: Don't use onclick, use ngClick if you need to trigger a click event in Angular.
Finally, in your controller you can just check what's the option selected in radio:
$scope.radioSelected = 'summary'; // Default selection
$scope.checkShow = function() {
if ($scope.radioSelected === 'summary') {
show();
showTable();
} else {
showSummary();
}
}

how to get select value with input values in angularjs

Can any one help how to get input and selected values by dynamically added with angularjs?
Here is my code in Plunkr
When I select vegtables, another input shows with some values. when I click submit button I need a json like
{
"itemName":"Mango",
"itemType":"fruits"
},
{
"itemName":"Carrot",
"itemType":"vegtables",
"iteminfo":"you selected Carrot"
},
{
"itemName":"Apple",
"itemType":"fruits"
}
I forked your plunker
You can bind itemInfo to an item object using ng-model. Rather than placing the text in a value attribute I just initialized the model value using ng-init.
<tr ng-repeat="item in Items">
<td>
<input type="text" ng-model="item.itemName">
</td>
<td>
<select ng-model="item.itemType">
<option value="">--Select--</option>
<option vaue="fruits">Fruits</option>
<option value="vegtables">Vegtables</option>
</select>
</td>
<td>
<div ng-switch on="item.itemType">
<input type="text" ng-model="item.itemInfo" ng-switch-when="vegtables" ng-init="item.itemInfo='you selected '+ item.itemName">
</div>
</td>
</tr>
Also I had to change the Items array to match your requested naming:
$scope.Items = [{itemName: "Mango"}, {itemName: "Carrot"}, {itemName: "Apple"}];
You can parse the JSON object in a for loop to get the selected value and add the new key value pair.
angular.forEach($scope.vegetables, function(value, key){
if(value.itemName== 'Carrot')
value.iteminfo = 'you selected Carrot';
});
I guess you might be looking for something like this, if I am not wrong.

Get encompassing model on ng-change

I apologize for the confusing title, the problem is quite simple.
I have a $scope.users variable. I ng-repeat them and create a input for every user's respective valid until property. On a ng-change on this input I want to send a update request on my User service. How can I get the proper user on my ng-change?
<div class="row" ng-repeat="user in users">
<input type="date" ng-change="setValidUntil" value="{{user.validUntil}}"/>
//...other user properties
</div>
//in appropriate controller
$scope.setValidUntil = function () {
User.update({
'studentNumber': $scope.user.studentNumber, // Missing this!!
'validUntil':validUntil
});
}
Pass the user into the function
<div class="row" ng-repeat="user in users" style="margin-bottom:0.5em">
<input type="date" ng-change="setValidUntil(user)" value="{{user.validUntil}}"/>
//...other user properties
</div>
//in appropriate controller
$scope.setValidUntil = function (data) {
//you can update your data like that.
}

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.

angularjs: event before change the radio button value

How do I identify the change in value of a "radio button" before the current value change?
In my application, I need to alert the User about the effects that changing the value of this radio button will cause in the rest of form.
I've tried using ngChange and ngClick, but the value of the radio button is always changed (to the new value) even before I can do something with the current value.
Example:
<form>
<label>Nivel de gestão</label>
<input name="gestao" type="radio" data-ng-model="nivelGestao" value="T" data-ng-change="mudarNivelGestao()">Tecnica
<input name="gestao" type="radio" data-ng-model="nivelGestao" value="O" data-ng-change="mudarNivelGestao()">Operacional
<input name="gestao" type="radio" data-ng-model="nivelGestao" value="I" data-ng-change="mudarNivelGestao()">Institucional
<table>
<tr>
<td>Id</td>
<td>Nome</td>
</tr>
<tr data-ng-repeat="gestor in gestores">
<td>{{gestor.id}}</td>
<td>{{gestor.nome}}</td>
</tr>
</table>
<script>
...
$scope.mudarNivelGestao = function() {
alert($scope.nivelGestao); // In this place the content has already been changed, but before I need to alert and ask if the User want continue, if "No" return to the last value, if "Yes" change the value and go ahead...
...
}
...
</script>
</form>
I answered a similar question here
Just surround the radio input with label and apply the confirm logic in labels ng-click event.
<label ng-click="confirmChange($event)"><input type="radio" ng-model="value" value="foo" ></label>
$scope.confirmChange = function(event) {
if(!confirm('sure?')) {
event.preventDefault();
}
};
You can use the mousedown event (which will fire before the click and change events).
E.g.:
<input type="checkbox" ng-model="option"
ng-mousedown="confirmOption($event)" />
$scope.confirmOption = function (evt) {
var confirmMsg = 'Are you sure you want to select this ?';
if (!$scope.option) {
var confirmed = confirm(confirmMsg);
$scope.option = confirmed;
}
};
See, also, this short demo.

Resources