Angularjs predicate on ng-if is not working - angularjs

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')">

Related

I am unable to get menulist options into arraylist

HTML Code:
<td width="50%">
<div id="divselect-tab" style="width:100%;">
<div id="s2id_select-tab" class="select2-container FontFace" style="width:100%;">
<a class="select2-choice select2-default" href="javascript:void(0)" tabindex="-1">
<span id="select2-chosen-1" class="select2-chosen"/>
<abbr class="select2-search-choice-clos**`strong text`**e"/>
<span class="select2-arrow" role="presentation">
</a>
I got the exception:
Element should have been "select" but was "a".

angular-bootstrap 2 paginations, issue in second pagination

image showing paginations
I have item list with many tables in which initaily i show headers by loop each table header works as accordion and on click any table header one request send to get data for that header and tbody populate accordingly.
I have applied pagination on headers and its working fine but pagination on any tbody not working.
Below I have mentioned HTML code.
<div class="col-md-12">
<!-- start table one -->
<div class="table table-responsive series-tbl series-tbl-m">
<table data-ng-repeat="series in serieses track by series._id" class="table table-striped" id="headingOne">
<thead class="table-blue">
<tr >
<th width="5%"><input ng-click="checkAllTalks(series._id,series.Selected)" ng-model="series.Selected" selected="{{series.Selected}}" type="checkbox"></th>
<th width="28%">{{series.title}}</th>
<th width="22%">{{series.totalCount || 0}} talks</th>
<th width="15%">{{ series.totalDuration || 0 | timestring }}</th>
<th width="15%">${{series.totalPrice || 0}}</th>
<th width="18%" class="th-action-icon">
<!-- <i class="fa fa-plus"></i> -->
<a ng-click="openUpdateSeriesModal('md',series._id)" href="#"><i class="fa fa-pencil"></i></a>
<i class="fa fa-trash"></i>
<a role="button" ng-click="showTalks(series._id)" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne"><i class="fa fa-caret-down"></i></a>
</th>
</tr>
</thead>
<tbody id="series-{{series._id}}" class="panel-collapse collapse in dn allSeriesTalks" role="tabpanel" aria-labelledby="headingOne">
<tr ng-repeat='talk in seriesTalks track by talk._id'>
<td><input ng-click="checkUncheckTalk(talk._id,talk.Selected)" ng-model="talk.Selected" selected="{{talk.Selected}}" type="checkbox"></td>
<td>{{talk.title}}</td>
<td>{{talk.duration || 0 | timestring }}</td>
<td>{{bytesToMB(talk.size) || 0}} mb</td>
<td>${{talk.price || 0}}</td>
<td class="action-item">
<!-- <i class="fa fa-plus icon-gray"></i> -->
<a ng-click="openUpdateTalkModal('md',talk._id)" href="#"><i class="fa fa-pencil icon-gray" ></i></a>
<a ng-click="deleteTalk(talk._id)" href="javascript:void(0);"><i class="fa fa-trash icon-gray"></i></a>
</td>
</tr>
<tr>
<td colspan="6">
<div data-ng-if="totalTalkCount> 10" class="col-md-12">
<div pagination total-items="totalTalkCount"
ng-model="currentTalkPageNo"
max-size="maxSize"
class="pagination-sm pull-right"
boundary-links="true"
ng-change="talkPageChanged(currentSeriesId)"></div>
</div>
<div data-ng-if="totalTalkCount == 0" class="col-md-2">
<div>No record Found.</div>
</div>
</td>
</tr>
</tbody>
</table>
</div> <!-- end table one -->
</div>
<div class="col-md-12">
<div pagination total-items="totalItems"
ng-model="currentPage"
max-size="maxSize"
class="pagination-sm pull-right"
boundary-links="true"
ng-change="pageChanged()"></div>
</div>
And below controller code
//first it is working properly
vm.currentPage = 1;
vm.maxSize = 5;
vm.pageChanged = function () {
console.log('page changed:', vm.currentPage);
vm.getSeriesList();
//$scope.getMembers();
};
//second its not working
vm.currentTalkPageNo = 1;
vm.talkPaginate = false;
vm.talkPageChanged = function (seriesId) {
console.log('page talk changed:', vm.currentTalkPageNo);//each time it shows 1 page no
vm.talkPaginate = true;
vm.showTalks(seriesId);
//$scope.getMembers();
};
remove below code
data-ng-if="totalTalkCount == 0"
in pagination parent div to
data-ng-class="{ 'dn':totalTalkCount <= 10}"
And use
ng-model="$parent.currentTalkPageNo"
instead of
ng-model="currentTalkPageNo"
css
.dn{display:none;}

selecting checkbox id from table in 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
}
}

AngularJS - Basic ng-repeat with ng-switch

I have a simple ng-repeat, that displays a list of statuses. I wanted to extend this ng-repeat to ng-switch to display a value depending on the status.
Current ng-repeat:
<td ng-repeat="myStatus in history.Status.slice(0, 12)">
{{ myStatus }}
</td>
With ng-switch:
<td ng-repeat="myStatus in history.Status.slice(0, 12)" ng-switch="myStatus">
<span ng-switch-when="1">test1</span>
<span ng-switch-when="2">test2</span>
<span ng-switch-when="3">test3</span>
<span ng-switch-when="4">test4</span>
<span ng-switch-when="5">test5</span>
<span ng-switch-when="6">test6</span>
<span ng-switch-when="7">test7</span>
<span ng-switch-default>Error</span>
</td>
However, witht eh above ng-switch, nothing is displayed.
Works at my end, here is the Plunker
However as a suggestion i would prefer to use a function expression instead of ng-switch.
<td ng-repeat="myStatus in history.Status.slice(0, 12)">
<span>{{getStatus(myStatus)}}</span>
</td>
getStatus is a function which would return the required information.
Use This
<td ng-repeat="myStatus in history.Status.slice(0, 12)" >
<span ng-switch="myStatus">
<span ng-switch-when="1">test1</span>
<span ng-switch-when="2">test2</span>
<span ng-switch-when="3">test3</span>
<span ng-switch-when="4">test4</span>
<span ng-switch-when="5">test5</span>
<span ng-switch-when="6">test6</span>
<span ng-switch-when="7">test7</span>
<span ng-switch-default>Error</span>
</span>
</td>

ng-hide depending on value of var

In AngularJS, how can I do what the following code is trying to achieve, i.e. hide a span if task.status is set to a specific value?
<li ng-repeat="task in tasks">
Move to:
<span ng-hide="task.status=todo">Todo</span>
<span ng-hide="task.status=doing">Doing</span>
<span ng-hide="task.status=done">Done</span>
</li>
<li ng-repeat="task in tasks">
Move to:
<span ng-hide="task.status == todo">Todo</span>
<span ng-hide="task.status == doing">Doing</span>
<span ng-hide="task.status == done">Done</span>
</li>
or
<li ng-repeat="task in tasks">
<div ng-switch on="task.status" >
<span ng-switch-when="todo">Todo</div>
<span ng-switch-when="doing">Doing</div>
<span ng-switch-when="done">Done</div>
</div>
</li>

Resources