how to get an array of column configurations in Kendo grid angular - angularjs

suppose i've the following kendo grid
<kendo-grid-column field="full_name" title="Contact Name" [width]="220">
<ng-template kendoGridCellTemplate let-dataItem>
<div
class="customer-photo"
[ngStyle]="{ 'background-image': photoURL(dataItem) }"
></div>
<div class="customer-name">{{ dataItem.full_name }}</div>
</ng-template>
</kendo-grid-column>
<kendo-grid-column field="job_title" title="Job Title" [width]="220">
</kendo-grid-column>
<kendo-grid-column
field="country"
title="Country"
[width]="100"
[class]="{ 'text-center': true }"
[resizable]="false"
>
<ng-template kendoGridCellTemplate let-dataItem>
<img class="flag" [src]="flagURL(dataItem)" width="30" />
</ng-template>
</kendo-grid-column>
what i want to do is to get an array of grid columns with column title & field name
eg: [{field:"full_name",title:"Contact Name"},
{field:"job_title",title:"Job Title"}];
how to do this in Kendo Grid for angular?

You can access the grid using ViewChild and then access its columns field:
import { Component, ViewChild, AfterViewInit } from '#angular/core';
import { GridComponent } from "#progress/kendo-angular-grid";
#Component({
selector: 'my-app',
template: `
<kendo-grid #grid [data]="gridData">
<kendo-grid-column field="Id" title="ID">
</kendo-grid-column>
<kendo-grid-column field="CompanyName" title="Company Name">
</kendo-grid-column>
</kendo-grid>
`
})
export class AppComponent implements AfterViewInit {
#ViewChild("grid") grid: GridComponent;
public readonly gridData = [{
'Id': 'ALFKI',
'CompanyName': 'Alfreds Futterkiste',
'ContactName': 'Maria Anders',
'ContactTitle': 'Sales Representative',
'City': 'Berlin'
}, {
'Id': 'ANATR',
'CompanyName': 'Ana Trujillo Emparedados y helados',
'ContactName': 'Ana Trujillo',
'ContactTitle': 'Owner',
'City': 'México D.F.'
}, {
'Id': 'ANTON',
'CompanyName': 'Antonio Moreno Taquería',
'ContactName': 'Antonio Moreno',
'ContactTitle': 'Owner',
'City': 'México D.F.'
}];
ngAfterViewInit() {
const columns = this.grid.columns.map((column) => {
return {
field: column.field,
title: column.title
};
});
console.log(columns);
}
}
Note: this.grid.columns gives you an object of type QueryList<ColumnBase>. The ColumnBase class doesn't contain the field field in its definition, however, it does appear on the object. I guess that this.grid.columns actually returns an object of type QueryList<ColumnComponent>. ColumnComponent derives from ColumnBase and does contain the field field.

For me, to get the column.filed, the above code didn't work. But when I use fieldName: column["field"], it worked for me.

Related

Get value from each *ngFor ionic 4, ionic 5, ionic 6

I have the following code:
<ion-item *ngFor="let box of boxes">
This will show results from array:
On the .ts file i have the following:
isApproved : boolean;
public box: any;
This will generate from boxes array:
box1 -> [id, name, isApproved]
box2 -> [id, name, isApproved]
box3 ->[id, name, isApproved]
I need to get the isApproved value of each box, so when i activate the toggle, isApproved will change in database.
I know one method that doesn't fits my needs, like clicking and getting the id from route but i want to open a new page for that.
Just put and ngModel in the ion-toggle:
html:
<ion-item *ngFor="let box of boxes">
<ion-avatar slot="start"></ion-avatar>
<ion-label>...</ion-label>
<ion-toggle [(ngModel)]="box.isApproved" (ionChange)="approvedToggled($event)"></ion-toggle>
</ion-item>
ts:
approvedToggled(event, box) {
if(event.detail.value) {
// save to box to database
}
/* or:
if(item.isApproved) {
// save to database
}
*/
}
The solution is very simple.
The working code is:
On my HTML:
<div *ngFor="let box of boxes">
<ion-item-sliding id="anyId">
<ion-item>
<ion-avatar slot="start">
<img [offset]="100" [alt]="box.user?.name"
defaultImage="./assets/img/photo.png" [lazyLoad]="box.user?.photo?.url()" />
</ion-avatar>
<ion-label class="ion-text-wrap">
<ion-text color="dark">
<h3 class="bold no-margin">
{{ box.user?.name }}
</h3>
</ion-text>
</ion-label>
</ion-item>
<ion-item-options side="end">
<ion-item-option color="primary" (click)="onDelete(box)">
<ion-icon slot="icon-only" name="trash"></ion-icon>
</ion-item-option>
</ion-item-options>
</ion-item-sliding>
</div>
On my TS i have:
Importing service:
import { Box } from '../../services/box-service';
Before constructor:
public boxes: Box[] = [];
public box: Box;
constructor(private BoxService: Box) {
super(injector);
}
Loading boxes from service:
async loadDataFromService() {
try {
const boxes = await this.boxService.loadBoxes(this.params);
for (let box of boxes) {
this.boxes.push(box);
}
this.onRefreshComplete(boxes);
} catch {
}
}
... this will return an array with arrays. Each array has an object.
Now we just access each box from HTML (click)="onDelete(box)"
async onDelete(box: Box) {
await Swal.fire({
title: 'Are you sure?',
text: 'Blah, blah',
icon: 'warning',
iconColor: '#5038de',
showCancelButton: true,
confirmButtonColor: '#5038de',
cancelButtonColor: '#e0b500',
confirmButtonText: 'Yes',
cancelButtonText: 'No',
heightAuto: false,
showClass: {
popup: 'animated fade-in'
},
hideClass: {
popup: 'animated fade-out'
}
}).then(async (result) => {
if (result.value) {
await this.boxService.deleteBox(box)
this.goTo()
} else {
this.goTo()
}
});
}
}
Resuming, the solution for:
<ion-item *ngFor="let box of boxes">
<ion-avatar slot="start"></ion-avatar>
<ion-label>...</ion-label>
<ion-toggle (ionChange)="myFunction(box)"></ion-toggle>
</ion-item>
was simply (ionChange)="myFunction(box)" or (click)="myFunction(box)"
In my case box will be an entire object, passing the id will be enough to perform any action.

Angularjs 2 ng-repeat not working

I am using ng-repeat in angularjs 2 with custom template but it's not working
html
<tr ng-repeat="data in itemList">
<td>{{data.test}}</td>
</tr>
.ts
import {Component, View} from "angular2/core";
#Component({
selector: 'my-app',
templateUrl: 'app/hello.component.html'
})
export class AppComponent {
public itemList = [
{name:"Apple", test:"1"},
{name:"Orange", test:"2"},
{name:"Grapes", test:"3"},
];
}
I don't know what is the problem. It's working in Angularjs 1 but Angularjs 2 not working
You should use *ngFor instead of ng-repeat. There is no ng-repeat with angular2.
Change it as,
<tr *ngFor="let data of itemList">
<td>{{data.test}}</td>
</tr>
DEMO
Here is full syntax of code.
import { Component } from '#angular/core';
#Component({
selector: 'app-root',
template: `
<p>List:</p>
<ul>
<li *ngFor="let item of itemList">
{{ item.name }} {{item.test}}
</li>
</ul>
`,
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app';
public carsList: Array<string>;
public itemList = [
{ name: "Apple", test: "1" },
{ name: "Orange", test: "2" },
{ name: "Grapes", test: "3" },
];
}

How to put tooltip description in each header cell ui-grid

i wont to use angulars bootstrap tooltip in header cell of ui-grid
$scope.gridOptions = {
//some settings
columnDefs: [
{
field: someId,
displayName: 'Name',
headerCellTemplate: 'linkToTemplate' //default headerCellTemplate with uib-tooltip={{tooltipDescription}},
tooltipDescription: 'Tooltip description' //it's not working
}
]
}
headerCellTemplate:
<div role="columnheader">
....
<span class="ui-grid-header-cell-label">
{{col.displayName}}
</span>
<span class="icon" uib-toolotp='{{col.tooltipDescription}}' tooltip-placement='right'></span>//it's not working
....
</div>
what is the right way to put tooltip description in each header cell?
Each cell has col.colDef property with access to all properties like field, displayName, tooltipDescription etc.

How to give angular ui-grids with different data inside angular ui-bootstrap tabs?

I have multiple tabs and each is having angular ui-grid inside it. Each grid should display different data. But i'm facing problem like in one tab data is coming in the grid on page load but in another tab ui-grid itself is not loading. Not getting what is the problem. Please help.
Below is the sample of the code:
<uib-tabset class="nav">
<uib-tab heading="User">
<div ng-include="'./public/partials/tabs/user.html'">
</div>
</uib-tab>
<uib-tab heading="Notes">
<div ng-include="'./public/partials/tabs/notifications.html'">
</div>
</uib-tab>
<uib-tab heading="Assets">
<div ng-include="'./public/partials/tabs/assetsList.html'"></div>
</uib-tab>
<uib-tab heading="Audit Logs">
<div ng-include="'./public/partials/tabs/audit.html'" >
</div>
</uib-tab>
</uib-tabset>
Html for tabs:
<div class="container-fluid">
<form name="assetsForm" novalidate class="form-horizontal">
<div class='container-fluid containerborder'>
<br>
<!--creating table-->
<div data-ui-grid="assetsOptions" data-ui-grid-pagination></div>
</div>
</form>
</div>
<div class="container-fluid">
<form name="userForm" novalidate class="form-horizontal">
<div class='container-fluid containerborder'>
<br>
<!--creating table-->
<div data-ui-grid="userOptions" data-ui-grid-pagination></div>
</div>
</form>
</div>
Controller part:
$scope.data = [
{ name: 'Alex', car: 'Toyota' },
{ name: 'Sam', car: 'Lexus' },
{ name: 'Joe', car: 'Dodge' },
{ name: 'Bob', car: 'Buick' },
{ name: 'Cindy', car: 'Ford' },
];
$scope.userOptions = {
data: 'data',
paginationPageSizes: [5, 10, 25],
paginationPageSize: 5,
columnDefs: [
{name: 'name'},
{name: 'car'}
]
};
$scope.assetsData = [
{ table: 'Brian', class: 'Audi' },
{ table: 'Malcom', class: 'Mercedes Benz' },
{ table: 'Dave', class: 'Ford' },
{ table: 'Stacey', class: 'Audi' },
{ table: 'Amy', class: 'Acura' },
{ table: 'Scott', class: 'Toyota' },
{ table: 'Ryan', class: 'BMW' },
];
$scope.assetsOptions = {
data: 'data',
paginationPageSizes: [5, 10, 25],
paginationPageSize: 5,
columnDefs: [
{name: 'table'},
{name: 'class'}
]
};
If one grid is loading on page load and others are not, it basically means A) other tabs are not initialized on page load B) grid initializing code should be in tab switch instead of page load C) file or data is not available at specified location.
I hope this will help.

$compile does not compile directive template with ngRepeat

After hour of powergoogle I can not imagine why directive`s template does not compile.
So this is my partial view html: (see the ngRepeat)
<appheader currenttab="currentTab"></appheader>
<div class="l-c-r-panes">
<div class="l-pane">
<renters></renters>
</div>
<div class="c-pane">
<div class="form-header" style="position: relative;">
<input class="form-control filter-above-table" type="text" ng-model="filter.$" custom-search />
<table ng-table="invoiceGrid" class="table" id="invoice-table">
<tr ng-repeat="invoice in $data"
ng-click="invoiceGridRowClick(invoice, $data)"
ng-class="{'active': invoice.$selected}" invoice-info-tooltip="invoice">
<td class="invoice-num-column" data-title="'Счет'" sortable="'Invoice'" >{{invoice.Invoice}}</td>
<td data-title="'Арендатор'" sortable="'Renter'">{{invoice.Renter}}</td>
<td class="invoice-sum-column" data-title="'Сумма по счёту'" sortable="'InvoiceSum'">{{invoice.InvoiceSum}}</td>
<td class="invoice-sum-column" data-title="'Оплата (сумма)'" sortable="'PaySum'">{{invoice.PaySum}}</td>
</tr>
</table>
</div>
</div>
<div class="r-pane">
<tasks></tasks>
</div>
</div>
<script type="text/ng-template" id="invoiceTooltipTemplate">
<div ng-repeat="employee in employees">
<div>{{employee.Post}}</div>
<div>{{employee.Name}}</div>
<div>{{employee.Phone}}</div>
<div>{{employee.Info}}</div>
<div>
</script>
And that is my invoiceInfoTooltipDirective:
//require qtip2
angular.module('invoiceModule')
.directive('invoiceInfoTooltip', ['$compile', function ($compile) {
return {
restrict: 'A',
scope: {
invoice: '=invoiceInfoTooltip'
},
link: function (scope, el, attrs) {
if (scope.invoice) {
var tooltipTitle = scope.invoice.Renter;
var tooltipText = '';
scope.employees = scope.invoice.RenterInfo.Employees;
tooltipText = $compile($('#invoiceTooltipTemplate').html())(scope);
el.qtip({
overwrite: true,
content: {
title: tooltipTitle,
text: tooltipText
},
style: {
classes: 'qtip-light invoice-qtip c-invoice-table-tooltip'
},
show: {
event: 'click',
solo: true
},
hide: {
fixed: true,
leave: true,
event: null
},
position: {
my: 'top center',
target: 'mouse',
adjust: {
mouse: false
}
}
});
}
}
};
}]);
directive use template #invoiceTooltipTemplate located in partial view
If this template do not have ngRepate, $compile in directive works fine. But I need iterate some content in template and want to use ngRepeat.
No console errors. Nothing.
If temlate does not compile it return this jquery obj:
[comment, jquery: "2.1.1", constructor: function, selector: "", toArray: function, get: function…]
0: comment
baseURI: null
childNodes: NodeList[0]
data: " ngRepeat: employee in employees "
firstChild: null
jQuery21106483948081731796: undefined
lastChild: null
length: 33
localName: null
namespaceURI: null
nextElementSibling: div.ng-scope
nextSibling: div.ng-scope
nodeName: "#comment"
nodeType: 8
nodeValue: " ngRepeat: employee in employees "
ownerDocument: document
parentElement: null
parentNode: document-fragment
previousElementSibling: null
previousSibling: null
textContent: " ngRepeat: employee in employees "
__proto__: Comment
length: 1
__proto__: Object[0]
I had similar situation in my project, but there`re no ngTable directive with ngRepeat. And it works fine.
Template always must have root element. So easy mistake and I got it..
So template looks like this. And it works.
<script type="text/ng-template" id="invoiceTooltipTemplate">
<div>
<div ng-repeat="employee in employees">
<div>{{employee.Post}}</div>
<div>{{employee.Name}}</div>
<div>{{employee.Phone}}</div>
<div>{{employee.Info}}</div>
</div>
</div>
</script>

Resources