|
| 1 | +import { Component, OnInit } from '@angular/core'; |
| 2 | +import { Column, Editors, FieldType, Formatter, Formatters, GridExtraService, GridExtraUtils, GridOption, OnEventArgs, ResizerService } from './../modules/angular-slickgrid'; |
| 3 | + |
| 4 | +@Component({ |
| 5 | + templateUrl: './grid-additem.component.html' |
| 6 | +}) |
| 7 | +export class GridAddItemComponent implements OnInit { |
| 8 | + title = 'Example 11: Add / Update / Highlight a Datagrid Item'; |
| 9 | + subTitle = ` |
| 10 | + Add / Update / Hightlight an Item from the Datagrid (<a href="https://github.com/ghiscoding/Angular-Slickgrid/wiki/Add,-Update-or-Highlight-a-Datagrid-Item" target="_blank">Wiki link</a>). |
| 11 | + <ul> |
| 12 | + <li><b>Note:</b> this demo is <b>only</b> on the datagrid (client) side, you still need to deal with the backend yourself</li> |
| 13 | + <li>Adding an item, will always be showing as the 1st item in the grid because that is the best visual place to add it</li> |
| 14 | + <li>Add/Update an item requires a valid Slickgrid Selection Model, you have 2 choices to deal with this:</li> |
| 15 | + <ul><li>You can enable "enableCheckboxSelector" or "enableRowSelection" to True</li></ul> |
| 16 | + <li>Click on any of the buttons below to test this out</li> |
| 17 | + <li>You can change the highlighted color & animation by changing the SASS variables:</li> |
| 18 | + <ul> |
| 19 | + <li>"$row-highlight-background-color" or "$row-highlight-fade-animation"</li> |
| 20 | + <li>Take a look at the available <a href="https://github.com/ghiscoding/Angular-Slickgrid/blob/master/src/app/modules/angular-slickgrid/styles/_variables.scss" target="_blank">SASS Variables</a></li> |
| 21 | + </ul> |
| 22 | + </ul> |
| 23 | + `; |
| 24 | + |
| 25 | + columnDefinitions: Column[]; |
| 26 | + gridOptions: GridOption; |
| 27 | + dataset: any[]; |
| 28 | + updatedObject: any; |
| 29 | + |
| 30 | + constructor(private gridExtraService: GridExtraService, private resizer: ResizerService) {} |
| 31 | + |
| 32 | + ngOnInit(): void { |
| 33 | + this.columnDefinitions = [ |
| 34 | + { id: 'title', name: 'Title', field: 'title', sortable: true, type: FieldType.string, editor: Editors.longText }, |
| 35 | + { id: 'duration', name: 'Duration (days)', field: 'duration', sortable: true, type: FieldType.number, editor: Editors.text, |
| 36 | + onCellChange: (args: OnEventArgs) => { |
| 37 | + alert('onCellChange directly attached to the column definition'); |
| 38 | + console.log(args); |
| 39 | + } |
| 40 | + }, |
| 41 | + { id: 'complete', name: '% Complete', field: 'percentComplete', formatter: Formatters.percentCompleteBar, type: FieldType.number, editor: Editors.integer }, |
| 42 | + { id: 'start', name: 'Start', field: 'start', formatter: Formatters.dateIso, sortable: true, type: FieldType.date/*, editor: Editors.date*/ }, |
| 43 | + { id: 'finish', name: 'Finish', field: 'finish', formatter: Formatters.dateIso, sortable: true, type: FieldType.date }, |
| 44 | + { id: 'effort-driven', name: 'Effort Driven', field: 'effortDriven', formatter: Formatters.checkmark, type: FieldType.number, editor: Editors.checkbox } |
| 45 | + ]; |
| 46 | + |
| 47 | + this.gridOptions = { |
| 48 | + asyncEditorLoading: false, |
| 49 | + autoResize: { |
| 50 | + containerId: 'demo-container', |
| 51 | + sidePadding: 15 |
| 52 | + }, |
| 53 | + editable: true, |
| 54 | + enableColumnPicker: true, |
| 55 | + enableCellNavigation: true, |
| 56 | + enableRowSelection: true |
| 57 | + }; |
| 58 | + |
| 59 | + // mock a dataset |
| 60 | + let mockedDataset = []; |
| 61 | + for (let i = 0; i < 1000; i++) { |
| 62 | + const randomYear = 2000 + Math.floor(Math.random() * 10); |
| 63 | + const randomMonth = Math.floor(Math.random() * 11); |
| 64 | + const randomDay = Math.floor((Math.random() * 29)); |
| 65 | + const randomPercent = Math.round(Math.random() * 100); |
| 66 | + |
| 67 | + mockedDataset[i] = { |
| 68 | + id: i, |
| 69 | + title: 'Task ' + i, |
| 70 | + duration: Math.round(Math.random() * 100) + '', |
| 71 | + percentComplete: randomPercent, |
| 72 | + percentCompleteNumber: randomPercent, |
| 73 | + start: new Date(randomYear, randomMonth, randomDay), |
| 74 | + finish: new Date(randomYear, (randomMonth + 1), randomDay), |
| 75 | + effortDriven: (i % 5 === 0) |
| 76 | + }; |
| 77 | + } |
| 78 | + this.dataset = mockedDataset; |
| 79 | + } |
| 80 | + |
| 81 | + addNewItem() { |
| 82 | + const randomId = Math.floor(Math.random() * (10000 - 1000) + 1000); |
| 83 | + const randomYear = 2000 + Math.floor(Math.random() * 10); |
| 84 | + const randomMonth = Math.floor(Math.random() * 11); |
| 85 | + const randomDay = Math.floor((Math.random() * 29)); |
| 86 | + const randomPercent = Math.round(Math.random() * 100); |
| 87 | + |
| 88 | + const newItem = { |
| 89 | + id: randomId, |
| 90 | + title: 'Task ' + randomId, |
| 91 | + duration: Math.round(Math.random() * 100) + '', |
| 92 | + percentComplete: randomPercent, |
| 93 | + percentCompleteNumber: randomPercent, |
| 94 | + start: new Date(randomYear, randomMonth, randomDay), |
| 95 | + finish: new Date(randomYear, (randomMonth + 2), randomDay), |
| 96 | + effortDriven: true |
| 97 | + }; |
| 98 | + this.gridExtraService.addItemToDatagrid(newItem); |
| 99 | + } |
| 100 | + |
| 101 | + highlighFifthRow() { |
| 102 | + this.gridExtraService.highlightRow(4, 1500); |
| 103 | + } |
| 104 | + |
| 105 | + updateSecondItem() { |
| 106 | + const firstItem = this.gridExtraService.getDataItemByRowNumber(1); |
| 107 | + firstItem.duration = Math.round(Math.random() * 100); |
| 108 | + this.gridExtraService.updateDataGridItem(firstItem); |
| 109 | + } |
| 110 | +} |
0 commit comments