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

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.

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!

Sorting Table with JSON key with a space

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.

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}"

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
}
}

Resources