|
1 | | -import { Component } from '@angular/core'; |
2 | | -import { ClickEvent } from 'devextreme/ui/button'; |
| 1 | +import { AfterViewInit, Component, ViewChild } from '@angular/core'; |
| 2 | +import DataSource from 'devextreme/data/data_source'; |
| 3 | +import * as AspNetData from 'devextreme-aspnet-data-nojquery'; |
| 4 | +import { DxDropDownBoxComponent, DxDataGridComponent } from 'devextreme-angular'; |
| 5 | +import type { DxDataGridTypes } from 'devextreme-angular/ui/data-grid'; |
| 6 | +import type { DxDropDownBoxTypes } from 'devextreme-angular/ui/drop-down-box'; |
| 7 | +import type dxDropDownBox from 'devextreme/ui/drop_down_box'; |
| 8 | + |
| 9 | +interface OrderItem { |
| 10 | + OrderNumber: number; |
| 11 | + Employee: string; |
| 12 | + StoreState: string; |
| 13 | + StoreCity: string; |
| 14 | + OrderDate: string; |
| 15 | + SaleAmount: number; |
| 16 | +} |
3 | 17 |
|
4 | 18 | @Component({ |
5 | 19 | selector: 'app-root', |
6 | 20 | templateUrl: './app.component.html', |
7 | 21 | styleUrls: ['./app.component.scss'], |
8 | 22 | }) |
9 | | -export class AppComponent { |
10 | | - title = 'Angular'; |
| 23 | +export class AppComponent implements AfterViewInit { |
| 24 | + @ViewChild('dropDownBox', { static: false }) dropDownBox!: DxDropDownBoxComponent; |
| 25 | + |
| 26 | + @ViewChild(DxDataGridComponent) dataGrid!: DxDataGridComponent<OrderItem, number>; |
| 27 | + |
| 28 | + dataSource: DataSource; |
| 29 | + |
| 30 | + dropDownBoxDataSource: DataSource; |
| 31 | + |
| 32 | + searchTimer: ReturnType<typeof setTimeout> | null = null; |
| 33 | + |
| 34 | + gridBoxValue: number[] = [35711]; |
| 35 | + |
| 36 | + gridBoxOpened = false; |
| 37 | + |
| 38 | + focusedRowIndex = 0; |
11 | 39 |
|
12 | | - counter = 0; |
| 40 | + focusedRowKey: number | null = null; |
| 41 | + |
| 42 | + private firstLoadCompleted = false; |
| 43 | + |
| 44 | + constructor() { |
| 45 | + this.dataSource = new DataSource({ |
| 46 | + store: this.makeAsyncDataSource(), |
| 47 | + searchExpr: ['StoreCity', 'StoreState', 'Employee'], |
| 48 | + } as any); |
| 49 | + this.dropDownBoxDataSource = new DataSource({ |
| 50 | + store: this.makeAsyncDataSource(), |
| 51 | + } as any); |
| 52 | + } |
| 53 | + |
| 54 | + ngAfterViewInit(): void { |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + private makeAsyncDataSource(): unknown { |
| 59 | + return AspNetData.createStore({ |
| 60 | + key: 'OrderNumber', |
| 61 | + loadUrl: 'https://js.devexpress.com/Demos/WidgetsGalleryDataService/api/orders', |
| 62 | + }); |
| 63 | + } |
13 | 64 |
|
14 | | - buttonText = 'Click count: 0'; |
| 65 | + gridBoxDisplayExpr = (item: OrderItem | null): string => ( |
| 66 | + item ? `${item.Employee}: ${item.StoreState} - ${item.StoreCity} <${item.OrderNumber}>` : '' |
| 67 | + ); |
| 68 | + |
| 69 | + onDropDownValueChanged(_e: DxDropDownBoxTypes.ValueChangedEvent): void { |
| 70 | + if (this.searchTimer) clearTimeout(this.searchTimer); |
| 71 | + this.gridBoxOpened = false; |
| 72 | + } |
| 73 | + |
| 74 | + private isSearchIncomplete(dropDownBox: any): boolean { |
| 75 | + const displayValue = dropDownBox.option('displayValue'); |
| 76 | + const text = dropDownBox.option('text'); |
| 77 | + const textValue = text?.length ? text : undefined; |
| 78 | + const displayFirst = displayValue?.length ? displayValue[0] : undefined; |
| 79 | + return textValue !== displayFirst; |
| 80 | + } |
| 81 | + |
| 82 | + onInput(e: DxDropDownBoxTypes.InputEvent): void { |
| 83 | + if (this.searchTimer) clearTimeout(this.searchTimer); |
| 84 | + this.searchTimer = setTimeout(() => { |
| 85 | + const text = e.component.option('text'); |
| 86 | + this.dataSource.searchValue(text ?? null); |
| 87 | + if (this.gridBoxOpened && this.isSearchIncomplete(e.component)) { |
| 88 | + this.dataSource.load().then((items: OrderItem[]) => { |
| 89 | + if (items.length > 0 && this.dataGrid?.instance) { |
| 90 | + this.focusedRowKey = items[0].OrderNumber; |
| 91 | + this.focusedRowIndex = 0; |
| 92 | + } |
| 93 | + }).catch(() => {}); |
| 94 | + } else { |
| 95 | + this.gridBoxOpened = true; |
| 96 | + } |
| 97 | + }, 500); |
| 98 | + } |
| 99 | + |
| 100 | + onOpened(e: DxDropDownBoxTypes.OpenedEvent): void { |
| 101 | + const ddbInstance = e.component as dxDropDownBox & { isKeyDown?: boolean }; |
| 102 | + if (ddbInstance.isKeyDown) { |
| 103 | + if (!this.dataGrid?.instance) return; |
| 104 | + const inst = this.dataGrid.instance; |
| 105 | + const contentReadyHandler = (args: DxDataGridTypes.ContentReadyEvent<OrderItem, number>): void => { |
| 106 | + const gridInstance = args.component; |
| 107 | + gridInstance.focus(); |
| 108 | + gridInstance.off('contentReady', contentReadyHandler as any); |
| 109 | + }; |
| 110 | + if (!this.firstLoadCompleted) { |
| 111 | + inst.on('contentReady', contentReadyHandler as any); |
| 112 | + } else { |
| 113 | + const optionChangedHandler = (args: DxDataGridTypes.OptionChangedEvent<OrderItem, number>): void => { |
| 114 | + const gridInstance = args.component; |
| 115 | + if (args.name === 'focusedRowKey' || args.name === 'focusedColumnIndex') { |
| 116 | + gridInstance.off('optionChanged', optionChangedHandler as any); |
| 117 | + gridInstance.focus(); |
| 118 | + } |
| 119 | + }; |
| 120 | + inst.on('optionChanged', optionChangedHandler as any); |
| 121 | + this.focusedRowIndex = 0; |
| 122 | + } |
| 123 | + ddbInstance.isKeyDown = false; |
| 124 | + } else if (this.firstLoadCompleted && this.isSearchIncomplete(ddbInstance)) { |
| 125 | + this.dataSource.load().then((items: OrderItem[]) => { |
| 126 | + if (items.length > 0) this.focusedRowKey = items[0].OrderNumber; |
| 127 | + ddbInstance.focus(); |
| 128 | + }).catch(() => {}); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + onClosed(e: DxDropDownBoxTypes.ClosedEvent): void { |
| 133 | + const ddbInstance = e.component; |
| 134 | + const searchValue = this.dataSource.searchValue(); |
| 135 | + if (this.isSearchIncomplete(ddbInstance)) { |
| 136 | + this.gridBoxValue = []; |
| 137 | + } |
| 138 | + if (searchValue) { |
| 139 | + this.dataSource.searchValue(null); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + onKeyDown(e: DxDataGridTypes.KeyDownEvent<OrderItem, number>): void { |
| 144 | + if (!e.event || e.event.keyCode !== 40) return; |
| 145 | + if (!this.gridBoxOpened) { |
| 146 | + if (this.dropDownBox?.instance) { |
| 147 | + (this.dropDownBox.instance as any).isKeyDown = true; |
| 148 | + } |
| 149 | + this.gridBoxOpened = true; |
| 150 | + } else if (this.dataGrid?.instance) { |
| 151 | + this.dataGrid.instance.focus(); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + dataGridContentReady(e: DxDataGridTypes.ContentReadyEvent<OrderItem, number>): void { |
| 156 | + if (!this.firstLoadCompleted) { |
| 157 | + this.firstLoadCompleted = true; |
| 158 | + this.dropDownBox.instance.focus(); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + dataGridKeyDown(e: DxDataGridTypes.KeyDownEvent<OrderItem, number>): void { |
| 163 | + if (e.event?.keyCode === 13 && this.focusedRowKey != null) { |
| 164 | + this.focusedRowIndex = (e.component as any).option('focusedRowIndex'); |
| 165 | + this.gridBoxValue = [this.focusedRowKey]; |
| 166 | + this.gridBoxOpened = false; |
| 167 | + } |
| 168 | + } |
15 | 169 |
|
16 | | - onClick(e: ClickEvent): void { |
17 | | - this.counter++; |
18 | | - this.buttonText = `Click count: ${this.counter}`; |
| 170 | + onDropDownBoxKeyDown(e: DxDropDownBoxTypes.KeyDownEvent): void { |
| 171 | + if (e.event?.keyCode !== 40) return; |
| 172 | + if (!this.gridBoxOpened && this.dropDownBox?.instance) { |
| 173 | + (this.dropDownBox.instance as any).isKeyDown = true; |
| 174 | + this.gridBoxOpened = true; |
| 175 | + } else if (this.dataGrid?.instance) { |
| 176 | + this.dataGrid.instance.focus(); |
| 177 | + } |
19 | 178 | } |
20 | 179 | } |
0 commit comments