Options
All
  • Public
  • Public/Protected
  • All
Menu

Class ArrayDataProvider

ArrayDataProvider implements a data provider based on a data array.

The allModels property contains all data models that may be sorted and/or paginated. ArrayDataProvider will provide the data after sorting and/or pagination. You may configure the [[sort]] and pagination properties to customize the sorting and pagination behaviors.

Elements in the allModels array may be either objects (e.g. model objects) or associative arrays (e.g. query results of DAO). Make sure to set the key property to the name of the field that uniquely identifies a data record or false if you do not have such a field.

Compared to ActiveDataProvider, ArrayDataProvider could be less efficient because it needs to have allModels ready.

ArrayDataProvider may be used in the following way:

provider = new ArrayDataProvider({
allModels: Post.findAll({ where: { companyId: 123 } }),
sort: [
attributes: ['id', 'username', 'email'],
],
pagination: {
pageSize: 10,
},
});
// get the posts in the current page
posts = provider.getModels();

Note: if you want to use the sorting feature, you must configure the [[sort]] property so that the provider knows which columns can be sorted.

For more details and usage information on ArrayDataProvider, see the guide article on data providers.

author

Mahesh S Warrier https://github.com/codespede

Hierarchy

Index

Constructors

Properties

allModels: Model[]

The data that is not paginated or sorted. When pagination is enabled, this property usually contains more elements than [[models]]. The array elements must use zero-based integer keys.

id: string

An ID that uniquely identifies the data provider among all data providers. Generated automatically the following way in case it is not set:

  • First data provider ID is empty.
  • Second and all subsequent data provider IDs are: "dp-1", "dp-2", etc.
initialization: Promise<void>

A Promise which resolves when this Component has been successfully initialized.

key: string | CallableFunction

The column that is used as the key of the data models. This can be either a column name, or a callback that returns the key value of a given data model. If this is not set, the index of the [[models]] array will be used.

see

getKeys

modelClass: Model

Model Class which can be used for resolving Attribute Labels, Hints etc.

totalCountPromise: Promise<number>

Promise which resolves to the number matching records for the current query.

Methods

  • getCount(): number
  • getKeys(): void | string[]
  • Returns the key values associated with the data models.

    Returns void | string[]

    the list of key values corresponding to [[models]]. Each data model in [[models]] is uniquely identified by the corresponding key value in this array.

  • getModels(): Promise<Model[]>
  • getPagination(): any
  • getSort(): any
  • getTotalCount(): Promise<number>
  • init(): Promise<void>
  • Initializes the object. This method is invoked at the end of the constructor after the object is initialized with the given configuration.

    Returns Promise<void>

  • prepare(forcePrepare?: boolean): Promise<void>
  • Prepares the data models and keys.

    This method will prepare the data models and keys that can be retrieved via getModels and getKeys.

    This method will be implicitly called by getModels and getKeys if it has not been called before.

    Parameters

    • forcePrepare: boolean = false

      whether to force data preparation even if it has been done before.

    Returns Promise<void>

  • prepareKeys(models: Model[]): string[]
  • prepareModels(): Promise<Model[]>
  • prepareTotalCount(): Promise<number>
  • refresh(): void
  • setModels(models: Model[]): void
  • setPagination(value: any): void
  • Sets the pagination for this data provider.

    Parameters

    • value: any

      the pagination to be used by this data provider. This can be one of the following:

      • a configuration array for creating the pagination object. The "class" element defaults to Pagination
      • an instance of Pagination or its subclass
      • false, if pagination needs to be disabled.

    Returns void

  • setSort(value: any): void
  • Sets the sort definition for this data provider.

    Parameters

    • value: any

      the sort definition to be used by this data provider. This can be one of the following:

      • a configuration array for creating the sort definition object. The "class" element defaults to 'yii\data\Sort'
      • an instance of Sort or its subclass
      • false, if sorting needs to be disabled.

    Returns void

  • setTotalCount(value: number): void
  • Sorts the data models according to the given sort definition.

    Parameters

    • models: Model[]

      the models to be sorted

    • sort: Sort

      the sort definition

    Returns Model[]

    the sorted data models

Generated using TypeDoc