Sorting Table with JSON key with a space - angularjs

I am trying to implement a sort functionality on a table.
I am able to sort table headers with json(no spaces). However, I am unable to sort table headers with JSON key with spaces
How to solve this problem?
Script:
$scope.sortType = 'Age';
$scope.sortReverse = false;
HTML:
<table border="1" class="table table-striped">
<thead>
<tr>
<th>
<a href="#" ng-click="sortType = x[First Name]">First Name
<span ng-show="sortType == x[First Name] && !sortReverse" class="fa fa-caret-down"></span>
<span ng-show="sortType == x[First Name] && sortReverse" class="fa fa-caret-up"></span>
</a>
</th>
<th>
<a href="#" ng-click="sortType = x[Last Name;sortReverse = !sortReverse">
Last Name
<span ng-show="sortType == x[Last Name] && !sortReverse" class="fa fa-caret-down"></span>
<span ng-show="sortType == x[Last Name] && sortReverse" class="fa fa-caret-up"></span>
</a>
</th>
<th>
<a href="#" ng-click="sortType = 'Age';sortReverse = !sortReverse">
Age
<span ng-show="sortType == 'Age' && !sortReverse" class="fa fa-caret-down"></span>
<span ng-show="sortType == 'Age' && sortReverse" class="fa fa-caret-up"></span>
</a>
</th>
<th>
<a href="#" ng-click="sortType = 'Address';sortReverse = !sortReverse">Address
<span ng-show="sortType == 'Address' && !sortReverse" class="fa fa-caret-down"></span>
<span ng-show="sortType == 'Address' && sortReverse" class="fa fa-caret-up"></span>
</a>
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="x in result | filter:searchText | orderBy:sortType:sortReverse">
<td>{{x['First Name']}}</td>
<td>{{x['Last Name']}}</td>
<td>{{x.Age}}</td>
<td>{{x.Address}}</td>
</tr>
</tbody>
</table>

You have syntax errors in your code which you should be seeing in your browser console.
Example:
<th>
<a href="#" ng-click="sortType = x[First Name]">First Name
<span ng-show="sortType == x[First Name] && !sortReverse" class="fa fa-caret-down"></span>
<span ng-show="sortType == x[First Name] && sortReverse" class="fa fa-caret-up"></span>
</a>
</th>
It seems like you should be using the string literal 'First Name' and not x[First Name] which is a syntax error and an undefined variable.
Try changing the above to:
<th>
<a href="#" ng-click="sortType = 'First Name'">First Name
<span ng-show="sortType == 'First Name' && !sortReverse" class="fa fa-caret-down"></span>
<span ng-show="sortType == 'First Name' && sortReverse" class="fa fa-caret-up"></span>
</a>
</th>
That should help with the syntactical issues which you should be seeing in your browser console right now and may solve the issue altogether.

Related

AngularJS Directive - ng-click not working when using the directive

I'm facing a problem with my new directive. The variables are not being actualized when ng-click. I've found some answers here but all of them call a method. I'm just changing the value of 2 variables when there's a click.
Directive:
pmcApp.directive('sorting', function() {
return {
restrict : 'AECM',
templateUrl : 'partials/directives/sorting.html',
replace : true,
scope : {
title : "#",
field : "#"
}
}
});
TemplateURL:
<a href="" ng-click="sortType = '{{field}}'; sortReverse = !sortReverse;">
{{title}}
<span ng-show="sortType == '{{field}}' && !sortReverse"
class="glyphicon glyphicon-sort-by-attributes font-size-10"></span>
<span ng-show="sortType == '{{field}}' && sortReverse"
class="glyphicon glyphicon-sort-by-attributes-alt font-size-10"></span>
</a>
html
<!-- Using directive - Not work when we click -->
<td><sorting title="First Name" field="firstName"></td>
<td><sorting title="Last Name" field="lastName"></td>
<!-- Not using directive - work when we click -->
<td>
<a href="" ng-click="sortType = 'username'; sortReverse = !sortReverse;">
Email
<span ng-show="sortType == 'username' && !sortReverse" class="glyphicon glyphicon-sort-by-attributes font-size-10"></span>
<span ng-show="sortType == 'username' && sortReverse" class="glyphicon glyphicon-sort-by-attributes-alt font-size-10"></span>
</a>
</td>
Code generated on browse is almost identical for all TDs as you can see bellow
<td>
<a href="" ng-click="sortType = 'firstName'; sortReverse = !sortReverse;" title="First Name" field="firstName" class="ng-binding ng-isolate-scope">
First Name
<span ng-show="sortType == 'firstName' && !sortReverse" class="glyphicon glyphicon-sort-by-attributes font-size-10 ng-hide"></span>
<span ng-show="sortType == 'firstName' && sortReverse" class="glyphicon glyphicon-sort-by-attributes-alt font-size-10 ng-hide"></span>
</a>
</td>
<td>
<a href="" ng-click="sortType = 'lastName'; sortReverse = !sortReverse;" title="Last Name" field="lastName" class="ng-binding ng-isolate-scope" >
Last Name
<span ng-show="sortType == 'lastName' && !sortReverse" class="glyphicon glyphicon-sort-by-attributes font-size-10 ng-hide"></span>
<span ng-show="sortType == 'lastName' && sortReverse" class="glyphicon glyphicon-sort-by-attributes-alt font-size-10 ng-hide"></span>
</a>
</td>
<td>
<a href="" ng-click="sortType = 'username'; sortReverse = !sortReverse;">
Email
<span ng-show="sortType == 'username' && !sortReverse" class="glyphicon glyphicon-sort-by-attributes font-size-10"></span>
<span ng-show="sortType == 'username' && sortReverse" class="glyphicon glyphicon-sort-by-attributes-alt font-size-10 ng-hide"></span>
</a>
</td>
When we click on Email it works normal. But when we click on First Name or Last Name which were created with the directive it doesn't work.
The sorting directive isn't change the property for the parent scope. You are modifying the local scope of the sorting directive and not the parent scope like email is.

angularjs: orderBy sort not working correctly for multiple field types

I have a table that contains multiple field types (string, dates, addresses). The orderBy seems to be working for Dates and Case Number, which contains both letters and numbers, however it doesn't seem to be working correctly for addresses and strings.
<thead>
<tr>
<th>
<input type="checkbox" id="selectAll" ng-model="selectAll" ng-click="checkAll()"/>
</th>
<th>
<a href="javascript:void(0)" ng-click="sortType = 'case_num'; sortReverse = !sortReverse">
<span ng-show="sortType == 'case_num' && !sortReverse" class="fa fa-arrow-down"></span>
<span ng-show="sortType == 'case_num' && sortReverse" class="fa fa-arrow-up"></span>
${Case Number}
</a>
</th>
<th>
<a href="javascript:void(0)" ng-click="sortType = 'short_desc'; sortReverse = !sortReverse">
<span ng-show="sortType == 'short_desc' && !sortReverse" class="fa fa-arrow-down"></span>
<span ng-show="sortType == 'short_desc' && sortReverse" class="fa fa-arrow-up"></span>
${Short Description}
</a>
</th>
<th>
<a href="javascript:void(0)" ng-click="sortType = 'start_date'; sortReverse = !sortReverse">
<span ng-show="sortType == 'start_date' && !sortReverse" class="fa fa-arrow-down"></span>
<span ng-show="sortType == 'start_date' && sortReverse" class="fa fa-arrow-up"></span>
${Start Date}
</a>
</th>
<th>
<a href="javascript:void(0)" ng-click="sortType = 'work_location'; sortReverse = !sortReverse">
<span ng-show="sortType == 'work_location' && !sortReverse" class="fa fa-arrow-down"></span>
<span ng-show="sortType == 'work_location' && sortReverse" class="fa fa-arrow-up"></span>
${Work Location}
</a>
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in c.onbCase | orderBy:sortType:sortReverse | filter:searchTable track by $index" ng-if="item.case_visible">
td><input type="checkbox" id={{item.number}} ng-click="checkboxFunc(item)" ng-model="item.checked" ng-checked="selectAll || item.checked"/></td>
<td>{{item.number}}</td>
<td>{{item.short_description}}</td>
<td>{{item.start_date}}</td>
<td>{{item.location}}</td>
</tr>
</tbody>
When I sort by Case Number and Start Date, the entries order correctly. However, when I try to sort by Short Description or Location, it doesn't seem to ordering it correctly:
Am I missing something? THanks!

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.

Why works wrong the angular filter?

PictureFilter
Angular's filter not works correctly, as you see in the picture, for seach 'shi' it is also find the abc-sea , nnnn-road row...
I tried with this:
<div flex="30">
<label>Search: <input ng-model="searchText"></label>
<table class="table table-hover" id="shipmentTable">
<thead>
<tr>
<th>
<a href="#" ng-click="sortType = 'name'; sortReverse = !sortReverse">
shipment name
<i ng-show="sortType == 'name' && !sortReverse"
class="fa fa-angle-up"></i>
<i ng-show="sortType == 'name' && sortReverse"
class="fa fa-angle-down"></i>
</a>
</th>
<th>
<a href="#" ng-click="sortType = 'transportMode'; sortReverse = !sortReverse">
transport mode
<i ng-show="sortType == 'transportMode' && !sortReverse"
class="fa fa-angle-up"></i>
<i ng-show="sortType == 'transportMode' && sortReverse"
class="fa fa-angle-down"></i>
</a>
</th>
</tr>
</thead>
<tbody class="member">
<tr ng-class='{selected: $index==selectedRow}'
ng-click="selectRow($index);getSelected(shipment);"
ng-repeat="shipment in shipments | orderBy:sortType:sortReverse | filter:searchText">
<td>{{shipment.name}}</td>
<td>{{shipment.transportMode}}</td>
</tr>
</tbody>
</table>
But its not working correctly....what can be a problem?
Currently you filter searching for contains in through out the object shipment, so current filter filter has shi somewhere there in there property. Do apply filter on specific property of object like {name: searchText},
ng-repeat="shipment in shipments | orderBy:sortType:sortReverse | filter:{name:searchText}"

Sorting data in AngularJS table by column

I'm working on a web application in AngularJS. Basically, it makes an HTTP request to a database server and displays information for all the claims in a table. The table shows 100 claims per page and there are ~131 pages. My problem is, when I try to sort the claim info by columns, it only sorts the claim information for each page and not for all the data (it's only working for the createdDate). Any ideas on how I con sort the data for the entire table (all pages) by column?
I'm very new to AngularJS MVC, am on a tight deadline, and haven't been able to implement ngTable (mainly because I don't understand where the $data and $dataset is coming from).
This is my view (HTML):
<div class="row" id="dvData">
<table class="table table-striped table-hover " >
<thead>
<tr>
<th>
<a href="" ng-click="sortType = 'userName'; sortReverse = !sortReverse">
Pass Code
<span ng-show="sortType == 'userName' && sortReverse" class="fa fa-caret-down"></span>
<span ng-show="sortType == 'userName' && !sortReverse" class="fa fa-caret-up"></span>
</a>
</th>
<th>
<a href="" ng-click="sortType = 'claimNumber'; sortReverse =! sortReverse">
Claim Number
<span ng-show="sortType == 'claimNumber' && sortReverse" class="fa fa-caret-down"></span>
<span ng-show="sortType == 'claimNumber' && !sortReverse" class="fa fa-caret-up"></span>
</a>
</th>
<th>Vehicle</th>
<th>Status</th>
<th>Company Name</th>
<th>
<a href="" ng-click="sortType = 'createdDate'; sortReverse = !sortReverse">
Created
<span ng-show="sortType == 'createdDate' && sortReverse" class="fa fa-caret-down"></span>
<span ng-show="sortType == 'createdDate' && !sortReverse" class="fa fa-caret-up"></span>
</a>
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="claim in claimList | filter:{claimNumber:searchClaimNumber} | orderBy:sortType:sortReverse">
<td>{{claim.userName==null?'Not Assigned':claim.userName}}</td>
<td>{{claim.claimNumber}}</td>
<td>{{getVehicle(claim)}}</td>
<td>{{getClaimStatus(claim)}}</td>
<td>{{getClientName(claim)}}</td>
<td>{{claim.createdDate| date:'MM/dd/yyyy h:mma'}}</td>
</tr>
</tbody>
</table>
controller:
angular
.module('mbeapp')
.controller('DashboardController', [
'$scope', 'ClaimService', 'SurveyDataService', '$state', '$http', 'NavigateService',
function($scope, ClaimService, SurveyDataService, $state, $http, NavigateService) {
[...]
$scope.sortType = 'createdDate'; // set the default sort type
$scope.sortReverse = true;
}
]);
In order to sort the entire data, you have two options.
You can load all data from backend at once and create a front-end pagination. Sort/Search will filter the whole data set and change the pagination accordingly.
When user wants to sort/search, you can pass that information to backend and get a new set of data based on the criteria.
Option 1 is easy to implement but it's a bad choice when you have a large data set. Option 2 is the ideal way to achieve your goal.
Sample Code
In your controller, you can write a function like the following:
$scope.sortBy = function(keyName){
$scope.sortReverse = !$scope.sortReverse;
$scope.sortType = keyName;
var reqParams = {
sortBy: keyName,
sortReverse: $scope.sortReverse
};
// do a $http request
$http.get('http://apiUrl', reqParams).then(function(response){
$scope.claimList = response.data; // update the claimList data
});
};
Sample table header may be like this:
<th>
<a href="" ng-click="sortBy('userName')">Pass Code
<span ng-show="sortType == 'userName' && sortReverse" class="fa fa-caret-down"></span>
<span ng-show="sortType == 'userName' && !sortReverse" class="fa fa-caret-up"></span>
</a>
</th>
You also need to change the following:
<tr ng-repeat="claim in claimList>

Resources