How to bind dropdown value dynamically in Angular 7 - angular7

I have a dropdown, the values of this dropdown should be bind dynamically.
<mat-select formControlName="approver">
<mat-option *ngFor="let action of approver; let i = index" [value]="approver[i].value">
{{approver[i].viewValue}}
</mat-option>
</mat-select>
I have a grid and selected rows column values should be the value of dropdown.
getApprover() {
console.log('123');
this.selectedRowData = this.data.Dialog_rowData;
for (let i = 0; i < this.selectedRowData.length; i++) {
this.approver[i] = [
{ value: this.selectedRowData[i].approverList.login0,
viewValue: this.selectedRowData[i].approverList.name0 }
];
}
console.log(this.approver);
return this.approver;
}
export interface ExApprover {
value: string;
viewValue: string;
}
approver: ExApprover[] = [
{value: 'Brasile, Theresa', viewValue: 'Brasile, Theresa'}, ];
constructor(private formBuilder: FormBuilder) {
this.form = this.formBuilder.group({
approver: [ { value: '', viewValue: ''}]
});
of(this.getApprover()).subscribe(approver => {
this.approver = approver;
});
}
ngOnInit() {
}
I am getting error like 'Cannot find control with name: 'user Input' and values are not loaded.

Related

Dynamically Enable/Disable Mat-Selection-List Mat-List-Option

I have the following mat-selection-list. I need it to enable the options in a cascading fashion. i.e. The Second Selection will not be enabled until the First Selection is selected. The Third Selection will not be enabled until the Second Selection is select, etc...
Here is the html code for the list:
<mat-selection-list id='selectedParameters' [(ngModel)]="selectedCriteria">
<mat-list-option *ngFor="let item of incomingParameters" [selected]="item.selected" [value]="item.name"
[disabled]="item.disabled" checkboxPosition="before" (click)="GetChange(item)">
{{item.name}}
</mat-list-option>
</mat-selection-list>
Here is how I populate the List:
if (this.data.criteria.FirstSelection) {
arr.push({
name: 'First Selection',
selected: false,
disabled: 'false',
});
}
if (this.data.criteria.SecondSelection) {
arr.push({
name: 'Second Selection',
selected: false,
disabled: 'true',
});
}
if (this.data.criteria.ThirdSelection) {
arr.push({
name: 'Third Selection',
selected: false,
disabled: 'true',
});
}
if (this.data.criteria.FourthSelection) {
arr.push({
name: 'Fourth Selection',
selected: false,
disabled: 'true',
});
}
if (this.data.criteria.FifthSelection) {
arr.push({
name: 'Fifth Selection',
selected: false,
disabled: 'true',
});
}
if (this.data.criteria.Sixth Selection) {
arr.push({
name: 'Sixth Selection',
selected: false,
disabled: 'true',
});
}
How can I cascade the enabling/disabling of the options?
Nice question, here is my solution, get the length of the selected array and enable those that are less than the value!
html
<h1 class="mat-headline">MatSelectionList</h1>
<h2 class="mat-subheading-1">Input compareWith is not triggered</h2>
<mat-selection-list
#matSelectionList
[compareWith]="compareServiceTypes"
[(ngModel)]="data"
(ngModelChange)="uncheck($event)"
multiple
>
<mat-list-option
checkboxPosition="before"
[value]="s.id"
[selected]="data.includes(s.id)"
*ngFor="let s of serviceTypes; trackBy: s?.id; let i = index"
[disabled]="i > getMax()"
>
{{ s.label }}
</mat-list-option>
</mat-selection-list>
{{ data | json }}
<div class="mat-small material-version">
#angular/material: {{ version.full }}
</div>
ts
import { ViewChild } from '#angular/core';
import { Component } from '#angular/core';
import { MatSelectionList, VERSION } from '#angular/material';
#Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent {
#ViewChild('matSelectionList') matSelectionList: MatSelectionList;
version: any;
data: any;
serviceTypes = [];
constructor() {
this.version = VERSION;
this.data = [];
setTimeout(() => {
this.serviceTypes = [
{
id: 1,
label: 'Service type 1',
},
{
id: 2,
label: 'Service type 2',
},
{
id: 3,
label: 'Service type 2',
},
{
id: 4,
label: 'Service type 2',
},
{
id: 5,
label: 'Service type 2',
},
];
}, 1000);
}
uncheck(value) {
let found = null;
this.serviceTypes.forEach((item, index) => {
if (
(this.data[index] || this.data[index] === 0) &&
item.id !== this.data[index] &&
!(found || found === 0)
) {
found = index;
}
});
if (found || found === 0) {
this.data.splice(found);
}
}
getMax() {
return this.data.length;
}
compareServiceTypes(st1: any, st2: any) {
console.log('compareServiceTypes');
// const result = st1 && st2 ? st1.id === st2.id : st1 === st2;
return st1 && st2 ? st1.id === st2.id : st1 === st2;
}
}
stackblitz

How to use useIntl hook and localize the array elements

I have a basic react functional component and I bind an array in which there are strings to be localized. Is there any other way to do it? I am trying as below and it says "Invalid Hook Call"
import { useIntl } from "react-intl";
const NavBasicExample: React.FunctionComponent = () => {
return (
<Nav
groups={navLinkGroups}
/>
</div>
);
};
const navLinkGroups: INavLinkGroup[] = [
{
name: getFormattedString(Strings.details),//This fails and says invalidHookCall
links: [{ name: Strings.appDetails, url: "" }]
},
{
name: Strings.capabilities,
links: [
{ name: Strings.tabs},
{ name: Strings.bots}
]
}
];
const getFormattedString = (inputString: string) => {
const intl = useIntl(); //This fails.
return intl.formatMessage({ id: "details", defaultMessage: "Login });
};
The problem is that you are calling a Hook from a non-react function. You are not allowed to do that. Try moving the "navLinkGroups" into the "NavBasicExample" and it should work
import { useIntl } from "react-intl";
const NavBasicExample: React.FunctionComponent = () => {
const intl = useIntl();
const navLinkGroups: INavLinkGroup[] = [
{
name: getFormattedString(Strings.details),
links: [{ name: Strings.appDetails, url: "" }]
},
{
name: Strings.capabilities,
links: [
{ name: Strings.tabs},
{ name: Strings.bots}
]
}
];
const getFormattedString = (inputString: string) => {
return intl.formatMessage({ id: "details", defaultMessage: "Login" });
};
return (
<Nav
groups={navLinkGroups}
/>
</div>
);
};

Material Angular Mutliple table dataSource + Material Angular Mutliple table in single template 'Could not find column with id'

In single page I want to make two tables in tabs. The tabs are workin fine. If I comment any of the tables in html then the single table is loading and I try to get data with consol.table the data also working, but in html it is giving error:
Material Anguler Mutliple table dataSource + Material Anguler Mutliple table in single templete 'Could not find column with id'
//table 1
<table mat-table [dataSource]="dataSource" class="table" matSort></table>
//table 2
<table mat-table [dataSource]="dataSource2" class="table" matSort></table>
//TS file Code for Material Anguler in ts file
import { Component, OnInit, ViewChild } from '#angular/core';
import { DataSource } from '#angular/cdk/table';
import { MatPaginator, MatSort, MatTableDataSource } from '#angular/material';
import { SelectionModel } from '#angular/cdk/collections';
export interface Health_Center_Site {
value: string;
viewValue: string;
}
#Component({
selector: 'app-interaction',
templateUrl: './interaction.component.html',
styleUrls: ['./interaction.component.scss']
})
export class InteractionComponent implements OnInit {
Health_Center_Sites: Health_Center_Site[] = [
{ value: '2 Gotham', viewValue: '2 Gotham' },
{ value: 'Astoria Health Center', viewValue: 'Astoria Health Center' },
{ value: 'Bushwick Health Center', viewValue: 'Bushwick Health Center' },
{ value: 'Corona Health Center', viewValue: 'Corona Health Center' },
{ value: 'Crown Heights Health Center', viewValue: 'Crown Heights Health Center' },
{ value: 'East Tremont Health Center', viewValue: 'East Tremont Health Center' },
{ value: 'Fort Greene Health Center', viewValue: 'Fort Greene Health Center' },
{ value: 'Homecrest Health Center', viewValue: 'Homecrest Health Center' },
{ value: 'Morrisania Health Center', viewValue: 'Morrisania Health Center' },
{ value: 'Parsons Health Center', viewValue: 'Parsons Health Center' },
{ value: 'Pop-up LIC', viewValue: 'Pop-up LIC' },
{ value: 'Riverside Health Center', viewValue: 'Riverside Health Center' }
];
panelOpenState = false;
displayedColumns: string[] = ['Contacted_On','Contacted_By','Mode_of_Contact','Time_Spend','Outcome','Outcome_Reason','Comments','follow_up_Date','action'];
dataSource = new MatTableDataSource(ELEMENT_DATA);
displayedColumns2: string[] = ['Transaction','Time_in_source_status','Last_Assignee','Transaction_Date' ];
dataSource2 = new MatTableDataSource(ELEMENT_DATA2);
applyFilter(filterValue: string) {
this.dataSource.filter = filterValue.trim().toLowerCase();
}
#ViewChild(MatPaginator) paginator: MatPaginator;
#ViewChild(MatSort) sort: MatSort;
constructor() { }
ngOnInit() {
console.table(ELEMENT_DATA2);
}
}
export interface ActivityDetailsDataTable {
Contacted_On: string;
Contacted_By: string;
Mode_of_Contact: string;
Time_Spend: string;
Outcome: string;
Outcome_Reason: string;
Comments: string;
follow_up_Date: string;
action: boolean;
}
const ELEMENT_DATA: ActivityDetailsDataTable[] = [
{ Contacted_On: '10/19/2018', Contacted_By: 'Kagno, Natalya', Mode_of_Contact: 'Call', Time_Spend: '1:00 Hrs', Outcome: 'Not Started', Outcome_Reason: 'Open Enrollment Closed, Special Exception Do Not Apply', Comments: 'Client did not have childs ss number on hand', follow_up_Date: '10/19/2018 ', action: true, }
];
export interface TransactionDetailsDataTable {
Transaction: string;
Time_in_source_status: string;
Last_Assignee: string;
Transaction_Date: string;
}
const ELEMENT_DATA2: TransactionDetailsDataTable[] = [
{ Transaction: 'Inprogress >> Complete ', Time_in_source_status: '13:30', Last_Assignee: 'Kagno, Natalya', Transaction_Date: '08/18/2018' }
];
Use this display formate
for table 1
<tr mat-header-row *matHeaderRowDef="displayedColumns2"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns2;"></tr>
for table 2
<tr mat-header-row *matHeaderRowDef="displayedColumns2"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns2;"></tr>

filter the ui grid value based on the row check box is selected

This is the ui grid code( minimal)
//js file
vm.gridOptions1 = {
enableColumnResizing: true,
enableAutoResizing: false,
columnDefs: [
{
field: 'createdDate',
displayName: 'Created Date',
type: 'date',
cellFilter: 'date:"dd-MM-yyyy"',
enableHiding: false, headerTooltip: 'Created Date'
},{
name: 'Refer',
displayName: 'Refer', enableSorting: false, headerTooltip: 'Refer',
cellTemplate: '<input type="checkbox" ng-model="row.entity.isReferred" />'
}
]});
On click of this byutton I need to filter, get only rows which check box is selected(isReferred = true)
//html file
<button type="button" class="btn btn-primary-joli " ng-click="srchctrl.send">Send</button>
This is the file trying to get the filtered list based on the redeffered check box value, but its not working.
//JS file
vm.send = function () {
if (vm.gridApi.selection.data != null && vm.gridApi.selection.data != undefined) {
vm.referredList = filterFilter(vm.gridApi.selection.data, {
isReferred: true
});
console.log("referredList :"+JSON.stringify(referredList));
}
};
How can I get all the value ticked. I don't want to invoke method on each click event on check box.
I think the easiest way to achieve this is by using the gridApi.grid.registerRowsProcessor function. I have adapted a Plunker to show what I mean:
http://plnkr.co/edit/lyXcb90yQ0ErUJnSH7yF
Apps.js:
var app = angular.module('plunker', ['ui.grid']);
app.controller('MainCtrl', ['$scope', 'uiGridConstants', function($scope, uiGridConstants) {
$scope.gridOptions = {
columnDefs: [
{field: 'col1', displayName: 'Column 1', width: 175},
{field: 'col2', displayName: 'isReferred', width: '*'}
],
data: [
{ col1: "Hello 1",col2: true},
{ col1: "Hello 2", col2: false},
{ col1: "Hello 3", col2: true},
{ col1: "Hello 4", col2: false},
{ col1: "Hello 5", col2: true}
],
enableFiltering: true,
onRegisterApi: function(gridApi) {
$scope.gridApi = gridApi;
}
};
$scope.Filter = Filter;
$scope.ShowAll = ShowAll;
function ShowAll() {
$scope.gridApi.grid.removeRowsProcessor(myFilter);
$scope.gridApi.core.queueGridRefresh();
}
function Filter() {
$scope.gridApi.grid.registerRowsProcessor(myFilter, 150);
$scope.gridApi.core.queueGridRefresh();
}
function myFilter(renderableRows) {
renderableRows.forEach( function(row) {
row.visible = row.entity.col2;
});
return renderableRows;
}
}]);
Clicking the Filter button will register the myFilter RowsProcessor, which will iterate through all rows to alter the visible attribute.
Clicking the ShowAll button will remove the RowsProcessor, thus showing all previously hidden rows again.
Whenever an isReferred value changes, the filter will cause the grid to automatically update this change.

Export data from jqxgrid

I want to export all data in my jqxgrid into json and send it to another page via AJAX.
My problem is when I click export button, the data in the grid and data before export was not the same. It change float number to Interger. Here is my code:
Javascript:
$('#export_bt').on('click', function(){
var row = $("#jqxgrid").jqxGrid('exportdata', 'json');
$('#debug').html(row);
console.log(row);
});
var tableDatas = [
{"timestamp":"06:00:00","A":99.49,"B":337.77,"C":155.98},
{"timestamp":"07:00:00","A":455.67,"B":474.1,"C":751.68},
{"timestamp":"08:00:00","A":1071.02,"B":598.14,"C":890.47}
];
var tableDatafields = [
{"name":"timestamp","type":"string"},
{"name":"A","type":"number"},
{"name":"B","type":"number"},
{"name":"C","type":"number"}
];
var tableColumns = [
{"text":"Times","datafield":"timestamp","editable":"false","align":"center","cellsalign":"center","width":150},
{"text":"A","datafield":"A","editable":"false","align":"center"},
{"text":"B","datafield":"B","editable":"false","align":"center"},
{"text":"C","datafield":"C","editable":"false","align":"center"}
];
function setTableData(table_data,table_column,table_datafields)
{
sourceTable.localdata = table_data;
sourceTable.datafields = table_datafields;
dataAdapterTable = new $.jqx.dataAdapter(sourceTable);
$("#jqxgrid").jqxGrid({columns:table_column});
$("#jqxgrid").jqxGrid('updatebounddata');
$('#jqxgrid').jqxGrid('sortby', 'timestamp', 'asc');
$("#jqxgrid").jqxGrid('autoresizecolumns');
for(var i=0;i<table_column.length;i++){
$('#jqxgrid').jqxGrid('setcolumnproperty',table_column[i].datafield,'cellsrenderer',cellsrenderer);
}
}
var cellsrenderer = function (row, columnfield, value, defaulthtml, columnproperties) {
if (value||value===0) {
return value;
}
else {
return '-';
}
};
var sourceTable ={ localdata: '', datatype: 'array'};
var dataAdapterTable = new $.jqx.dataAdapter(sourceTable);
dataAdapterTable.dataBind();
$("#jqxgrid").jqxGrid({
width: '500',
autoheight:true,
source: dataAdapterTable,
sortable: true,
columnsresize: false,
selectionmode: 'none',
columns: [{ text: '', datafield: 'timestamp', width:'100%' , editable: false, align:'center'}]
});
setTableData(tableDatas,tableColumns,tableDatafields);
Html:
<div id="jqxgrid"></div>
<button id="export_bt">Export</button>
<div id="debug"></div>
http://jsfiddle.net/jedipalm/jHE7k/1/
You can add the data type in your source object as below.
datafields: [{ "name": "timestamp", "type": "number" }]
And also I suggest you to apply cellsformat in your column definition.
{ text: 'timestamp', datafield: 'timestamp', cellsalign: 'right', cellsformat: 'd' }
The possible formats can be seen here.
Hope that helps
You can export data in very fast way just like it is id jqxGrid with
var rows = $("#jqxGrid").jqxGrid("getrows");
It will be json array.

Resources