IE Datepicker too long - html

I have a date picker that is to be IE friendly. However when I use it, it's displaying far too long.
HTML:
<label class="mr-sm-2" for="startDate">Start Date:</label>
<date-picker
class="mr-sm-2"
name="startDate"
formControlName="startDate">
</date-picker>
datepicker.component.ts:
import * as _ from "lodash" ;
import * as moment from 'moment';
import { Component, Input, forwardRef, ViewEncapsulation } from '#angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '#angular/forms';
#Component({
selector: 'date-picker',
template: `
<ng-container *ngIf="isGoodBrowser; else datePickerFallback">
<input
type="date"
class="form-control text-right"
[class.is-invalid]="invalid"
[(ngModel)]="value"
[disabled]="disabled"
/>
</ng-container>
<ng-template #datePickerFallback>
<div class="fallback-date-picker" [class.invalid]="invalid">
<my-date-picker
[options]="options"
[disabled]="disabled"
[(ngModel)]="value"
(dateChanged)="value = $event">
</my-date-picker>
</div>
</ng-template>
`,
encapsulation: ViewEncapsulation.None,
styles: [
`.fallback-date-picker.invalid .mydp {
border-color: #dc3545;
}`,
`.fallback-date-picker .mydp .selection {
text-align: right;
padding-right: 65px;
}`
],
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => DatePickerComponent),
multi: true
}
]
})
export class DatePickerComponent implements ControlValueAccessor {
private _value: any = null;
private propagateChange = _.identity;
isGoodBrowser: boolean = true;
disabled: boolean = false;
options: any = {
dateFormat: 'dd/mm/yyyy',
editableDateField: false
};
constructor() {
const userAgent = window.navigator.userAgent;
if (userAgent.indexOf('MSIE') >= 0) this.isGoodBrowser = false;
if (userAgent.indexOf('Trident') >= 0) this.isGoodBrowser = false;
if (userAgent.indexOf('Edge') >= 0) this.isGoodBrowser = true;
}
#Input() invalid: boolean = false;
#Input()
set value(v: any) {
if (v) {
if (_.isString(v)) {
const mDate = moment(v);
if (this.isGoodBrowser) {
this._value = v;
} else {
this._value = {
date: {
year: mDate.year(),
month: mDate.month() + 1,
day: mDate.date()
}
};
}
this.propagateChange(v);
} else if (v && v.jsdate) {
const mDate = moment(v.jsdate);
this.propagateChange(mDate.format('YYYY-MM-DD'));
} else {
this.propagateChange(null);
}
} else {
this._value = null;
this.propagateChange(null);
}
}
get value() {
return this._value;
}
writeValue(value: any) {
this.value = value ? value : null;
}
registerOnChange(fn: any) {
this.propagateChange = fn;
}
registerOnTouched() { }
setDisabledState(isDisabled: boolean) {
this.disabled = isDisabled;
}
}
The images shows how the date picker displays, it goes across off the screen.
I feel like the problem might be in the html layout as this doesn't happen everytime I use the date picker.

I have solved the issue and it has nothing to do with the code shown.

Related

Display mat-label on custom mat-form-field component, angular

I'm creating a custom input component to be used in my angular material forms, but assigning a mat-label in the form field where the component is used has no effect.
My component HTML is as follows:
<form *ngIf="val">
<table>
<td>
+
</td>
<td>
<input matInput
id="country"
type="text"
[ngModelOptions]="{standalone: true}"
[(ngModel)]="val.country"
class="input"
placeholder="27"
maxlength="2"
(keyup)="phoneFieldFocusHandler($event)"
required>
</td>
<td>
<input matInput
id="area"
type="text"
[ngModelOptions]="{standalone: true}"
[(ngModel)]="val.area"
class="input"
placeholder="949"
maxlength="3"
(keyup)="phoneFieldFocusHandler($event)"
required>
</td>
<td>
<input matInput
id="prefix"
type="text"
[ngModelOptions]="{standalone: true}"
[(ngModel)]="val.prefix"
class="input"
placeholder="555"
maxlength="3"
(keyup)="phoneFieldFocusHandler($event)"
required>
</td>
<td>
<input matInput
id="line"
type="text"
[ngModelOptions]="{standalone: true}"
[(ngModel)]="val.line"
class="input"
placeholder="5555"
maxlength="4"
(keyup)="phoneFieldFocusHandler($event)"
required>
</td>
</table>
</form>
And the corresponding typescript:
import { coerceBooleanProperty } from '#angular/cdk/coercion';
import { Component, forwardRef, HostBinding, Input, OnDestroy, OnInit } from '#angular/core';
import { ControlValueAccessor, NgControl, NG_VALUE_ACCESSOR } from '#angular/forms';
import { MatFormFieldControl } from '#angular/material/form-field';
import { Subject } from 'rxjs';
import { PhoneNumber } from '../phone-number.model';
#Component({
selector: 'app-phone-input',
templateUrl: './phone-input.component.html',
styleUrls: ['./phone-input.component.scss'],
providers: [
{ provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => PhoneInputComponent),
multi: true
},
{
provide: MatFormFieldControl,
useExisting: PhoneInputComponent
}
]
})
export class PhoneInputComponent implements OnInit, OnDestroy, ControlValueAccessor, MatFormFieldControl<any> {
#HostBinding('attr.id')
externalId = '';
val: PhoneNumber = new PhoneNumber();
stateChanges = new Subject<void>();
private _ID = '';
/*tslint:disable-next-line*/
_placeholder: string;
/*tslint:disable-next-line*/
_required: boolean;
/*tslint:disable-next-line*/
_disabled: boolean;
ngControl: NgControl;
focused: boolean;
empty: boolean;
shouldLabelFloat: boolean;
errorState: boolean;
userAriaDescribedBy?: string;
#Input()
set id(value: string) {
this._ID = value;
this.externalId = null;
}
get id(): string {
return this._ID;
}
#Input()
get placeholder(): string {
return this._placeholder;
}
set placeholder(plh) {
this._placeholder = plh;
this.stateChanges.next();
}
#Input()
get required(): boolean {
return this._required;
}
set required(req) {
this._required = coerceBooleanProperty(req);
this.stateChanges.next();
}
#Input()
get disabled(): boolean {
return this._disabled;
}
set disabled(dis) {
this._disabled = coerceBooleanProperty(dis);
this.stateChanges.next();
}
setDescribedByIds(ids: string[]): void {
this.userAriaDescribedBy = ids.join(' ');
}
onContainerClick(event: MouseEvent): void {}
writeValue(value: any): void {
this.value = value;
}
// upon UI element value changes, this method gets triggered
registerOnChange(fn: any): void {
this.onChange = fn;
}
// upon touching the element, this method gets triggered
registerOnTouched(fn: any): void {
this.onTouch = fn;
}
onChange: any = () => {};
onTouch: any = () => {};
set value(val: PhoneNumber) {
if (val !== undefined && this.val !== val) {
this.val = val;
this.onChange(val);
this.onTouch(val);
}
}
constructor() { }
ngOnInit(): void {
}
ngOnDestroy(): void {
this.stateChanges.complete();
}
// Internally handle moving focus from one input to the next
phoneFieldFocusHandler(e: KeyboardEvent): void {
const target = e.target as any as HTMLInputElement;
if (target.value.length >= target.maxLength) {
const parent = target.parentElement.parentElement;
const children = (Array.prototype.slice.call(parent.children) as Array<HTMLElement>).map(el => el.children[0] as HTMLInputElement);
const curr = children.indexOf(target);
if (!(curr >= children.length - 1)) {
children[curr + 1].focus();
}
}
}
}
It is used in the form as:
<mat-form-field>
<mat-label>Phone Number</mat-label>
<app-phone-input formControlName="phone"></app-phone-input>
</mat-form-field>
But the label is not displayed.
Do I need to define a space in my custom component where the label will be shown?

Date picker in Html is not working as expected

I'm working on a requirement where date picker is displayed based on min and max dates when minimum and maximum are provided. But the format shows as attached enter image description here
Requirement is enter image description here
Below is the code snippet i have added HTML and TS.
<mat-form-field>
<input matInput type="date" [min]="todayDate [max]="maxReleaseDate" name='spoDate'
#spoDate="ngModel" [(ngModel)]="newSpoDate" autocomplete="off" required
[disabled]="spoPoCreateDate">
<mat-error *ngIf="spoDate && spoDate.untouched && spoDate.invalid">This is required field
</mat-error>
</mat-form-field>
import { Component, OnInit } from '#angular/core';
import { ActivatedRoute } from '#angular/router';
import { DatePipe } from '#angular/common';
import { Order } from 'app/general-component/models/order';
import { GetService } from 'app/general-component/services/get.service';
import { PostService } from 'app/general-component/services/post.service';
import { environment } from 'environments/environment.prod';
#Component({
selector: 'app-spo-date-popup',
templateUrl: './spo-date-popup.component.html',
styleUrls: ['./spo-date-popup.component.css']
})
export class SpoDatePopupComponent implements OnInit {
currentSpoDate: any;
newSpoDate: any
orderId: any;
selectedOrganization: any;
itemId: any;
orderData: Order;
todayDate: any;
spoCreatePoDateNote: any;
maxReleaseDate: any
orderLineId: any;
currentOrderId: any;
loggedInUser: string;
userGrantList: any;
spoPoCreateDate: boolean;
constructor(private route: ActivatedRoute, private getService: GetService, private postService: PostService, private datePipe: DatePipe) { }
ngOnInit(): void {
let date = this.datePipe.transform(new Date(), 'dd/MM/yyyy');
this.todayDate = (date as string).split('/').reverse().join('-');
this.route.queryParams.subscribe(
(params) => {
this.orderId = params["orderId"];
this.selectedOrganization = params["selectedOrganization"];
this.itemId = params["ItemId"];
this.orderLineId = params["OrderLineId"];
this.currentOrderId = params["OrderId"];
this.loggedInUser = params["loggedInUser"]
},
(error) => console.log("error" + error)
)
this.grantCheck();
this.getService.getData(environment.getOrderByIdURL + this.currentOrderId, this.selectedOrganization).subscribe(res => {
this.orderData = res as Order;
if (this.orderData) {
this.orderData.OrderLine.forEach((e) => {
if (this.orderLineId === e.OrderLineId) {
this.currentSpoDate = e.Extended.PODropDate ? e.Extended.PODropDate : "";
this.maxReleaseDate = e.Extended.ReleaseDate ? this.datePipe.transform(new Date(e.Extended.ReleaseDate), 'yyy-MM-dd') : "";
console.log("SPOPODATEPOPUP :: ", this.maxReleaseDate);
}
})
}
});
let configData = {};
this.getService.getData(`${environment.configStoreURL}/REST_EX01`, this.selectedOrganization).subscribe((res) => {
let REST_EX01_CONFIG = res.ConfigStoreData;
if (res.ConfigStoreData) {
let list = REST_EX01_CONFIG.split(';');
list.forEach(e => {
let ex01Configs = e.split('=');
configData = {
...configData,
[ex01Configs[0]]: ex01Configs[1]
}
})
// #ts-ignore
this.spoCreatePoDateNote = configData.SPOPOCreateDateUpdateNote;
}
})
}
updateDate(res: any) {
console.log(res);
if (res && res.spoDate) {
if (this.orderData && this.orderData.OrderLine) {
let orderLineData = this.orderData.OrderLine.find((i) => i.OrderLineId === this.orderLineId);
let isAllOLSame = this.orderData.OrderLine.every((e) =>
e.OrderLinePromisingInfo && e.FulfillmentGroupId &&
e.FulfillmentGroupId === orderLineData.FulfillmentGroupId &&
e.OrderLinePromisingInfo.ShipFromLocationId === orderLineData.OrderLinePromisingInfo.ShipFromLocationId);
this.orderData.OrderLine.forEach((e) => {
if (this.orderLineId === e.OrderLineId) {
e.Extended.PODropDate = this.datePipe.transform(res.spoDate, 'MM/dd/yyy');
e.Extended.SPOPOCreateDateUpdated = true;
} else {
e.Extended.PODropDate = e.Extended.PODropDate;
e.Extended.SPOPOCreateDateUpdated = e.Extended.SPOPOCreateDateUpdated;
}
if (isAllOLSame) {
e.Extended.PODropDate = this.datePipe.transform(res.spoDate, 'MM/dd/yyy');
e.Extended.SPOPOCreateDateUpdated = true;
}
})
this.currentSpoDate = this.datePipe.transform(res.spoDate, 'MM/dd/yyy');
this.saveOrder(this.orderData)
}
}
// Extended.SPOPOCreateDateUpdated is flipped to true on updating
}
saveOrder(order: any) {
// let saveOrder: any;
// let postURL = environment.post_order_save;
// saveOrder = this.postService.postData(postURL, this.selectedOrganization, order);
this.postService.postData(environment.post_order_save, this.selectedOrganization, order)
.switchMap((res) => {
let data = {}
return this.postService.postData(environment.rhFetaFocURL, this.selectedOrganization, data)
})
.subscribe(response => {
},
error => { throw error; }
);
}
grantCheck() {
this.getService.getData(environment.getUserGrant + this.loggedInUser, this.selectedOrganization).subscribe(response => {
this.userGrantList = response;
let spoPoCreateDateGrant = this.userGrantList.some((res) => {
return res.ResourceId === "SPOPOCreateDateUpdate";
})
if (!spoPoCreateDateGrant) {
this.spoPoCreateDate = true;
} else {
this.spoPoCreateDate = false
}
console.log('SPOPOCREATEDATE', this.spoPoCreateDate);
});
}
}
can anyone suggest how to fix?
This is might solve your problem
<input type="date" value="2022-02-03" min="2022-02-03" max="2022-03-03">

How to use checkboxes to filter a simple table in angular 7

I am trying to use checkboxes to filter my table.
Its a normal table i do use bootstrap MDB tho.
I think I'm on the right path tho and i don't know what I'm doing wrong with the checkbox or the table :
This is my table :
<table mdbTable striped="true" [hidden]="!show" >
<thead>
<tr>
<th *ngFor="let head of tableColumn" scope="col">{{head}} </th>
</tr>
</thead>
<tbody>
<tr mdbTableCol *ngFor="let data of dataSourceFilterd | paginate: { id: 'listing_pagination', itemsPerPage: 10, currentPage: p, totalItems: totalPage }" >
<th scope="row">{{data.AITOR}}</th>
<td>{{data.SOG_MCOLH}}</td>
<td>{{data.GOBH_MCOLH}}</td>
<td>{{data.AORKH_MCOLH}}</td>
<td>{{data.MCOLH_N7}}</td>
<td>{{data.MCOLH_AAAA}}</td>
<td>{{data.TAOR_QTSR_EBRI}}</td>
<td>{{data.QOD_MCHSN}}</td>
<td>{{data.STTOS_RASHI_4_1}}</td>
<td>{{data.LQOCH_SHM_MQOTSR_EBRI}}</td>
<td>{{data.LQOCH_SHM_LOEZI_QTSR}}</td>
<td>{{data.LQOCH_QOD_LQOCH}}</td>
</tr>
</tbody>
</table>
This is the check box :
<input type="checkbox" [(ngModel)]="filter.OT" (ngModelChange)="filterChange()" />OT<br>
This is the Component :
import { Component, OnInit, ViewChild} from '#angular/core';
import { MarinServiceService } from 'src/app/services/marin-service.service';
#Component({
selector: 'app-containers-page',
templateUrl: './containers-page.component.html',
styleUrls: ['./containers-page.component.scss']
})
export class ContainersPageComponent implements OnInit {
tableColumn = ['AITOR', 'SOG_MCOLH', 'GOBH_MCOLH', 'AORKH_MCOLH', 'MCOLH_N7', 'MCOLH_AAAA', 'TAOR_QTSR_EBRI', 'QOD_MCHSN', 'STTOS_RASHI_4_1', 'LQOCH_SHM_MQOTSR_EBRI', 'LQOCH_SHM_LOEZI_QTSR', 'LQOCH_QOD_LQOCH'];
RG: any;
p: number = 1;
dataSource: any = [];
totalPage: number;
public show: boolean = false;
public tableHide: any = 'Show';
searchText: string = "";
total: any;
dataSourceFilterd: any = [];
filter = {
RG: true,
OT: true,
HC: true
};
constructor(private marinService: MarinServiceService) {}
ngOnInit() {
this.marinService.getAllContainers().subscribe((result) => {
this.dataSource = result;
this.total = this.dataSource.length;
this.dataSourceFilterd = this.dataSource;
})
}
toggle() {
this.show = !this.show;
if (this.show) {
this.tableHide = "Hide";
} else {
this.tableHide = "Show";
}
}
public doFilter = (value: string) => {
this.dataSource.filter = value.trim().toLocaleLowerCase();
}
public getRowsValue(flag) {
if (flag === null) {
return this.dataSource.length;
} else {
return this.dataSource.filter(i => (i.state == flag)).length;
}
}
filterChange() {
this.dataSourceFilterd = this.dataSource.filter(x =>
(x.category === 'RG' && this.filter.RG) ||
(x.category === 'OT' && this.filter.OT) ||
(x.category === 'HC' && this.filter.HC)
);
}
}
I have tried moving the checkbox into the table - Din't work.
I have tried changing the inputs for the checkbox - Didn't work.

How to fix ngFor each row clickStatus value change when click on that row only

am new for this angular6
i have done code for generating div with *ngFor
i have tried to click on each row (span text) then that row Boolean value should effect but its changing the other row values also.
my goal is when click on span row then that clicked row value should change.
component.ts
import { Component, NgModule, VERSION, Input, OnInit } from '#angular/core';
import { BrowserModule } from '#angular/platform-browser';
import { FormsModule } from '#angular/forms';
#Component({
selector: 'my-app-test',
template:
<div>
<h1>Hello</h1>
<div id="{{obj.name}}" *ngFor="let obj of objList">
<span id="{{obj.name}}" (clickStatus)='false'
(click)='changeColor($event, obj)'>{{obj.text}}</span>
<!-- [(ngModel)]="clickStatus" ngDefaultControl -->
</div>
</div>
,
})
export class TestComponent implements OnInit {
name: string;
#Input() clickStatus: boolean = false;
#Input() objList: any[] = [];
objData = {
name : 'name',
text : 'text'
};
list = [];
constructor() {
this.name =Angular! v${VERSION.full};
}
ngOnInit() {
for (let i = 0; i < 10; ++i) {
this.objData = {
name : 'name' + i,
text : 'text' + i
};
this.list.push(this.objData);
}
console.log('data .. ', this.list);
this.objList = this.list;
}
changeColor(event, obj) {
console.log('event...', event.target as Element);
console.log('obj and clickstatus ...', obj, this.clickStatus);
if (this.clickStatus) {
console.log('click status in if true', this.clickStatus);
} else {
console.log('click status in else false', this.clickStatus);
}
this.clickStatus = !this.clickStatus;
}
}
My code Editor : Code
Well, your using the same ngmodel for all your rows ofcorse it will change everyone.
If you want to do it like this make it as Array.
<div id="{{obj.name}}" *ngFor="let obj of objList;let i = index">
<span id="{{obj.name}}" [(ngModel)]="clickStatus[i]" ngDefaultControl (click)='changeColor($event, obj,i)'>
{{obj.text}}</span>
</div>
public clickStatus:Array<boolean>= new Array(this.objList.length);
changeColor(event, obj,i) {
console.log('event...', event.target as Element);
console.log('obj and clickstatus ...', obj, this.clickStatus);
if (this.clickStatus[i]) {
console.log('click status in if true', this.clickStatus[i]);
} else {
console.log('click status in else false', this.clickStatus[i]);
}
this.clickStatus[i] = !this.clickStatus[i];
}
something like this should work.

How to set default format as ('dd/mm/yyyy') in ng-bootstrap

https://puu.sh/yThgV/bd55df9829.png
html
<label for="date">{{ "date" | translate }}</label>
<input type="date" class="form-control checking-field" id="date">
I want to get that format ('dd/mm/yyyy'). Any Suggestion?
extends my comment
In your app.module
import { NgbDatepickerConfig, NgbDateParserFormatter } from '#ng-bootstrap/ng-bootstrap';
import { NgbDateFRParserFormatter } from "./ngb-date-fr-parser-formatter"
#Component({
providers: [{provide: NgbDateParserFormatter, useClass: NgbDateFRParserFormatter}]
})
export class AppComponent {}
In your NgbDateFRParserFormater
//your NgbDateFRParserFormater extends from NgbDateParserFormatter
//Is a Injectable that have two functions
#Injectable()
export class NgbDateFRParserFormatter extends NgbDateParserFormatter {
parse(value: string): NgbDateStruct { //parse receive your string dd/mm/yyy
//return a NgbDateStruct
//calculate year,month and day from "value"
return {year:year,month:month,day:day}
}
format(date: NgbDateStruct): string { //receive a NgbDateStruct
//return a string
return ''+date.day+'/'+date.month+'/'+date.year;
}
}
My complete working solution:
Create a folder named "date-picker" and add the following files inside it.
date-picker > ngb-date-parser-formatter-ext.service.ts
date-picker > index.ts
The file
date-picker > ngb-date-parser-formatter-ext.service.ts
import { NgbDateParserFormatter, NgbDateStruct } from "#ng-bootstrap/ng-bootstrap";
import { Injectable } from "#angular/core";
#Injectable()
export class NgbDateParserFormatterExtService extends NgbDateParserFormatter {
private dateSeparatorChar: string = "-";
// dateFormat should be "yyyy-MM-dd" or "dd-MM-yyyy" with the valid separator.
private dateFormat = `yyyy${this.dateSeparatorChar}MM${this.dateSeparatorChar}dd`;
parse(value: string): NgbDateStruct {
if (value === "") {
return null;
}
const dateString: string = value;
const dateValues = dateString.split(this.dateSeparatorChar);
if (dateValues.length !== 3) {
return null;
}
let dayIndex: number;
let yearIndex: number;
if (this.dateFormat === "dd-MM-yyyy") {
dayIndex = 0;
yearIndex = 2;
} else {
dayIndex = 2;
yearIndex = 0;
}
const year = Number(dateValues[yearIndex]);
const month = Number(dateValues[1]);
const day = Number(dateValues[dayIndex]);
const date: NgbDateStruct = {
year: year, month: month, day: day
};
return date;
}
format(date: NgbDateStruct): string {
if (date === null) {
return "";
}
const dayString: string = date.day < 10 ? `0${date.day}` : `${date.day}`;
const monthString: string = date.month < 10 ? `0${date.month}` : `${date.month}`;
const separator = this.dateSeparatorChar;
const dateString = this.dateFormat === "dd-MM-yyyy"
? `${dayString}${separator}${monthString}${separator}${date.year}`
: `${date.year}${separator}${monthString}${separator}${dayString}`;
return dateString;
}
}
The file
date-picker > index.ts
import { NgbDateParserFormatter } from "#ng-bootstrap/ng-bootstrap";
import { NgbDateParserFormatterExtService } from "./ngb-date-parser-formatter-ext.service";
export const NgbDateParserFormatterExtProvider =
{ provide: NgbDateParserFormatter, useClass: NgbDateParserFormatterExtService };
In the file
app.module.ts
import { NgbDateParserFormatterExtProvider } from "./common/services/date-picker";
#NgModule({
providers: [...., NgbDateParserFormatterExtProvider],
})
export class AppModule { }
In your
your-component1.component.html
<div class="form-group">
<label>Publish Date</label><span> * </span>
<input #datepicker="ngbDatepicker" class="form-control" name="datepicker" formControlName="publishDate"
ngbDatepicker (click)="datepicker.toggle()" placeholder="yyyy-MM-dd" />
</div>
I'm using Reactive forms.
You can change the date format to "dd-MM-yyyy" or "dd/MM/yyyy" by setting the variable
private dateFormat = `yyyy${this.dateSeparatorChar}MM${this.dateSeparatorChar}dd`;
Regards..
In general you need to use directive.
So, you can use one of ready-made date-time pickers for angular. Like this: https://www.npmjs.com/package/angular-date-time-input
Or create your own directive to make it work.
myModule.directive(
'dateInput',
function(dateFilter) {
return {
require: 'ngModel',
template: '<input type="date"></input>',
replace: true,
link: function(scope, elm, attrs, ngModelCtrl) {
ngModelCtrl.$formatters.unshift(function (modelValue) {
return dateFilter(modelValue, 'yyyy-MM-dd');
});
ngModelCtrl.$parsers.unshift(function(viewValue) {
return new Date(viewValue);
});
},
};
});
I took this code from: https://stackoverflow.com/a/18062552/3710672