Skip to content

Instantly share code, notes, and snippets.

@ruisebastiao
Forked from brgaulin/Readme.md
Created December 30, 2018 10:38
Show Gist options
  • Save ruisebastiao/023941748def07aff3b6d5535f277557 to your computer and use it in GitHub Desktop.
Save ruisebastiao/023941748def07aff3b6d5535f277557 to your computer and use it in GitHub Desktop.
ng2-smart-table datepicker

Install

npm install --save ng-pick-datetime

Setup

Add the component in this gist to your project Add to your smart table settings:

{
  columns: {
        startDate: {
        title: 'Start Time',
        type: 'custom',
        renderComponent: SmartTableDatepickerRenderComponent,
        width: '250px',
        filter: false,
        sortDirection: 'desc',
        editor: {
          type: 'custom',
          component: SmartTableDatepickerComponent,
        }
      },
      endDate: {
        title: 'End Time',
        type: 'custom',
        renderComponent: SmartTableDatepickerRenderComponent,
        width: '250px',
        filter: false,
        editor: {
          type: 'custom',
          component: SmartTableDatepickerComponent,
          config: {
            placeholder: 'End Time'
          }
        }
      }
   }
}
<div class="input-group">
<span [owlDateTimeTrigger]="dt" class="input-group-addon"><i class="fa fa-calendar"></i></span>
<input
[owlDateTimeTrigger]="dt" [owlDateTime]="dt"
[(ngModel)]="inputModel"
placeholder="{{placeholder}}"
[min]='min' [max]='max'
readonly
class="form-control">
</div>
<owl-date-time #dt [stepMinute]="15" [hour12Timer]='true' (afterPickerClosed)="onChange()"></owl-date-time>
.fa {
font-size: 1.2rem;
}
input {
padding: .375em .75em !important;
}
import { Component, OnInit, Input } from '@angular/core';
import { DefaultEditor, ViewCell } from 'ng2-smart-table';
@Component({
selector: 'smart-table-datepicker',
templateUrl: './smart-table-datepicker.component.html',
styleUrls: ['./smart-table-datepicker.component.scss']
})
export class SmartTableDatepickerComponent extends DefaultEditor implements OnInit {
@Input() placeholder: string = 'Choose a Date/Time';
@Input() min: Date; // Defaults to now(rounded down to the nearest 15 minute mark)
@Input() max: Date; // Defaults to 1 month after the min
stringValue;
inputModel: Date;
constructor() {
super();
}
ngOnInit() {
if(!this.min) {
this.min = new Date();
this.min.setMinutes(Math.floor(this.min.getMinutes() / 15) * 15 );
}
if(!this.max) {
this.max = new Date(this.min);
this.max.setFullYear(this.min.getFullYear() + 1);
}
if(this.cell.newValue) {
let cellValue = new Date(this.cell.newValue);
if(cellValue.getTime() >= this.min.getTime() && cellValue.getTime() <= this.max.getTime()) {
this.inputModel = cellValue;
this.cell.newValue = this.inputModel.toISOString();
}
}
if(!this.inputModel) {
this.inputModel = this.min;
this.cell.newValue = this.inputModel.toISOString();
}
}
onChange() {
if(this.inputModel) {
this.cell.newValue = this.inputModel.toISOString();
}
}
}
@Component({
template: `{{value | date:'short'}}`,
})
export class SmartTableDatepickerRenderComponent implements ViewCell, OnInit {
@Input() value: string;
@Input() rowData: any;
constructor() { }
ngOnInit() { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment