How to display json data to table on Html page using angularjs - html

please im trying to display my json file on html page using angularjs to get the data from a source and displaying the data on a table format whenever the respective link to the data is clicked. I'm new to angular.
---this is my app.js file---
var app = angular.module('dapp',['ngRoute']);
app.config(function($routeProvider) {
$routeProvider
// route for the home page
.when('/', {
templateUrl: 'templates/home.html',
controller: 'homeController'
})
// route for the about page
.when('/facilities', {
templateUrl: 'templates/facilties.html',
controller: 'facilitiesController'
});
});
----this is my facilities.html page----
<div class="span3">
<ul class="nav nav-list well">
<li ng-repeat="facility in facilities" ng-bind="facility.Province">
</li>
</ul>
<!--<div ng-repeat="facility in facilities" ><div>Type:{{facility.type}}-->
</div>
<div class="table">
<p ng-hide="facilities">No facility found</p>
<table class="table table-striped table-bordered table-hover" ng-show="facilities">
<thead>
<tr>
<th>Facility Id</th>
<th>Name of Facility</th>
<th>Municipality</th>
<th>Province</th>
<th>Year of Construction</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="facility in facilities">
<td class="span2" ng-bind="facility.id">{{facility.id}}</td>
<td class="span2" ng-bind="facility.name">{{facility.name}}</td>
<td class="span2" ng-bind="facility.municipality"></td>
<td class="span2" ng-bind="facility.province">{{facility.province}}</td>
<td class="span2" ng-bind="facility.yearOfConstruction"></td>
</tr>
</tbody>
</table>
<div class="row controls">
<form ng-click="fetchFacilities()">
<input type="submit" class="btn btn-primary btn-lg" value="indoor sport facilities">
</form>
</div>
</div>
-----this is the function i created for fetching the data----
$scope.fetchFacilities = function() {
$http.get("thesis-folorunsho.c9users.io/facility").then(function(response) {
console.info('Facilities:', response.data);
$scope.facilities = response.data;
});
};

Related

Create a show all button to show hidden row

My code is like this: when you check the 'hide' column, the whole row is hidden. I also have a 'Show All' checkbox. When I check it, all rows (including the hidden ones) are shown. But when I uncheck it, all rows disappear. How can I make it so only the hidden rows disappear when I uncheck 'Show All', and the other rows still remain visible?
<body ng-app="myApp" ng-controller="myController">
<h1>Fruits in Vietnam</h1>
<hr>
<form name="myForm">
<div class="input-group input-group-lg">
<input type="text" class="form-control" name="mdfruitname" id="txtfruitname" ng-model="mdfruitname"
required>
<div class="input-group-append">
<button class="btn btn-outline-secondary" type="button" ng-click="addnew()"
ng-disabled="myForm.mdfruitname.$pristine || myForm.mdfruitname.$invalid">Add
</button>
</div>
</div>
</form>
</div>
<table class="table table-striped">
<thead>
<tr>
<th>Description</th>
<th>Hide</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="fruit in fruitnameDetail" ng-show="showall" ng-hide="hidef">
<td>{{fruit.fruitname}}</td>
<td><input type="checkbox" ng-model="hidef"></td>
<td>
<a class="btn btn-xs delete-record" ng-click="removefruitname($index)">
<i class="glyphicon glyphicon-trash"></i>
</a>
</td>
</tr>
</tbody>
</table>
<div><input type="checkbox" ng-model="showall"><span>SHOW ALL</span></div>
</body>
<script>
var app = angular.module("myApp", []);
app.controller('myController', function ($scope, $window) {
$scope.fruitnameDetail = [
{
"fruitname": "Banana"
},
{
"fruitname": "Mango"
},
{
"fruitname": "Orange"
}
];
$scope.addnew = function () {
var fruitnameDetail = {
fruitname: $scope.mdfruitname
};
$scope.fruitnameDetail.push(fruitnameDetail);
$scope.mdfruitname = '';
};
$scope.removefruitname = function (index) {
var name = $scope.fruitnameDetail[index].fruitname;
if ($window.confirm("Do you want to delete " + name + " ?")) {
$scope.fruitnameDetail.splice(index, 1);
}
};
});
</script>
Do not use ng-show and ng-hide at the same time. Just change a little bit your condition for ng-repeat. Here the example that should work:
<body ng-app="myApp" ng-controller="myController">
<h1>Fruits in Vietnam</h1>
<hr>
<form name="myForm">
<div class="input-group input-group-lg">
<input type="text" class="form-control" name="mdfruitname" id="txtfruitname" ng-model="mdfruitname"
required>
<div class="input-group-append">
<button class="btn btn-outline-secondary" type="button" ng-click="addnew()"
ng-disabled="myForm.mdfruitname.$pristine || myForm.mdfruitname.$invalid">Add
</button>
</div>
</div>
</form>
</div>
<table class="table table-striped">
<thead>
<tr>
<th>Description</th>
<th>Hide</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="fruit in fruitnameDetail" ng-show="showall || !hidef">
<td>{{fruit.fruitname}}</td>
<td><input type="checkbox" ng-model="hidef"></td>
<td>
<a class="btn btn-xs delete-record" ng-click="removefruitname($index)">
<i class="glyphicon glyphicon-trash"></i>
</a>
</td>
</tr>
</tbody>
</table>
<div><input type="checkbox" ng-model="showall"><span>SHOW ALL</span></div>
</body>

Angular showing json object in html table

Hello guys i have json data from firestore and i want to display these data in html table.
This is my customer-list.component.html
<body>
<div class="container">
<div class="title">
<h3>Customer Table</h3>
</div>
<div class="row">
<div class="col-md-12">
</div>
<div class="col-lg-8 col-md-10 ml-auto mr-auto">
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th class="text-center">#</th>
<th>Name</th>
<th>Age</th>
<th>Active</th>
</tr>
</thead>
<tbody>
<div *ngFor="let customer of customers" style="width: 300px;">
<app-customer-details [customer]='customer'></app-customer-details>
</div>
<div style="margin-top:20px;">
<button type="button" class="button btn-danger" (click)='deleteCustomers()'>Delete All</button>
</div>
</tbody>
</table>
</div>
</div>
Seems like there is nothing wrong in there.
This is my customer-details.component.html
<div *ngIf="customer">
<tr>
<td>
<label>First Name: </label> {{customer.name}}
</td>
<td>
<label>Age: </label> {{customer.age}}
</td>
<td>
<label>Active: </label> {{customer.active}}
</td>
<span class="button is-small btn-primary" *ngIf='customer.active' (click)='updateActive(false)'>Inactive</span>
<span class="button is-small btn-primary" *ngIf='!customer.active' (click)='updateActive(true)'>Active</span>
<span class="button is-small btn-danger" (click)='deleteCustomer()'>Delete</span>
</tr>
</div>
But my table output is :
Thanks for answers.
When you look at the DOM tree you will find out that
<tr><app-customer-details>...</app-customer-details</td>
This will not result in the component rendering inside the table as a but will cram the entire component HTML template inside one column.
By adding an attribute selector to the component i.e. selector: 'app-customer-details, [app-customer-details]', you can add the selector directly to the element like so and now the elements inside the component HTML template will render correctly inside the table.
Look at this link;
The correct code is as follows:
Customer-list.component.html
<div class="container">
<div class="title">
<h3>Customer Table</h3>
</div>
<div class="row">
<div class="col-md-12">
</div>
<div class="col-lg-8 col-md-10 ml-auto mr-auto">
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<!-- <th class="text-center">#</th> -->
<th>Name</th>
<th>Age</th>
<th>Active</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let customer of customers" [customerDetails]="customer">
</tr>
<div style="margin-top:20px;">
<button type="button" class="button btn-danger" (click)='deleteCustomers()'>Delete All</button>
</div>
</tbody>
</table>
</div>
</div>
</div>
</div>
Customer-details.component.html
<td>
{{customerDetails.name}}
</td>
<td>
{{customerDetails.age}}
</td>
<td>
{{customerDetails.active}}
</td>
Customer-details.component.ts
import { Component, OnInit, Input } from '#angular/core';
#Component({
selector: 'customerDetails, [customerDetails]',
templateUrl: './customer-details.component.html',
styleUrls: ['./customer-details.component.css']
})
export class CustomerDetailsComponent implements OnInit {
#Input() customerDetails: object = {};
constructor() { }
ngOnInit() {
}
}
Don´t limit the width of a customer´s component to 300px, like you did here:
<div *ngFor="let customer of customers" style="width: 300px;">
You have many things which will not render your table properly:
<tbody>
<div *ngFor="let customer of customers" style="width: 300px;">
<app-customer-details [customer]='customer'>
</app-customer-details>
</div>
....
</tbody>
This will render like this:
<tbody>
<div ...>
<app-customer-details>
<div> <tr>.....</tr></div>
</app-customer-details>
</div>
....
</tbody>
Which is not correct HTML.
Change you detail component to use ng-template:
<ng-template >
<tr *ngIf="customer">
<td><label>First Name: </label> {{customer.name}}</td>
<td><label>Age: </label> {{customer.age}}</td>
<td><label>Active: </label> {{customer.active}}</td>
<td>
Note: All spans should also be inside a TD tag
</td>
</tr>
</ng-template>

Checkbox in table to select table row in Angular

I have a table in Angular 6, it has a checkbox in there? What I want to be able to do is select the table row, i.e checkbox, then hit a select button and that gets submitted. I was wondering does the table have to be wrapped in a <form>. The structure of my HTML so far is but it does not work:
<form [formGroup]="assetForm" (ngSubmit)="onSubmit()">
<table class="table table-striped table-hover mb-10">
<thead>
<tr>
<th>Number</th>
<th>Sev</th>
<th>Phone</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let incident of data">
<td>
<label class="form-radio">
<input type="radio" name="id_number" value="id_number">
<i class="form-icon"></i>{{incident.number}}
</label></td>
<td>{{incident.sev}}</td>
<td>{{incident.phone}}</td>
</tr>
</tbody>
</table>
<button class="btn btn-primary" [disabled]="!Form.valid" type="submit">Select</button>
</form>
Ts.file
onSubmit() {
if (this.assetForm.invalid) {
this.assetForm.setErrors({ ...this.assetForm.errors, 'required': true });
return;
}
this.uploading = true;
this.service.post(this.assetForm.value).subscribe((response: any) => {
console.log(response);//On success response
}, (errorResponse: any) => {
console.log(errorResponse); //On unsuccessful response
this.error = true;
this.uploading = false;
});
}
<tr *ngFor="let incident of data">
<td>
<label class="form-radio">
<input type="radio" name="id_number" value="id_number" [(ngModel)]="incident.checked">
<i class="form-icon"></i>{{incident.number}}
</label>
</td>
<td>{{incident.sev}}</td>
<td>{{incident.phone}}</td>
</tr>
Try like, hope it will work.

Add elements below row <td>

In my angularjs app there a rows (using ng-repeat), created through .
The code creating rows is below:
<tbody>
<tr ng-repeat="data in downloads">
<td data-title="ID">{{$index + 1}}</td>
<td data-title="User">{{data.user_name}}</td>
<td data-title="Issue">{{data.download_name}}</td>
<td data-title="Progress">{{data.size}}</td>
<td data-title="Completed time" am-time-ago="data.completed_time|amFromUnix"</td>
<td class="information-parent" data-title="More">
<md-icon ng-click="switchIcon()" class="bassa-red-color">{{icon}}</md-icon
</td>
</tr>
</tbody>
I'm trying to implement something that when you click the arrow, the row expands with information appearing like this:
I however can not get the information to appear along the bottom like that. What should I be trying to implement to get text along the bottom - flex box, inline-box ?
Take a look at Bootstrap collapse.js. I think this is what you are looking for, of course, you have to change it in order to meet your needs.
Try below code for your scenario.
(function(ng, app){
app = angular.module('app', [])
app.controller("mainController", ['$scope',
function($scope) {
$scope.downloads = [{
"user_name": "John",
"download_name": "Doe jhonejhonejhone",
"size": "0 byte"
},
{
"user_name": "Anna",
"download_name": "Doe DoeDoeDoeDoe",
"size": "0 byte"
},
{
"user_name": "Maron",
"download_name": "Anna DoeDoeDoeDoeDoe",
"size": "5 byte"
}
];
$scope.editFiles = function() {
$scope.editBar = true;
};
$scope.remove = function(index) {
$scope.downloads.splice(index, 1);
};
}
]);
}(angular));
.btn-primary{
margin-right: 10px;
}
<html ng-app="app">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container" ng-controller="mainController">
<div class="row">
<div class="col-md-12">
<input type="submit" class="btn btn-primary addnew pull-left" value="Edit Files" ng-click="editFiles()">
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-body">
<form>
<table border=1 class="table table-striped table-bordered">
<thead>
<tr>
<th>
<b> # </b>
</th>
<th>User</th>
<th>Download Name</th>
<th>Complete Name</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="data in downloads">
<td>
<b> {{$index + 1}} </b>
</td>
<td>
{{data.user_name}}
</td>
<td>
{{data.download_name}}
<div class="btn-group pull-right" ng-show="editBar">
<button class="btn btn-primary btn-xs"><span class="glyphicon glyphicon-pencil"></span></button>
<button class="btn btn-danger btn-xs"><span class="glyphicon glyphicon-trash" ng-click="remove($index)"></span></button>
</div>
</td>
<td>
{{data.size}}
</td>
</tr>
</tbody>
</table>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
You can try the following:
move your ng-repeat to your tbody
add ng-init="data.showSubRow = false" to your tbody
add another row to the tbody that should only be visible when showSubRow is true
so your ng-repeat would look like
<tbody ng-repeat="data in downloads" ng-init="data.showSubRow = false">
<tr>
visible row
</tr>
<tr ng-show="data.showSubRow">
visible when clicked
</tr>
</tbody>
now you can add individual buttons in each row to show / hide content on individual rows or add an edit all function that would make all subrows visible
show / hide individual row
<tr>
<td data-title="ID">
{{$index + 1}
<span>
<button ng-show="!data.showSubRow" ng-click="data.showSubRow = true">+</button>
<button ng-show="data.showSubRow" ng-click="data.showSubRow = false">-</button>
</span>
</td>
<td data-title="User">{{data.user_name}}</td>
<td data-title="Issue">{{data.download_name}}</td>
<td data-title="Progress">{{data.size}}</td>
<td data-title="Completed time" am-time-ago="data.completed_time|amFromUnix"</td>
<td class="information-parent" data-title="More">
<md-icon ng-click="switchIcon()" class="bassa-red-color">{{icon}}</md-icon
</td>
</tr>
display all
$scope.editAll = function(){
angular.forEach($scope.downloads, function(value, key) {
value.showSubRow = !value.showSubRow;
});
}
Working sample ----> Demo

AngularJS processing input value

When i start typing a name of student, like "M", the list of student needs to include only students, name of that are starting on letter "M" and so on rest of the word. But input value .length is undefined all the time.
(function() {
var app = angular.module('app', []);
app.controller('DataController', function() {
this.students = arr;
this.compare = function() {
for (var i = 0; i < this.text.length; i++) {
if (this.text[i] == this.students.name[i]) {
alert(this.text);
return true;
}
}
}
});
var arr= [{
name: 'Azurite',
price: 2.95
}, {
name: 'Bloodstone',
price: 5.95
}, {
name: 'Zircon',
price: 3.95
}];
}());
HTML
<div class="container-fluid">
<div class="row-fluid">
<input class="col-md-12 col-xs-12" type="text" placeholder="Search people by name..." ng-model='text' ng-change='data.students.name'>
<div class="buttons">
<button class="btn btn-sort">Sort by name</button>
<button class="btn btn-sort">Sort by age</button>
<button ng-click='data.click()' class="btn btn-danger">Reset</button>
</div>
</div>
<!--Main content-->
<div class="main-table col-sm-8 col-md-7 col-md-offset-1 col-lg-7 col-lg-offset-1">
<table class="table table-hover">
<thead>
<tr>
<th class="text-center">Image</th>
<th>Name</th>
<th>Age</th>
<th>Phone</th>
</tr>
</thead>
<tbody ng-repeat='student in data.students' ng-show='data.compare()'>
<tr>
<td>
<img src="/img/cat.svg" alt="">
</td>
<td>{{student.name}}</td>
<td>41</td>
<td>sieg#example.com</td>
</tr>
</tbody>
</table>
</div>
</div>
The angular way to do this is to use the filter feature built in to angular.
first, the text box you want to type your search into:
<input class="col-md-12 col-xs-12" type="text"
placeholder="Search people by name..." ng-model='data.text'>
Note that this only needs an ng-model value.
Next, the ng-repeat:
<tbody ng-repeat='student in data.students | filter:data.text'>
Nothing is necessary at all in the controller.
http://plnkr.co/edit/aoR2ZkrgjCvhC60Ky0QA?p=preview