|
| 1 | +import { Component, OnInit } from '@angular/core'; |
| 2 | +import { Column, FieldType, Formatter, Formatters, GridExtraService, GridExtraUtils, GridOption } from './../modules/angular-slickgrid'; |
| 3 | + |
| 4 | +@Component({ |
| 5 | + templateUrl: './grid-rowselection.component.html' |
| 6 | +}) |
| 7 | +export class GridRowSelectionComponent implements OnInit { |
| 8 | + title = 'Example 10: Grid with Row Selection'; |
| 9 | + subTitle = ` |
| 10 | + Row selection, single or multi-select (<a href="https://github.com/ghiscoding/Angular-Slickgrid/wiki/Row-Selection">Wiki link</a>). |
| 11 | + `; |
| 12 | + |
| 13 | + columnDefinitions: Column[]; |
| 14 | + gridOptions: GridOption; |
| 15 | + dataset: any[]; |
| 16 | + gridObj: any; |
| 17 | + dataviewObj: any; |
| 18 | + isMultiSelect = true; |
| 19 | + selectedObjects: any[]; |
| 20 | + selectedObject: any; |
| 21 | + |
| 22 | + constructor(private gridExtraService: GridExtraService) {} |
| 23 | + |
| 24 | + ngOnInit(): void { |
| 25 | + this.prepareGrid(); |
| 26 | + } |
| 27 | + |
| 28 | + prepareGrid() { |
| 29 | + this.columnDefinitions = [ |
| 30 | + {id: 'title', name: 'Title', field: 'title', sortable: true, type: FieldType.string}, |
| 31 | + {id: 'duration', name: 'Duration (days)', field: 'duration', sortable: true, type: FieldType.number}, |
| 32 | + {id: 'complete', name: '% Complete', field: 'percentComplete', formatter: Formatters.percentCompleteBar, type: FieldType.number, sortable: true}, |
| 33 | + {id: 'start', name: 'Start', field: 'start', formatter: Formatters.dateIso, sortable: true, type: FieldType.dateIso }, |
| 34 | + {id: 'finish', name: 'Finish', field: 'finish', formatter: Formatters.dateIso, sortable: true, type: FieldType.date }, |
| 35 | + {id: 'effort-driven', name: 'Effort Driven', field: 'effortDriven', formatter: Formatters.checkmark, type: FieldType.number, sortable: true} |
| 36 | + ]; |
| 37 | + this.gridOptions = { |
| 38 | + autoResize: { |
| 39 | + containerId: 'demo-container', |
| 40 | + sidePadding: 15 |
| 41 | + }, |
| 42 | + enableAutoResize: true, |
| 43 | + enableCellNavigation: false, |
| 44 | + enableCheckboxSelector: true |
| 45 | + }; |
| 46 | + |
| 47 | + this.dataset = this.prepareData(); |
| 48 | + } |
| 49 | + |
| 50 | + prepareData() { |
| 51 | + // mock a dataset |
| 52 | + const mockDataset = []; |
| 53 | + for (let i = 0; i < 500; i++) { |
| 54 | + const randomYear = 2000 + Math.floor(Math.random() * 10); |
| 55 | + const randomMonth = Math.floor(Math.random() * 11); |
| 56 | + const randomDay = Math.floor((Math.random() * 29)); |
| 57 | + const randomPercent = Math.round(Math.random() * 100); |
| 58 | + |
| 59 | + mockDataset[i] = { |
| 60 | + id: i, |
| 61 | + title: 'Task ' + i, |
| 62 | + duration: Math.round(Math.random() * 100) + '', |
| 63 | + percentComplete: randomPercent, |
| 64 | + percentCompleteNumber: randomPercent, |
| 65 | + start: new Date(randomYear, randomMonth, randomDay), |
| 66 | + finish: new Date(randomYear, (randomMonth + 1), randomDay), |
| 67 | + effortDriven: (i % 5 === 0) |
| 68 | + }; |
| 69 | + } |
| 70 | + return mockDataset; |
| 71 | + } |
| 72 | + |
| 73 | + gridReady(grid) { |
| 74 | + this.gridObj = grid; |
| 75 | + |
| 76 | + grid.onSelectedRowsChanged.subscribe((e, args) => { |
| 77 | + if (Array.isArray(args.rows)) { |
| 78 | + this.selectedObjects = args.rows.map(idx => { |
| 79 | + const item = grid.getDataItem(idx); |
| 80 | + return item.title || ''; |
| 81 | + }); |
| 82 | + } |
| 83 | + }); |
| 84 | + } |
| 85 | + dataviewReady(dataview) { |
| 86 | + this.dataviewObj = dataview; |
| 87 | + } |
| 88 | + |
| 89 | + onChooseMultiSelectType(isMultiSelect) { |
| 90 | + this.isMultiSelect = isMultiSelect; |
| 91 | + |
| 92 | + this.gridObj.setOptions({ |
| 93 | + enableCellNavigation: !isMultiSelect, |
| 94 | + enableCheckboxSelector: isMultiSelect |
| 95 | + }); // change the grid option dynamically |
| 96 | + this.gridExtraService.setSelectedRows([]); |
| 97 | + |
| 98 | + return true; |
| 99 | + } |
| 100 | +} |
0 commit comments