i have a table in angular, i want to write a click event to change the vlaue of the cell that was clicked - angularjs-directive

here is my Template that makes the table
<div class="container col-md-6 col-lg-6 container-fluid col-md-offset-3 col-lg-offset-3">
<h1 class="page-header">
multiplication-table <small>angular demo</small>
</h1>
<table class="table table-bordered ">
<tr *ngFor="let i of matnumbers" class="text-center ">
<td *ngFor="let j of matnumbers" class="text-center" (click)="showmul()">
<span *ngIf="showItem"> {{i*j}}</span>
<span *ngIf="!showItem"> {{i}}x{{j}}</span>
</td>
</tr>
</table>
</div>
this is the component:
export class MultiplicationTable {
matnumbers: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
showmul() {
var that = this;
this.showItem = false;
setTimeout(function () {
that.showItem = true;
}, 550);
that.showItem = false;
}
}
unfortunatly this code change the spans of all the cells in my table,
and i want to change only the specific one that was clicked.
thanks.

export class MultiplicationTable {
shownNumbers: number[] = [];
matnumbers: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
toggleNum(num: number) {
var index = this.shownNumbers.indexOf(num);
if(index != -1){
this.shownNumbers.splice(index, 1);
} else {
this.shownNumbers.push(num);
}
}
}
In your html
<div class="container col-md-6 col-lg-6 container-fluid col-md-offset-3 col-lg-offset-3">
<h1 class="page-header">
multiplication-table <small>angular demo</small>
</h1>
<table class="table table-bordered ">
<tr *ngFor="let i of matnumbers" class="text-center ">
<td *ngFor="let j of matnumbers" class=text-center"(click)="showmul(j)">
<span *ngIf="shownNumbers.indexOf(j) != -1"> {{i*j}}</span>
<span *ngIf="shownNumbers.indexOf(j) == -1"> {{i}}x{{j}}</span>
</td>
</tr>
</table>
This might work

this is the html
multiplication-table angular demo
<table class="table table-bordered ">
<tr *ngFor="let i of matnumbers" class="text-center ">
<td *ngFor="let j of matnumbers" class="text-center" (click)="toggleNum(i,j)">
<span *ngIf="shownNumbers.indexOf(i*10+j) != -1"> {{i}}x{{j}}</span>
<span *ngIf="shownNumbers.indexOf(i*10+j) == -1"> {{i*j}}</span>
</td>
</tr>
</table>
this is the component
export class MultiplicationTable {
shownNumbers: number[] = [];
matnumbers: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
toggleNum(i: number, j: number) {
var num = j +i* 10;
var index = this.shownNumbers.indexOf(num);
if (index != -1) {
this.shownNumbers.splice(index, 1);
} else {
this.shownNumbers.push(num);
}
}
}

Related

Angular Materials-Data not displaying which is from database, error not defined

I am using mat-table for my project. Here is my html code:
<table mat-table class=" first_container mat-elevation-z8 demo-table">
<!-- Item Column -->
<ng-container matColumnDef="Item">
<th mat-header-cell *matHeaderCellDef>Item </th>
<td mat-cell *matCellDef="let Menu ">{{Menu.item}}</td>
</ng-container>
<!-- Price Column -->
<ng-container matColumnDef="Price">
<th mat-header-cell *matHeaderCellDef>Price</th>
<td mat-cell *matCellDef="let Menu ">{{Menu.price}}</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns sticky: true" ></tr>
<tr mat-row
(click)="clickedRows.add(row)"
[class.demo-row-is-clicked]="clickedRows.has(row)"
*matRowDef="let row; columns: displayedColumns;"></tr>
</table>
<!-- ************************BOTTOM SPLIT LEFT********************* -->
<div class="split left">
<div class="centered">
<p>Token Number: 01</p>
</div>
<ul>
<li *ngFor="let Menu of clickedRows">
Clicked on {{Menu.item}} which is {{Menu.price}}/-
</li>
</ul>
<div class="btn">
<button mat-stroked-button color="primary">Cancel</button>
<button mat-flat-button color="primary">Print</button>
</div>
</div>
<!-- ****************************BOTTOM Split RIGHT Div******************** -->
<div class="split right">
<div class="centered">
<p>Token Number: 02</p>
</div>
<div class="btn">
<button mat-stroked-button color="primary">Cancel</button>
<button mat-flat-button color="primary">Print</button>
</div>
</div>
and here is my ts file code
displayedColumns: string[] = ['Item', 'Price'];
Menu1: menu1[] = [
// {
// item: 'idly',
// price: 20.
// },
// {
// item: 'vada',
// price: 90.
// },
// {
// item: 'vada',
// price: 90.
// },
];
constructor(private morningService: MorningService) { }
datasource=this.Menu1
ngOnInit(): void {
this.morningService.getAllMenu()
.subscribe({
next: (menu) => {
this.Menu1=menu;
},
error: (Response) => {
console.log(Response);
}
});
// this.Menu1.push()
}
// datasource = this.Menu1
clickedRows = new Set<menu1>
This is my service file
export class MorningService {
url: string = environment.baseApiUrl
constructor(private http: HttpClient) { }
getAllMenu(): Observable<menu1[]> {
return this.http.get<menu1[]>(this.url +'/api/Menu');
}
}
The data is coming from the sql which I have been integrated with.
I have tried hard-coding the data which is working fine... but when I want it statically it is going blank.

How to make icon change only for particular table instead of changing as whole

Hi I have multiple tables looped in an array, but here if i click on icon of first table, the icon in that header name of all the tables are getting changed instead of changing to particular table.
DEMO:
DEMO
TS:
public sortContactsList(param,key) {
this.sorting = (this.sorting === 'asc') ? 'desc' : 'asc';
this.contactListDetails.forEach(item => {
if (item.param === param) {
item.icon = item.icon === 'fa fa-sort-down active' ? 'fa fa-sort-up active' : 'fa fa-sort-down active';
this.sorting = (item.icon === 'fa fa-sort-down active') ? 'desc' : 'asc';
} else {
item.icon = 'fa fa-sort'
}
});
}
HTML:
<div *ngIf="contactsDetails[0].result[key]?.length > 0">
<h6 style="font-weight: 600;">{{key | titlecase}}</h6>
<table class="table table-hover accordion-table" id="accordionContact">
<thead>
<tr>
<th scope="col" *ngFor="let field of contactListDetails" (click)="sortContactsList(field.param,key)">
{{field.displayName}}
<i class="{{field.icon}}" aria-hidden="true"></i>
</th>
<th scope="col" class="width125"></th>
</tr>
</thead>
<tbody>
<tr *ngFor="let fields of contactsList[key]">
<td *ngFor="let field of contactListDetails">{{fields[field.param]}}</td>
<td class="width125 {{paginationDisable?'link-disabled':''}}"><button class="btn btn-outline-primary btn-table" title="Send Mail"
(click)="userDisplay(contactsDetails[0].result[key][0])" [disabled]="isReadOnly && mode ==1">Email</button>
</td>
</tr>
</tbody>
</table>
</div>

How to create dynamic grid component in angular 8 and bootstrap whit row, col and list string?

I have a problem understanding how to create a grid component, which has in input of a number of columns, rows and a list of strings for the inside element of grid.
my thumbnails.component.ts
import { Component, Input } from '#angular/core';
#Component({
selector: 'thumbnails',
templateUrl: './thumbnails.component.html',
styleUrls: ['./thumbnails.component.scss']
})
export class ThumbnailsComponent{
public UrlList = ["one","two","three","four","five","six","seven","eight","nine","ten"];
private col: Number = 4;
private row: Number = 4;
}
my thumbnails.component.html
<div id="grid">
<ng-container *ngFor="let x of UrlList; let i = index">
<div class="row">
<div class="col">
{{x}}
</div>
</div>
</ng-container>
</div>
I don't know how to implement it. Can someone help me and explain it to me?
Another approach is the CSS way using display gird with gridTemplateColumns properties.
thumbnails.component.ts
import { Component, Input } from '#angular/core';
#Component({
selector: 'thumbnails',
templateUrl: './thumbnails.component.html',
styleUrls: ['./thumbnails.component.scss']
})
export class ThumbnailsComponent{
public UrlList =["one","two","three","four","five","six","seven","eight","nine","ten"];
grid(v) {
document.getElementById(
'grid'
).style.gridTemplateColumns = `repeat( ${v.value}, 175px)`;//Important step
}
}
thumbnails.component.css
.grid-container {
display: grid;
grid-gap: 15px;
}
thumbnails.component.html
<label>Enter Col :</label><input type="text" #col />
<button (click)="grid(col)">Set Grid</button>
<div id="grid" class="grid-container row">
<ng-container *ngFor="let x of UrlList; let i = index">
<div class="col">
{{ x }}
</div>
</ng-container>
</div>
Try this code:
your.compomnent.html
<div>
<input type="number" [(ngModel)]="rows" />
<input type="number" [(ngModel)]="colums" />
<button (click)="setDimension()" type="button">Change</button>
<table class="table">
<thead>
<tr>
<th scope="col" *ngFor="let col of colArray; index as i">{{i+1}}</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let col of rowArray; index as i">
<td *ngFor="let col of colArray; index as j">{{j+1}}</td>
</tr>
</tbody>
</table>
</div>
your.compomnent.ts
colArray = [];
rowArray = [];
colums = 4;
rows = 10;
setDimension(){
this.colArray = [];
this.rowArray = [];
for(let i = 0; i<this.colums; i++){
this.colArray.push(i);
}
for(let i = 0; i<this.rows; i++){
this.rowArray.push(i);
}
}
The i which is pushed in rowArray could be your string URLs.
Note: Table is the good way to use grids. You can even use BS rows and cols

Why angular-datatable not fill col in bootstrap 4?

I tried to set a responsive table with angular-datatables (for angular 7) and bootstrap 4 but this is the current aspect of the table:
Current HTML code:
<div class="row">
<div class="col-12">
<div class="table-responsive">
<table datatable [dtOptions]="dtOptions" [dtTrigger]="dtTrigger" class="table table-hover table-bordered">
<thead class="thead-colored thead-primary">
<tr class="text-center">
<th>ID</th>
<th>NICKNAME</th>
<th>EMAIL</th>
<th>REGISTRO</th>
<th>ESTATUS</th>
<th>ALTA</th>
<th>ACCIONES</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let usuario of usersArray" class="text-center">
<td>{{usuario.ID}}</td>
<td>{{usuario.NICKNAME}}</td>
<td>{{usuario.EMAIL}}</td>
<td>{{usuario.REGISTRO | date: 'dd-MMMM-yyyy' | uppercase}}</td>
<td>
<span class="bg-success pd-y-3 pd-x-10 text-white tx-11 tx-roboto">
{{usuario.ESTATUS | toStatusReadable}}
</span>
</td>
<td>{{usuario.PROVEEDORACCESO}}</td>
<td>
<div type="button" class="dropdown">
<a [routerLink]="" class="tx-gray-800 d-inline-block" data-toggle="dropdown">
<div class="ht-45 pd-x-20 bd d-flex align-items-center justify-content-center">
<span class="mg-r-10 tx-13 tx-medium">Opciones</span>
<img src="https://via.placeholder.com/500" class="wd-25 rounded-circle" alt="">
<i class="fas fa-angle-down"></i>
</div>
</a>
<div class="dropdown-menu pd-10 wd-200">
<nav class="nav nav-style-2 flex-column">
<a [routerLink]="" class="nav-link"><i class="icon fas fa-edit"></i> Editar datos</a>
</nav>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
TS File code for the table:
export class AdministradoresComponent implements OnInit, OnDestroy {
/* dtOptions: DataTables.Settings = {}; */
dtOptions: any = {};
dtTrigger: Subject<any> = new Subject<any>();
usersArray: Array<Users> = [];
constructor(
private devInfo: DevelopmentService,
private dtService: DatatableService,
private administradoresService: AdministradoresService
) {
this.ObtenerUsuariosSmartlive();
}
ngOnInit() {
document.getElementById('mainSystemPanel').removeAttribute('class');
document.getElementById('mainSystemPanel').className = 'br-mainpanel';
this.dtOptions = this.dtService.getDataTableConfig();
}
ngOnDestroy() {
this.dtTrigger.unsubscribe();
}
async ObtenerUsuariosSmartlive() {
const usuarios = <any []> await this.administradoresService.getSmartliveUsersList();
for ( let i = 0; i < usuarios.length; i++) {
this.usersArray.push(JSON.parse(usuarios[i]));
}
this.dtTrigger.next();
}
}
The service to provide config of the table:
export class DatatableService {
constructor() {
}
getDataTableConfig() {
return {
language: {
url: 'assets/content/smartlive/spanish.json'
},
responsive: true
};
}
}
angular.json snippet:
"styles": [
"node_modules/#fortawesome/fontawesome-free/css/all.min.css",
"node_modules/animate.css/animate.min.css",
"src/assets/content/template/lib/ionicons/css/ionicons.min.css",
"src/assets/content/template/lib/rickshaw/rickshaw.min.css",
"src/assets/content/template/lib/select2/css/select2.min.css",
"src/assets/content/template/css/bracket.css",
"src/assets/content/template/css/bracket.oreo.css",
"node_modules/datatables.net-dt/css/jquery.dataTables.min.css",
"node_modules/datatables.net-bs/css/dataTables.bootstrap.min.css",
"node_modules/datatables.net-responsive-dt/css/responsive.dataTables.min.css",
"src/styles.css"
],
"scripts": [
"node_modules/jquery/dist/jquery.js",
"node_modules/datatables.net/js/jquery.dataTables.js",
"node_modules/datatables.net-bs/js/dataTables.bootstrap.min.js",
"node_modules/datatables.net-responsive/js/dataTables.responsive.min.js"
]
I already installed all the packages files for the datatable but i can't set a correct 100% of width and always the pagination and search input have a bad location over the table.
A bit late but it might help someone else ;-)
Try:
npm install datatables.net-bs4
Then call:
"styles": [
...
"./node_modules/datatables.net-bs4/css/dataTables.bootstrap4.min.css",
...
],
"scripts": [
...
"./node_modules/datatables.net-bs4/js/dataTables.bootstrap4.min.js",
...
],

Problem in updating the HTML table data as the browser crashes when I am getting the data every second using Socket

I am showing real time data in a HTML table by getting the data from back-end socket developed using Node.js, which sends the data of 200 row (objects) every second which haves some object's value changed.
When I am trying to display or render those data in HTML table, the browser crashes and displays nothing.
I am using Angular and D3 table libraries to do. The code is attached below. Please help me with some ways to fix this or even some better idea to resolved this issue.
My Component.ts File
import { Component, OnInit } from '#angular/core';
import { HttpService } from '../../Service/http.service';
import { SymbolClass } from 'src/app/Model/Symbol';
import { SocketService } from 'src/app/Service/socket.service';
import { ResponseClass } from 'src/app/Model/Response';
import * as d3 from 'd3';
import { Subscription } from 'rxjs';
import { DataSetClass } from 'src/app/Model/DataSet';
import { ChangeDetectorRef } from '#angular/core';
import { BidData } from 'src/app/Model/DataBid';
#Component({
selector: 'dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.css']
})
export class DashboardComponent implements OnInit {
private symbolList: SymbolClass[];
private symbolSelected = "XBTUSD";
private url = 'http://188.166.85.186:7000';
private socket;
private dataSetlist: ResponseClass;
private sub: Subscription;
private askDept: DataSetClass[];
private bidDept: BidData[];
constructor(private httpService: HttpService, private socketService: SocketService, private changeDetectorRef: ChangeDetectorRef) {
}
ngOnInit() {
var leftTable = d3.select('#leftTable')
var rightTable = d3.select('#rightTable')
leftTable.append('tbody')
rightTable.append('tbody')
this.httpService.getSymbol()
.subscribe(data => {
this.symbolList = data
this.socketService.emitAfterSymbolChange(this.symbolSelected);
this.sub = this.socketService.getDataResponse()
.subscribe(data => {
this.dataSetlist = data
this.askDept = data.askDepth
this.setupData(this.askDept,'#leftTable')
this.bidDept = data.bidDepth
this.setupData(this.bidDept,'#rightTable')
});
});
}
private dev = function(){
this.setupData(this.randomizeData(),'#leftTable')
this.setupData(this.randomizeData(),'#rightTable')
}
private setupData = function (data: any,tableType: String) {
var rows = d3.select(tableType).select('tbody')
.selectAll('tr')
.data(data, function (d) {
return d.title;
})
rows.exit().remove()
var entertd = rows.enter()
.append('tr')
.selectAll('td')
.data(function (d) {
return d3.map(d).values();
})
.enter()
.append('td')
.append('div')
.append('span')
.text(function(d){
return d;
})
var td = rows.selectAll('td')
.data(function (d) {
return d3.map(d).entries();
})
.attr('class', function (d) { return d.key })
td.select('span')
.text(function (d) {
if (d.key == 'conversion_rate') {
return Math.round(100 * d.value) + '%'
}
return d.value
})
}
}
My component.html file
<div class="container" style="margin-top: 100px; margin-bottom: 100px;">
<select class="form-control">
<option *ngFor="let sym of symbolList">{{sym}}</option>
</select>
<div class="row" style="margin:20px 0px;">
<button (click)="dev()" type="button">Update Data</button>
</div>
<div class="row">
<div class="col-lg-12 col-md-12 mt-2">
<div class="chartDoug bg-dark">
<span class="headTip text-nowrap" style="font-size:20px">Live Order Book!</span>
<div id="myChart"></div>
</div>
</div>
</div>
<div class="row">
<div class="col col-xs-12 col-sm-6 col-md-3 col-lg-3">
<h4 style="text-align:center; color:red;float:left;margin-left:10px;">Volumn:</h4>
<h4 style="text-align:right; color:red;float:left;margin-left:5px;" class="volumn"></h4>
</div>
<div class="col col-xs-12 col-sm-6 col-md-3 col-lg-3">
<h4 style="text-align:center; color:green;float:left;margin-left:10px;">Last Price:</h4>
<h4 style="text-align:right; color:green;float:left;margin-left:5px;" class="lastprice"></h4>
</div>
<div class="col col-xs-12 col-sm-6 col-md-3 col-lg-3">
<h4 style="text-align:center; color:red;float:left;margin-left:10px;">HIGH:</h4>
<h4 style="text-align:right; color:red;margin-left:5px;float:left;" class="high"></h4>
</div>
<div class="col col-xs-12 col-sm-6 col-md-3 col-lg-3">
<h4 style="text-align:center; color:green;float:left;margin-left:10px;">LOW:</h4>
<h4 style="text-align:right; color:green;margin-left:5px;float:left;" class="low"></h4>
</div>
</div>
<div class="row">
<div class="col col-xs-12 col-sm-12 col-md-6 col-lg-6">
<h2 style="text-align:center; color:green;">BUY</h2>
<h4 style="text-align:right; color:green;" class="buy_total_amount"></h4>
<table id="leftTable" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th style="text-align: center;">No</th>
<th style="text-align: center;">Price</th>
<th style="text-align: center;">Quantity</th>
<th style="text-align: center;">Total Amount</th>
</tr>
</thead>
<tbody style="text-align: right;" class="buy">
</tbody>
</table>
</div>
<div class="col col-xs-12 col-sm-12 col-md-6 col-lg-6">
<h2 style="text-align:center; color:red;">SELL</h2>
<h4 style="text-align:right; color:red;" class="sell_total_amount"></h4>
<table id="rightTable" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th style="text-align: center;">No</th>
<th style="text-align: center;">Price</th>
<th style="text-align: center;">Quantity</th>
<th style="text-align: center;">Total Amount</th>
</tr>
</thead>
<tbody style="text-align: right;" class="sell">
</tbody>
</table>
</div>
</div>
</div>