selecting checkbox id from table in Angularjs - angularjs

I am new to Angularjs. I have created a table using Angularjs. I am populating the content of the table from the controller and using ng-repeat in the html to render the data.
I also need to handle the checkbox check event and populate the array in the controller. I have added the checkboxes in the view. I am able to bind the content but when I am not able to handle the the checkbox event when checked. How to handle this in Angularjs. I have added the ng-change and ng-model but I want to populate the selected checkbox id from the $scopr.update function. Currently whenever there is change event, the function is called with the id, but I don't know if the checkbox is checked on unchecked. Where I am going wrong?
Code for the UI
<div class="ibox-content">
<div class="row">
<div class="col-sm-4 m-b-xs">
<div data-toggle="buttons" class="btn-group">
<label class="btn btn-sm btn-white"> <input type="radio" id="option1" name="options">10</label>
<label class="btn btn-sm btn-white"> <input type="radio" id="option2" name="options">25</label>
<label class="btn btn-sm btn-white"> <input type="radio" id="option3" name="options">50</label>
</div>
</div>
<div class="col-sm-3 pull-right">
<div class="input-group">
<div class="input-group-addon"><i class="fa fa-search"></i></div>
<input type="text" class="form-control" placeholder="Search Host" ng-model="name">
</div>
</div>
</div>
<div class="table-responsive">
<table class="table table-striped table-bordered">
<thead>
<tr>
<td>Select</td>
<td>
<a href="#" ng-click="sortType = 'name'; sortReverse = !sortReverse">
Hostname
<span ng-show="sortType == 'name' && !sortReverse" class="fa fa-caret-down"></span>
<span ng-show="sortType == 'name' && sortReverse" class="fa fa-caret-up"></span>
</a>
</td>
<td>
<a href="#" ng-click="sortType = 'application'; sortReverse = !sortReverse">
Application
<span ng-show="sortType == 'application' && !sortReverse" class="fa fa-caret-down"></span>
<span ng-show="sortType == 'application' && sortReverse" class="fa fa-caret-up"></span>
</a>
</td>
<td>
<a href="#" ng-click="sortType = 'environment'; sortReverse = !sortReverse">
Environment
<span ng-show="sortType == 'environment' && !sortReverse" class="fa fa-caret-down"></span>
<span ng-show="sortType == 'environment' && sortReverse" class="fa fa-caret-up"></span>
</a>
</td>
</tr>
</thead>
<tbody>
<tr dir-paginate="host in newHosts | orderBy:sortType:sortReverse | filter:name | itemsPerPage: 10
" pagination-id="host">
<td><input type="checkbox" ng-change="update(host.id)" ng-model="host_id"/></td>
<td>{{host.name}}</td>
<td>{{host.application}}</td>
<td>{{host.environment}}</td>
</tr>
</tbody>
</table>
</div>
</div>
In the controller:
$scope.selectedHost=[];
$scope.sortType = 'name'; // set the default sort type
$scope.sortReverse = false; // set the default sort order
$scope.searchHost = ''; // set the default search/filter term
$scope.newHosts=[
{
"name": "Pradeep",
"application": "MMC",
"environment": "Prod"
},
{
"name": "Praveen",
"application": "TTC",
"environment": "Pre"
}
]
$scope.update=function(hostId){
console.log(hostId);
}

First, add the ng-click into your checkbox
<td><input type="checkbox" ng-click=update(host)/></td>
Then call the update function in the controller
$scope.update = function(host){
var hostIndex = $scope.selectedHost.indexOf(host);
if(hostIndex == -1) {
$scope.selectedHost.push(host); //Add the selected host into array
} else {
$scope.selectedHost.splice(hostIndex, 1); //Remove the selected host
}
}

U could send inde as well by using $index to the function and it will give you the
exact index of the selected row, so that u can get it from your array,
as well as u can do more code by using this, u are not required to get
index ( a probability to not to get right index in some cases)
<td><input type="checkbox" ng-click=update($index)/></td>
$scope.update = function(hostIndex){
var host= $scope.selectedHost[hostIndex];
if(hostIndex == -1) {
$scope.selectedHost.push(host); //Add the selected host into array
} else {
$scope.selectedHost.splice(hostIndex, 1); //Remove the selected host
}
}

Related

AngularJs disabled directive not updated on ng-click but after refreshing it will work fine

When i click on Book button , buttons should be disabled if status is booked but is not updated or disabled directive not updated ,but when i refresh the page then it works fine and button is updated
$scope.bookingData = function(){
$http.post('myUrl',$scope.myRequiredParameters).
then(function(response){
$scope.datas = response.data.data;//here i am getting my data successfully
});
}
$scope.booked = function(){
$http.post('myUrl',$scope.myRequiredParameters).
then(function(response){
$scope.bookingData = response.data.data;//here also i am getting my data successfully
$scope.bookingData();//here i am calling again bookingData function to update status in
});
}
<tr ng-repeat="data in datas" >
<td >
<input type="button" class="btn btn-danger"
value="Cancel" ng-click="cancelled()" ng-disabled="data.status =='BOOKED' " >
<input type="button" class="btn btn-success"
value="Book" ng-click="booked()" ng-disabled="data.status ='BOOKED' ">
</td>
<td>
<span class="col-md-2 col-xs-12 no-padding text-left-xs" >User Name. :- <b>{{data.name}}</b> </span>
<span class="col-md-3 col-xs-12 no-padding text-left-xs">Name :-<b>{{data.status}}</b> </span>
</td>
</tr>

Angularjs - uib-datepicker in an table

I am trying to display an uib-datepicker in a table.
<table ng-table="tableParamsReglement" class="table table-bordered table-hover" id="dynamic-table" >
<tr ng-repeat="activite in $data" >
<td class="text-center" data-title="'Date fin astreinte'" >
<div class="input-group">
<input id="field_dateReglementEffectifClient" type="text" class="form-control" name="dateReglementEffectifClient" uib-datepicker-popup="{{vm.formatDate}}" ng-model="activite.dateReglementEffectifClient"
is-open="vm.datePickerOpenStatus.dateReglementEffectifClient" />
<span class="input-group-btn">
<button type="button" class="btn btn-default" ng-click="vm.openCalendar('dateReglementEffectifClient')"><i class="glyphicon glyphicon-calendar"></i></button>
</span>
</div>
</td>
</tr>
</table>
Controller :
vm.datePickerOpenStatus = {};
vm.datePickerOpenStatus.dateReglementEffectifClient = false;
vm.openCalendar = function(date) {
vm.datePickerOpenStatus[date] = true;
};
When I click on the calendar button multiple calendars show. And I when I choose a date, it's applied on the last element of my table.
I don't know what I am doing wrong here.
Solved with :
<td class="text-center" data-title="'Date fin astreinte'" >
<div class="input-group">
<input type="text" class="form-control" uib-datepicker-popup="{{vm.formatDate}}" ng-model="activite.dateReglementEffectifClient"
is-open="activite.isOpen" />
<span class="input-group-btn">
<button type="button" class="btn btn-default" ng-click="vm.openCalendar($event, activite)"><i class="glyphicon glyphicon-calendar"></i></button>
</span>
</div>
</td>
vm.openCalendar = function($event, activite) {
$event.preventDefault();
$event.stopPropagation();
activite.isOpen = true;
};

Displaying a value from a JSON array to a table in HTML5

I am doing a function of reading a content of csv file (test.csv) and store it in a variable (dataArray) and I have to display the values in this dataArray as a table in HTML5. I move those values from dataArray to $scope.sushi and created a HTML table with this variable but the data is not displaying in a table.
app.js:
$.get("test.csv").then(function(data) {
//window.alert($scope.rowSplit(data,"#"));
var rows = data.split("\r\n");
// var array = string.split('#');
// window.alert("Rows"+rows[0]);
window.alert("inside datasplit"+rows[1]);
if(rows.length > 0){
var firstRowCells = rows[0].split(",");
var dataArray = new Array();
for (var i=1; i < rows.length; i++)
{
var cells = rows[i].split(",");
var obj = {};
for(var j=0; j < cells.length; j++)
{
obj[firstRowCells[j]] = cells[j];
}
dataArray.push(obj);
}
//window.alert(dataArray);
$scope.sushi=dataArray;
// window.alert("$scope.sushi*********************8"+JSON.stringify($scope.sushi));
}
html file sampletableangular.html
<div class="container">
<form>
<div class="form-group">
<div class="input-group">
<input type="text" class="form-control" placeholder="Filter" ng-model="searchcon">
</div>
</div>
</form>
<div id="exportdata">
<table class="table table-bordered table-striped tablelink">
<thead>
<tr>
<td>
<a href="#" ng-click="sortType = 'issueno'; sortReverse = !sortReverse">
issue no
<span ng-show="sortType == 'issueno' && !sortReverse" class="fa fa-caret-down"></span>
<span ng-show="sortType == 'issueno' && sortReverse" class="fa fa-caret-up"></span>
</a>
</td>
<td>
<a href="#" ng-click="sortType = 'dateassigned'; sortReverse = !sortReverse">
date assigned
<span ng-show="sortType == 'dateassigned' && !sortReverse" class="fa fa-caret-down"></span>
<span ng-show="sortType == 'dateassigned' && sortReverse" class="fa fa-caret-up"></span>
</a>
</td>
<td>
<a href="#" ng-click="sortType = 'assignedby'; sortReverse = !sortReverse">
assigned by
<span ng-show="sortType == 'assignedby' && !sortReverse" class="fa fa-caret-down"></span>
<span ng-show="sortType == 'assignedby' && sortReverse" class="fa fa-caret-up"></span>
</a>
</td>
<td>
<a href="#" ng-click="sortType = 'description'; sortReverse = !sortReverse">
description
<span ng-show="sortType == 'description' && !sortReverse" class="fa fa-caret-down"></span>
<span ng-show="sortType == 'description' && sortReverse" class="fa fa-caret-up"></span>
</a>
</td>
<td>
<a href="#" ng-click="sortType = 'priority'; sortReverse = !sortReverse">
priority
<span ng-show="sortType == 'priority' && !sortReverse" class="fa fa-caret-down"></span>
<span ng-show="sortType == 'priority' && sortReverse" class="fa fa-caret-up"></span>
</a>
</td>
<td>
<a href="#" ng-click="sortType = 'targetdate'; sortReverse = !sortReverse">
target date
<span ng-show="sortType == 'targetdate' && !sortReverse" class="fa fa-caret-down"></span>
<span ng-show="sortType == 'targetdate' && sortReverse" class="fa fa-caret-up"></span>
</a>
</td>
<td>
<a href="#" ng-click="sortType = 'status'; sortReverse = !sortReverse">
status
<span ng-show="sortType == 'status' && !sortReverse" class="fa fa-caret-down"></span>
<span ng-show="sortType == 'status' && sortReverse" class="fa fa-caret-up"></span>
</a>
</td>
<td>
<a href="#" ng-click="sortType = 'attachment'; sortReverse = !sortReverse">
attachment
<span ng-show="sortType == 'attachment' && !sortReverse" class="fa fa-caret-down"></span>
<span ng-show="sortType == 'attachment' && sortReverse" class="fa fa-caret-up"></span>
</a>
</td>
</tr>
</thead>
<tbody>
<tr ng-repeat="roll in sushi | orderBy:sortType:sortReverse | filter:searchcon">
<td ng-click="display(roll.issueno);">{{ roll.issueno }}</td>
<td>{{ roll.dateassigned }}</td>
<td>{{ roll.assignedby }}</td>
<td>{{ roll.description}}</td>
<td>{{ roll.priority}}</td>
<td>{{ roll.targetdate}}</td>
<td>{{ roll.status}}</td>
<td>{{ roll.attachment}}</td>
</tr>
</tbody>
</table>
</div>
and the contents in test.csv:
issue no ,date assigned ,assigned by ,description ,priority ,target date ,status ,attachment
1001,1/24/2015,TE_002,Dev,1,3/4/2016,pending,file1
1002,2/20/2016,TE_002,Dev,1,3/4/2016,pending,file52
1003,4/5/2016,SD_003,Des,2,3/1/2016,pending,file4
1004,2/1/2016,TE_002,Test,1,5/19/2016,completed,file23
1005,8/27/2016,TL_001,Des,3,12/18/2016,pending,file41
1006,10/16/2016,TE_002,Dev,3,10/25/2016,completed,file35
1007,7/13/2016,TE_003,Test,5,8/7/2016,pending,file12
I have to display the value in a csv file as a table in HTML using $scope.sushi variable.
It looks like you are mixing jQuery and Angular.
You make the request with jQuery $.get("test.csv"), then update the angular $scope. When you do this angular doesn't notice the change.
Try wrapping the $scope update with apply to force the angular digest:
$scope.$apply(function () {
$scope.sushi=dataArray;
});
Or use the angular $http service to make the request.

Uncaught TypeError: Cannot read property 'file' of undefined(…)

I am trying to upload an image from my html an on click of save button, i am calling an upload function in the controller. When i enter the upload function in controller, i am not able to access $scope to check the $scope.file.name.
//upload image.html
<div class="horizontal">
<table border=1 frame=void rules=rows class="ui celled table" >
<thead style="text-align: center;">
<tr>
<th> Id </th>
<th> Question </th>
<th> Option A </th>
<th> Option B </th>
<th> Option C </th>
<th> Option D </th>
<th> Answer </th>
<th> Section id </th>
<th> Image </th>
<th> Edit</th>
</tr></thead>
<tbody ng-repeat="ques in questionObj | filter: searchText" style="text-align: center;">
<tr>
<td>{{$index + 1}}</td>
<td><span ng-show="editEnabled" ng-model="Title">
{{ ques.Title || 'empty' }}
</span>
<div ng-hide="editEnabled">
<textarea ng-model="ques.Title"></textarea>
</div>
</td>
<td><span ng-show="editEnabled" ng-model="Title">{{ques.Option_a || 'empty'}} </span>
<div ng-hide="editEnabled" class="option">
<textarea ng-model="ques.Option_a"></textarea>
</div>
</td>
<td><span ng-show="editEnabled" ng-model="Title">{{ques.Option_b || 'empty'}} </span>
<div ng-hide="editEnabled" class="option">
<textarea ng-model="ques.Option_b"></textarea>
</div>
</td>
<td><span ng-show="editEnabled" ng-model="Title">{{ques.Option_c || 'empty'}} </span>
<div ng-hide="editEnabled" class="option">
<textarea ng-model="ques.Option_c"></textarea>
</div>
</td>
<td><span ng-show="editEnabled" ng-model="Title">{{ques.Option_d || 'empty'}} </span>
<div ng-hide="editEnabled" class="option">
<textarea ng-model="ques.Option_d"></textarea>
</div>
</td>
<td><span ng-show="editEnabled" ng-model="Title">{{ques.Answer || 'empty'}} </span>
<div ng-hide="editEnabled" class="option">
<textarea ng-model="ques.Answer"></textarea>
</div>
</td>
<td><span ng-show="editEnabled" ng-model="Title">{{ques.Section_id || 'empty'}} </span>
<div ng-hide="editEnabled" class="option">
<input type="text" ng-model="ques.Section_id"></input>
</div>
</td>
<td>
<span ng-if="ques.Image != 'nil'" ng-show="editEnabled" ng-model="Title"> <img ng-src="{{ques.Image}}" class="image-container" /></span>
<span ng-if="ques.Image === 'nil'" ng-show="editEnabled" ng-model="Title">No Image</span>
<div ng-if="ques.Image != 'nil'" ng-hide="editEnabled" class="option">
<img ng-src="{{ques.Image}} " class="image-container" />
</div>
<div ng-if="ques.Image === 'nil'" ng-hide="editEnabled" class="option">
<input class="bottom-marg-15" type="file" name="file" file onchange="angular.element(this).scope().imageLoad(this)"></input>
<!-- Progress Bar -->
<div class="progress">
<div class="progress-bar" role="progressbar" aria-valuenow="{{ uploadProgress }}" aria-valuemin="0" aria-valuemax="100" style="width: {{ uploadProgress }}%;">
{{ uploadProgress == 0 ? '' : uploadProgress + '%' }}
</div>
</div>
<div ng-repeat="step in stepsModel">
<img class="small-thumb" ng-src="{{step}}" />
</div>
</div>
</td>
<td style="white-space: nowrap">
<div class="buttons" ng-show="editEnabled" ng-show="editEnabled">
<button class="btn btn-primary" ng-click="editEnabled = !editEnabled">edit</button>
<button class="btn btn-danger" ng-click="question.removeUser($index,ques.Id)">del</button>
</div>
<div ng-hide="editEnabled" class="form-buttons form-inline">
<button ng-model="Title" ng-disabled="editQuestionForm.$waiting" ng-click=" upload(); editEnabled = !editEnabled" class="btn btn-primary">
save
</button>
<button type="button" ng-click="editEnabled = !editEnabled" class="btn btn-default">
cancel
</button>
</div>
</td>
</tr>
</tbody>
this is the controller which has the upload function. I am not able to access $scope inside upload function.
'use strict';
angular.module('onlineTestAngularApp')
.controller('editQuestionCtrl', function($scope, GetQuestionsService, $window, $location, localStorageService, ENV) {
var vm = this;
vm.success = false;
vm.auth_token = localStorageService.get('rec-auth-token');
vm.role = localStorageService.get('role');
$scope.editEnabled = true;
$scope.access_key = ENV.access_key;
$scope.secret_key = ENV.secret_key;
$scope.bucket = "q-auth/angular_test/";
$scope.stepsModel = [];
$scope.imageLoad = function(element){
var reader = new FileReader();
reader.onload = $scope.imageIsLoaded;
reader.readAsDataURL(element.files[0]);
}
$scope.imageIsLoaded = function(e){
$scope.$apply(function() {
$scope.stepsModel.push(e.target.result);
});
}
$scope.upload = function($scope){
console.log("inside upload");
}
});
Your upload() is having a parameter called $scope, remove it.
Change:
$scope.upload = function($scope){
console.log("inside upload");
}
To:
$scope.upload = function(){
console.log("inside upload");
};
Because when you call the function upload() it will expect to pass an argument, $scope in the function is taken as local variable of that function, hence becoming undefined.
Once you enter the controller's function you have access to its $scope. There's no need to pass it as a parameter:
Controller
$scope.upload = function() {
console.log($scope.stepsModel) // or any other property
}
It seems you mix two techniques for accessing controller's scope: vm and $scope. My advice is to use vm only for exposing the controller variables to the view. Using $scope will soon be deprecated and I use it mainly for $watching changes in the view.

Angularjs predicate on ng-if is not working

I am trying to implement sorting for my AngularJS application using Predicate function. This is my code
<td>
<a href="" ng-click="predicate = 'date'; reverse=!reverse">
Time
<span ng-show="predicate == 'date'">
<span ng-show="!reverse">
<img src="img/ascending.gif">
</span>
<span ng-show="reverse">
<img src="img/descending.gif">
</span>
</span>
</a>
</td>
<td ng-if="SId==2">
<a href="" ng-click="predicate = 'name'; reverse=!reverse">
Name
<span ng-show="predicate == 'name'">
<span ng-show="!reverse">
<img src="img/ascending.gif">
</span>
<span ng-show="reverse">
<img src="img/descending.gif">
</span>
</span>
</a>
</td>
<td ng-if="SId==2">
<a href="" ng-click="predicate = 'rno'; reverse=!reverse">
RNO
<span ng-show="predicate == 'rno'">
<span ng-show="!reverse">
<img src="img/ascending.gif">
</span>
<span ng-show="reverse">
<img src="img/descending.gif">
</span>
</span>
</a>
</td>
<td ng-if="SId==1">
<a href="" ng-click="predicate = 'phoneNumber'; reverse=!reverse">
Phone Number
<span ng-show="predicate == 'phoneNumber'">
<span ng-show="!reverse">
<img src="img/ascending.gif">
</span>
<span ng-show="reverse">
<img src="img/descending.gif">
</span>
</span>
</a>
</td>
I have two dashboards, which are using the same HTML page. But Column names are different. So, column names are displayed based ng-if condition (SId = 1 is first dashboard & SId = 2 is second dashboard). Now columns are shown correctly in both dashboards. But when I implement sorting upon column names, the column which is having ng-if condition is not sorting.
ng-if has its own scope. So when you do predicate = 'rno' inside an ng-if, you're creating a predicate attribute in the ng-if scope instead of modifying the controller scope's predicate.
Use a function to set the predicate:
$scope.sortBy = function(property) {
$scope.predicate = property;
$scope.reverse = !$scope.reverse;
}
and
<td ng-if="SId==2"><a href="" ng-click="sortBy('name')">

Resources