diff --git a/packages/devextreme/js/__internal/grids/new/grid_core/data_controller/data_controller.ts b/packages/devextreme/js/__internal/grids/new/grid_core/data_controller/data_controller.ts index 343ebf5cf46..a53e2090ead 100644 --- a/packages/devextreme/js/__internal/grids/new/grid_core/data_controller/data_controller.ts +++ b/packages/devextreme/js/__internal/grids/new/grid_core/data_controller/data_controller.ts @@ -1,11 +1,12 @@ /* eslint-disable no-param-reassign */ /* eslint-disable spellcheck/spell-checker */ /* eslint-disable @typescript-eslint/no-non-null-assertion */ -import type { Subscribable } from '@ts/core/reactive/index'; +import type { SubsGets } from '@ts/core/reactive/index'; import { computed, effect, state, } from '@ts/core/reactive/index'; +import gridCoreUtils from '../../../grid_core/m_utils'; // import { EditingController } from '../editing/controller'; // import type { Change } from '../editing/types'; import { OptionsController } from '../options_controller/options_controller'; @@ -22,17 +23,33 @@ export class DataController { [this.dataSourceConfiguration, this.keyExpr], ); + public readonly cacheEnabled = this.options.oneWay('cacheEnabled'); + + public readonly pagingEnabled = this.options.twoWay('paging.enabled'); + public readonly pageIndex = this.options.twoWay('paging.pageIndex'); public readonly pageSize = this.options.twoWay('paging.pageSize'); + public readonly remoteFiltering = this.options.oneWay('remoteOperations.filtering'); + + public readonly remotePaging = this.options.oneWay('remoteOperations.paging'); + + public readonly remoteSorting = this.options.oneWay('remoteOperations.sorting'); + + public readonly remoteSummary = this.options.oneWay('remoteOperations.summary'); + + public readonly dateSerializationFormat = this.options.oneWay('dateSerializationFormat'); + + public readonly onDataErrorOccurred = this.options.oneWay('onDataErrorOccurred'); + private readonly _items = state([]); - public readonly items: Subscribable = this._items; + public readonly items: SubsGets = this._items; private readonly _totalCount = state(0); - public readonly totalCount: Subscribable = this._totalCount; + public readonly totalCount: SubsGets = this._totalCount; public readonly isLoading = state(false); @@ -124,4 +141,8 @@ export class DataController { public getDataKey(data: unknown): unknown { return this.dataSource.unreactive_get().store().keyOf(data); } + + public getCardIndexByKey(key: object): number { + return gridCoreUtils.getIndexByKey(key, this.items); + } } diff --git a/packages/devextreme/js/__internal/grids/new/grid_core/data_controller/options.ts b/packages/devextreme/js/__internal/grids/new/grid_core/data_controller/options.ts index 5a0d60b68ec..c316d9f15ef 100644 --- a/packages/devextreme/js/__internal/grids/new/grid_core/data_controller/options.ts +++ b/packages/devextreme/js/__internal/grids/new/grid_core/data_controller/options.ts @@ -1,22 +1,38 @@ import type { DataSourceLike } from '@js/data/data_source'; interface PagingOptions { + enabled?: boolean; pageSize?: number; - pageIndex?: number; } -export interface Options { - paging?: PagingOptions; +interface RemoteOperationsOptions { + filtering?: boolean; + paging?: boolean; + sorting?: boolean; + summary?: boolean; +} +export interface Options { + cacheEnabled?: boolean; dataSource?: DataSourceLike; - + dateSerializationFormat?: string; keyExpr?: string | string[]; + onDataErrorOccurred?: unknown; + paging?: PagingOptions; + remoteOperations?: RemoteOperationsOptions | boolean; } export const defaultOptions = { paging: { + enabled: true, pageSize: 6, pageIndex: 0, }, + remoteOperations: { + filtering: false, + paging: false, + sorting: false, + summary: false, + }, } satisfies Options; diff --git a/packages/devextreme/js/__internal/grids/new/grid_core/data_controller/public_methods.ts b/packages/devextreme/js/__internal/grids/new/grid_core/data_controller/public_methods.ts index a93586f9798..fa111dc3a67 100644 --- a/packages/devextreme/js/__internal/grids/new/grid_core/data_controller/public_methods.ts +++ b/packages/devextreme/js/__internal/grids/new/grid_core/data_controller/public_methods.ts @@ -6,11 +6,63 @@ import type DataSource from '@js/data/data_source'; import type { Constructor } from '../types'; import type { GridCoreNewBase } from '../widget'; import { DataController } from './data_controller'; +import { Deferred } from '@js/core/utils/deferred'; +import { FilterDescriptor } from '@js/data'; export function PublicMethods>(GridCore: T) { return class GridCoreWithDataController extends GridCore { public getDataSource(): DataSource { return this.diContext.get(DataController).dataSource.unreactive_get(); } + + public byKey(key: object): Promise | undefined { + const store = this.getDataSource().store(); + const cardIndex = this.diContext.get(DataController).getCardIndexByKey(key); + let result = undefined; + + if (!store) return; + + if (cardIndex >= 0) { + // @ts-expect-error + result = new Deferred().resolve(this.getDataSource().items()[cardIndex].data); + } + + return result || store.byKey(key); + } + + public getFilter(): FilterDescriptor | Array { + return this.getDataSource().filter(); + } + + public keyOf(obj: object) { + return this.diContext.get(DataController).getDataKey(obj); + } + + public pageCount(): number { + return this.diContext.get(DataController).pageCount.unreactive_get(); + } + + public pageSize(): number; + public pageSize(value: number):void; + public pageSize(value?: number):number | void { + if(value === undefined) { + return this.diContext.get(DataController).pageSize.unreactive_get(); + } + this.diContext.get(DataController).pageSize.update(value); + } + + public pageIndex(): number; + public pageIndex(newIndex: number): void; + public pageIndex(newIndex?: number): number | void { + if(newIndex === undefined) { + return this.diContext.get(DataController).pageIndex.unreactive_get(); + } + // TODO: Promise (jQuery or native) + return this.diContext.get(DataController).pageIndex.update(newIndex); + } + + public totalCount(): number { + return this.diContext.get(DataController).totalCount.unreactive_get(); + } }; }