I am using ajax call to get data its return data in json format like below:
{ "d" : [
{ "Goal" : "Some one client",
"GoalID" : 1,
"IsPublic" : true,
"MemberName" : "user1"
},
{ "Goal" : " this is goal",
"GoalID" : 1,
"IsPublic" : false,
"MemberName" : "user2"
},
{ "Goal" : "Get call",
"GoalID" : 4,
"IsPublic" : true,
"MemberName" : "user2"
}
] }
Now in my html page i want to create separate table for each user. Here is my table
//want username here then table for goal of that user
<table>
<thead>
<tr class="headerRow">
<th>member</th>
<th>
Goals
</th>
</tr>
</thead>
<tbody data-bind="foreach: tasks">
<tr">
<td>
<span data-bind="text: members" />
</td>
<td>
<span data-bind="text: goal" />
</td>
</tr>
</tbody>
</table>
And "task" is the observable array containing all goal details.
This is my goal constuctor in javascript
function Goal(data) {
var self = this;
self.goalID = data.GoalID;
self.goal = data.Goal;
self.isPublic = ko.observable(data.IsPublic);
self.members = ko.observable(data.MemberName);
}
Maybe it is not the best solution but it will work. Add computed that returns all users:
self.users = ko.computed(function(){
var list = ko.utils.arrayMap(self.tasks(), function(item){
return item.members
});
return ko.utils.arrayGetDistinctValues(list);
});
And using foreach binding create a table for each user:
<div data-bind="foreach: users">
<table>
<thead>
<tr>
<th>membername</th>
<th>
Goals
</th>
</tr>
</thead>
<tbody data-bind="foreach: $parent.tasks">
<tr data-bind="if: members == $parent">
<td>
<span data-bind="text: members" />
</td>
<td>
<span data-bind="text: goals" />
</td>
</tr>
</tbody>
</table>
</div>
Here is working fiddle: http://jsfiddle.net/TQXja/4/
Related
I'm using a template in my project. Before loging the page is configured to redirect in to a /threads all the methods that use is calling but is not showing in the page.
I need to refresh at least 3 times to show it correctly.
And the console error
On my component.ts:
ngOnInit(): void {
this.filters = {
id: '',
title: '',
description: '',
category: '',
initialDate: '',
finalDate: '',
status: 'Abierto',
user: ''
}
this.term = '';
// Get threads by filter
this.getThreads(this.filters);
}
// Get
private getThreads(filters: any) {
this.threadService.getByFilters(filters).subscribe(threads => {
this.threads = this.filteredThreads = threads;
this.threads.forEach(thread => thread.isSelected = false);
});
}
on my component.html
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th scope="col" *ngFor="let header of headers" (click)="setSortingOptions(header)">
<span class="sortable" *ngIf="header.toLowerCase() === this.sortHeader">
<i
[ngClass]="this.isFlowReverse ? 'ti-arrow-circle-down' : 'ti-arrow-circle-up'"></i>
</span>
<span class="sortable">
{{header}}
</span>
</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let thread of filteredThreads | orderBy: this.sortHeader : this.isFlowReverse: true | slice: (page-1) * pageSize : page * pageSize"
(click)="preview(thread)" [ngClass]="thread.isSelected ? 'selected': '' ">
<td>
<ngb-highlight [result]="thread.status" [term]="term"></ngb-highlight>
</td>
<td>
<ngb-highlight [result]="thread.title" [term]="term"></ngb-highlight>
</td>
<td>
<ngb-highlight [result]="thread.category" [term]="term"></ngb-highlight>
</td>
<td>
<ngb-highlight [result]="thread.date" [term]="term"></ngb-highlight>
</td>
<td>
<ngb-highlight [result]="thread.user" [term]="term"></ngb-highlight>
</td>
</tr>
</tbody>
</table>
</div>
I have the following firebase JSON object:
"items" : {
"111111111111" : {
"ins" : { <<<<<<<<< I want to display the length of this
"1523878813443" : true,
"1523878891312" : true,
"1523878911379" : true,
"1523879091312" : true
},
"name" : "10",
"outs" : { <<<<<<<<< and this one too
"1523878813443" : true,
"1523878891312" : true,
"1523878911379" : true
},
"ownerID" : "QpMHsVHHRrMvk92rbSQvcYEv4en1"
}
}
Here is the template part on where I am trying to get these values to update:
<tbody>
<tr v-for="item in items" >
<td>
{{item.childone - door.childtwo}}
</td>
<td>
{{item.childone}}
</td>
<td>
{{item.childtwo}}
</td>
</tr>
</tbody>
I got it to display the JSON object like this:
The "In's" column should display 4, the "Out's" should display 3 and the "Balance" column should display 1.
This is the firebase setup and data code on my component:
import firebase { doorsRef } from '../firebase-config';
export default {
firebase() {
return {
items: itemsRef,
}
},
...etc
}
Any kind soul out there who would be willing to help?
Knowing that a JSON object has keys (and values) I have used this on my template instead:
{{Object.keys(JSON.Object).length}}
So in my case this did the trick:
<tbody>
<tr v-for="item in items" >
<td>
{{Object.keys(item.childone).length - Object.keys(item.childtwo).length}}
</td>
<td>
{{Object.keys(item.childone).length}}
</td>
<td>
{{Object.keys(item.childtwo).length}}
</td>
</tr>
</tbody>
Alternative you you can also count the number of values: {{Object.values(JSON.Object).length}}
I am working on a dashboard and am trying to display a certain piece of data. In my scope I have a project which consists of a couple fields and an array of users. The users consist of the following fields:
user_id
user_name
user_email
role
I want to display the user_name of the role which matches a certain value. Is there a way to do this within my html file or is it recommended to do this in my controller?
My table code looks as follows:
<tbody md-body>
<tr md-row md-select="project" md-select-id="name" md-auto-select
ng-repeat="project in projects.data | orderBy : myOrder">
<td md-cell ng-click="getProject()">{{project.title}}</td>
<td md-cell><i class="material-icons" style="color: green">radio_button_checked</i></td>
<td md-cell>{{project.phase}}</td>
<td md-cell>{{project.budget_indication}}</td>
<td md-cell>{{project.users}}</td> <!-- Display user with specific role -->
<td md-cell>{{project.start_date | date:dd/MM/yyyy}}</td>
</tr>
</tbody>
Thanks in advance!
If the project.data contains objects which has the role property (project.role) then you could use angular filter to filter against that property like this
Search by role: <input type="text" ng-model="roleSearch">
<tbody md-body>
<tr md-row md-select="project" md-select-id="name" md-auto-select
ng-repeat="project in projects.data | filter:{ role: roleSearch }| orderBy : myOrder">
<td md-cell ng-click="getProject()">{{project.title}}</td>
<td md-cell><i class="material-icons" style="color: green">radio_button_checked</i></td>
<td md-cell>{{project.phase}}</td>
<td md-cell>{{project.budget_indication}}</td>
<td md-cell>{{project.users}}</td> <!-- Display user with specific role -->
<td md-cell>{{project.start_date | date:dd/MM/yyyy}}</td>
</tr>
</tbody>
Read more about filters here.
More examples in this SO question
Update
To set the default filter value, you could set the roleSearch in the controller:
$scope.roleSearch = 'Admin';
To filter by the users.role add this filter
filter:{ users.role: roleSearch }
<tbody md-body>
<tr md-row md-select="project" md-select-id="name" md-auto-select
ng-repeat="project in projects.data | orderBy : myOrder" ng-if="project.users === 'role'">
<td md-cell ng-click="getProject()">{{project.title}}</td>
<td md-cell><i class="material-icons" style="color: green">radio_button_checked</i></td>
<td md-cell>{{project.phase}}</td>
<td md-cell>{{project.budget_indication}}</td>
<td md-cell> {{project.users}} </td> <!-- Display user with specific role -->
<td md-cell>{{project.start_date | date:dd/MM/yyyy}}</td>
</tr>
</tbody>
Assuming role is specific role
You can use filter:role
<div>{{x.users | filter: role= "User"}}</div>
Here is an example I have taken to get your solution,
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body ng-app="myApp" ng-controller="myCtrl">
Role: {{ role ? role : 'All Data'}} <br><br>
<div ng-repeat="x in projects">
<div>{{x.users | filter: role= role}}</div>
</div>
<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.projects = [{
users: [
{
"Name" : "Alfreds Futterkiste",
"Country" : "Germany",
"Role": "User"
},
{
"Name" : "Berglunds snabbköp",
"Country" : "Sweden",
"Role": "Admin"
},
{
"Name" : "Centro comercial Moctezuma",
"Country" : "Mexico",
"Role": "User"
},
{
"Name" : "Ernst Handel",
"Country" : "Austria",
"Role": "Admin"
}
]
}]
$scope.role = "User"
});
</script>
</body>
</html>
Just change the role to "Admin" and check the filter
Here is the DEMO link which contain ADMIN role
I am attempting to do a nested loop to access the tutor information and the logs the students made for the tutor in my HTML
HTML:
<table ng-if="vm.log.length !== 0 && vm.checked === 1" class="table table-striped">
<thead>
<tr>
<th>Tutor First Name</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="log in vm.logs ">
<td>{{logs.tutorFirst}}</td>
<td>
<table>
<tr ng-repeat="Student in StudentLogs track by $index">
<td> {{Student.log}}</td>
</tr>
</table>
</td>
</tr>
</tbody>
</table>
JSON:
[
"Tutorfirst" :"tutor"
],
[
"Tutorfirst" :"tutor"
"Student" :{
"log" : "INFO"
}
],
The JSON might not be correct but my question is,
Will the ng-repeat initiate if the first object does not have the field i'm trying to loop?
I am trying to use ng-click inside table but its not working, however outside the table its working fine.
Below is HTML
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Section</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="product in products">
<td>{{product.ID}}</td>
<td>{{product.Name}}</td>
<td>{{product.Section}}</td>
<td><input type="button" name="name" value="Delete" class="btn btn-danger" ng-click="deleteProduct({{product.ID}});" /></td>
</tr>
</tbody>
</table>
On click of Delete button deleteProduct method doesn't call.
Problems :
You are traversing through a collection/object named rules and getting single item as rule. So you should access each single items properties with rule.yourProperty. How did product come here?
You don't need any double curly braces to for ng-click function parameters. Simply pass the property of the object. ng-click directive works this way.
HTML :
<div ng-controller="mainCtrl">
<table class="table table-striped">{{msg}}
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Section</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="rule in rules">
<td>{{rule.ID}}</td>
<td>{{rule.Name}}</td>
<td>{{rule.Section}}</td>
<td><input type="button" name="name" value="Delete" class="btn btn-danger" ng-click="deleteProduct(rule.ID)" /></td>
</tr>
</tbody>
</table>
</div>
angular :
angular.module('myapp', []).controller('mainCtrl', ['$scope', function($scope){
var data = [
{
ID : "1",
Name : "A1",
Section : "A1"
},
{
ID : "2",
Name : "A2",
Section : "A2"
},
{
ID : "3",
Name : "A3",
Section : "A3"
},
{
ID : "4",
Name : "A4",
Section : "A4"
}
];
$scope.rules = data;
$scope.deleteProduct = function(id){
alert(id);
// delete your item here
};
}]);
jsFiddle
Try this:-
ng-click="deleteProduct(product.ID)"