Tried to insert dropdown inside dynamic table but i do not know how to do it.I want to insert dropdown with some values on last column.Please help anyone to find the solution.
head = [ "Name", "Age", "Gender","Action"];
details = [
{ "name": "star1", "age": "25", "gender": "male","action": "" },
{ "name": "star2", "age": "22", "gender": "female","action": "" },
{ "name": "star3", "age": "21", "gender": "male","action": "" },
{ "name": "star4", "age": "35", "gender": "female","action": "" },
];
constructor(){
this.tableHead = this.head;
this.userDetails = this.details;
}
Demo:https://stackblitz.com/edit/angular-bfs3gp?file=src/app/app.component.ts
I have made a solution for you. Can you please replace your details array to
details = [
{ name: "star1", age: "25", gender: "male", action: ["Edit", "Delete"] },
{ name: "star2", age: "22", gender: "female", action: ["Edit", "Delete"] },
{ name: "star3", age: "21", gender: "male", action: ["Edit", "Delete"] },
{ name: "star4", age: "35", gender: "female", action: ["Edit", "Delete"] }
];
Then Replace the dynamic-table.component.html to below code
<table>
<thead>
<tr class="table-head">
<th *ngFor="let tableHead of tableHeads">{{tableHead}}</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let tableData of tableDatas">
<td *ngFor="let colName of tableColNameGenerated"> {{colName != 'action' ? tableData[colName] : ''}}
<span *ngIf="colName == 'action'">
<select>
<option *ngFor="let opt of tableData.action">{{opt}}</option>
</select>
</span>
</td>
</tr>
</tbody>
</table>
Now I hope your concern is resolved.
Hope this helps!
Thank you.
Related
I have an array with appointment data like this which I want to make tables from:
[
{
day: '20/10/2020',
subject: 'meeting',
client: 'Rob'
location: 'Office
},
{
day: '21/10/2020',
subject: 'meeting',
client: 'Lisa'
location: 'Town'
},
{
day: '21/10/2020',
subject: 'meeting',
client: 'Kevin'
location: 'Office
},
{
day: '22/10/2020',
subject: 'meeting',
client: 'Kevin'
location: 'Home'
}
]
my html file:
<div *ngFor="let appointment of appointments" class="card-body">
<table class="table table-striped">
<thead>
<tr>
<th> Day </th>
<th>Where </th>
<th> Client</th>
<th>Subject</th>
</tr>
</thead>
<tfoot>
<tr>
<td> <small>{{appointment.day}}</small></td>
<td> <small>{{appointment.location}} </small> </td>
<td><small>{{appointment.client}}</small> </td>
<td><small>{{appointment.subject}} </small></td>
</tfoot>
</table>
</div>
This produces a table per appointment, but how can I make it such that the appointments on the same day appear below eachother, without theader inbetween. So like this: (visualisation)
any help is appreciated
There is a very simple way to solve your issue, let's do it. We will make use of the lodash library, so first of all you will need to import.
Installing via NPM:
npm i lodash
npm i --save-dev #types/lodash
And importing to our project:
import * as _ from 'lodash';
So then the magic happens, with our just imported friend and its groupBy() method:
let result = _.groupBy(this.appointments, (appointment) => {
return appointments.day;
});
console.log(result);
The result from the console will be:
{
"20/10/2020": [
{
"day": "20/10/2020",
"subject": "meeting",
"client": "Rob",
"location": "Office"
}
],
"21/10/2020": [
{
"day": "21/10/2020",
"subject": "meeting",
"client": "Lisa",
"location": "Town"
},
{
"day": "21/10/2020",
"subject": "meeting",
"client": "Kevin",
"location": "Office"
}
],
"22/10/2020": [
{
"day": "22/10/2020",
"subject": "meeting",
"client": "Kevin",
"location": "Home"
}
]
}
As per my client requirement we are working on some custom table and I got stuck at nested rows.
when I click that plus symbol the rows of that particular row should be binded to the next row...
My data is in this format:
tabledata = [
{
reference: "191782-01", name: "American Electric LLC", assigned: "Rusty", Ship: "PKUP", Open: "15", total: "30", percentage: "50", staged: "1/4", datetime: "07/25/18 7:51 AM",
child: [{
reference: "191782-01", name: "American Electric LLC", assigned: "Rusty", Ship: "PKUP", Open: "15", total: "30", percentage: "50", staged: "1/4", datetime: "07/25/18 7:51 AM"
},
{
reference: "191782-01", name: "American Electric LLC", assigned: "Rusty", Ship: "PKUP", Open: "15", total: "30", percentage: "50", staged: "1/4", datetime: "07/25/18 7:51 AM"
}]
},
{ reference: "191780-02", name: "Good Business Company", assigned: "", Ship: "WISE", Open: "8", total: "8", percentage: "0", staged: "1/2", datetime: "07/25/18 8:12 AM", child: [] },
{ reference: "191782-02", name: "Cecchin Plumbing", assigned: "", Ship: "PKUP", Open: "23", total: "36", percentage: "36", staged: "1/3", datetime: "07/25/18 8:01 AM", child: [] },
{ reference: "191780-01", name: "Company Name 1", assigned: "MULTI", Ship: "PKUP", Open: "1", total: "1", percentage: "0", staged: "2/4", datetime: "07/25/18 8:05 AM", child: [] },
{ reference: "191702-02", name: "Leep's Supply", assigned: "MULTI", Ship: "WISE", Open: "3", total: "6", percentage: "50", staged: "2/4", datetime: "07/25/18 8:46 AM", child: [] }
]
I need child object to be visible when I click plus icon
My Angular Code:
<tbody>
<tr *ngFor="let data of tabledata">
<td class="text-left">{{data.reference}}<span *ngIf="data.reference.includes('191782')"
class="badge badge-warning win-badge win-badge-warning win-badge-large ml-3"
style="border-radius: 1.4rem">Urgent</span></td>
<td>{{data.name}}</td>
<td><span [ngClass]="{'badge badge-secondary win-badge win-badge-secondary': data.assigned=='MULTI', 'bgtrans': data.assigned!='MULTI'}">{{data.assigned}}</span></td>
<td>{{data.Ship}}</td>
<td><span [ngClass]="{'alert alert-error mobile-error alert-dismissible': data.Open>10, 'bgtrans': data.Open<10}">{{data.Open}}</span></td>
<td>{{data.total}}</td>
<td>{{data.percentage}}</td>
<td>{{data.staged}}</td>
<td>{{data.datetime}}</td>
<td><a href="javascript:void(0)"
*ngIf="data.child.length>0"><i class="fa fa-plus-square-o"></i></a> </td>
</tr>
</tbody>
You can map the array first, in order to transform it into a flat array, and then bind to the resulting array:
// pass a function to map
let arrays = tabledata.map(x => new Array(x).concat(x.child));
let flatTableData = [].concat.apply([], arrays);
To identify the child rows, just look for the ones that haven't the child property
Here is my json file.
{
"countries":[
{
"country": "India",
"cities" : [{
"name": "Bangalore",
"rank": "40"
},
{ "name": "Mumbai",
"rank": "32"
},
{ "name": "Kolkata",
"rank": "54"
},
{ "name": "Chennai",
"rank": "42"
}]
},
{ "country": "China",
"cities":[{"name": "Guangzhou",
"rank": "111"
},
{ "name": "Fuzhou",
"rank": "21"
},
{ "name": "Beijing",
"rank": "90"
},
{ "name": "Baotou",
"rank": "23"
}]
}
]}
I want to show all the name and rank of all the cities in the html table,
but I am unable to do this.
Angular js code is:
app.controller('cityCtrl1', function($scope, $http){
$http.get("http://localhost:8080/Angular%20Examples/angularCountries/app/json/countriesToCities1.json").success(function(data){
$scope.cities = data.countries;
});
});
Html code is:
<tr ng-repeat="city in cities | filter: selectName1">
<div ng-repeat="details in city.cities">
<td> {{details.name}} </td>
<td> {{details.rank}}</td>
</div>
</tr>
Might be I could change the code in controller file for getting the data, but not sure that will work or not.
First thing div inside a tr is not allowed(not working also) - Check this <div> into a <tr>: is it correct?
So I have change format -
<ul class="table" ng-repeat="country in cities.countries">
<li class="city-row" ng-repeat="city in country.cities">
<span class="city-name">{{city.name}}</span>
<span class="city-count">{{city.rank}}</span>
</li>
</ul>
I have created working example - http://plnkr.co/edit/1GTqNPcfigd9XRaAy0vg
Apply your own CSS to display it in table format.
what you are trying to do is,to refer city.states but where as states is not an object of json..change it to city.cities
<tr ng-repeat="city in cities | filter: selectName1">
<div ng-repeat="details in city.cities">
<td> {{details.name}} </td>
<td> {{details.rank}}</td>
</div>
</tr>
I have a table as follows:
<tr ng-repeat="user in users">
<td>{{user.fields.first_name}}, {{ user.fields.last_name }}</td>
<td>
<input type="checkbox">
</td>
</tr>
and the json is as follows:
{
"users":
[
{ "fields": {"first_name": "sam", "last_name": "smith"}, "model": "auth.user", "pk": 3},
{ "fields": {"first_name": "tom", "last_name": "moody"}, "model": "auth.user", "pk": 4}
]
}
I want to have a functionality to add the pk's of the selected checkboxes to a scope variable. Since i don't have a boolean field for the selected in my json, how will i approach this?
You can try the code in my fiddle, the selected PKs are stored in an array call 'selected'. The pk must be unique for each user.
var app = angular.module('app', []);
var ctrl = app.controller('MyCtrl', function($scope) {
$scope.users = [{
"fields": {
"first_name": "wasif",
"last_name": "abbas"
},
"model": "auth.user",
"pk": 3
}, {
"fields": {
"first_name": "asad",
"last_name": "zaman"
},
"model": "auth.user",
"pk": 4
}, {
"fields": {
"first_name": "hes",
"last_name": "man"
},
"model": "auth.user",
"pk": 5
}];
$scope.selected = [];
$scope.toggleSelection = function toggleSelection(pk) {
var idx = $scope.selected.indexOf(pk);
// is currently selected
if (idx > -1) {
$scope.selected.splice(idx, 1);
}
// is newly selected
else {
$scope.selected.push(pk);
}
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
<div ng-controller='MyCtrl'>
<table>
<tr ng-repeat="user in users">
<td>{{user.fields.first_name}}, {{ user.fields.last_name }}</td>
<td>
<input type="checkbox" ng-checked="selected.indexOf(user.pk) > -1" ng-click="toggleSelection(user.pk)" />
</td>
</tr>
</table>
Selected: {{selected}}
</div>
</div>
Or you can try with ng-change, which is a bit more elegant imo:
var app = angular.module('app', []);
app.controller('AController', function($scope) {
$scope.users = [
{ "fields": {"first_name": "wasif", "last_name": "abbas"}, "model": "auth.user", "pk": 3},
{ "fields": {"first_name": "asad", "last_name": "zaman"}, "model": "auth.user", "pk": 4}
];
$scope.selectedUsers = {};
$scope.addUser = function(pk, userselected) {
if(userselected) {
$scope.selectedUsers[pk] = true;
} else {
delete $scope.selectedUsers[pk];
}
}
$scope.getSelectedUsersArray = function() {
return Object.keys($scope.selectedUsers);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
<div ng-controller="AController">
<table>
<tr ng-repeat="user in users">
<td>{{user.fields.first_name}}, {{ user.fields.last_name }}</td>
<td>
<input type="checkbox" ng-model="userselected" ng-change="addUser(user.pk, userselected)"/>
</td>
</tr>
</table>
<div>Selected: {{getSelectedUsersArray()}}</div>
</div>
</div>
I'm trying to set up a nested ng-repeat but can't quite figure out what's wrong. Nothing is printing in the nested ng-repeat
HTML:
<div ng-repeat="person in persons">
<p> {{ person.name }} </p>
<div ng-repeat="friend in person.mutual_friends">
{{ friend.name }}
</div>
</div>
JSON:
$scope.persons = [
{
name: 'First Person',
mutual_friends: [
{
name: 'Mutual Friend 1'
},
{
name: 'Mutual Friend 2'
},
]
},
{
name: 'Second Person',
mutual_friends: [
{
name: 'Mutual Friend 1'
},
{
name: 'Mutual Friend 2'
},
]
},
]
JSON was the issue.
Following JSFiddle code works :
https://jsfiddle.net/sushruthreddygade/hcpb229g/2/
JSON Structure:
[
{
"name": "First Person",
"mutual_friends": [
{
"name": "Mutual Friend 1"
},
{
"name": "Mutual Friend 2"
}
]
},
{
"name": "Second Person",
"mutual_friends": [
{
"name": "Mutual Friend 3"
},
{
"name": "Mutual Friend 4"
}
]
}
]