---
title: "Model"
---
Defined in: [api/node/typescript/models.ts:90](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L90)

Model<T> is the interface for feeding dynamic data into
`.slint` views.

A model is organized like a table with rows of data. The
fields of the data type T behave like columns.

## Extended by

- [`ArrayModel`](/latest/docs/node/api/classes/arraymodel/)

## Type Parameters

### T

`T`

the type of the model's items.

### Example
As an example let's see the implementation of [ArrayModel](/latest/docs/node/api/classes/arraymodel/)

```js
export class ArrayModel<T> extends Model<T> {
   private a: Array<T>

  constructor(arr: Array<T>) {
       super();
       this.a = arr;
   }

   rowCount() {
       return this.a.length;
   }

   rowData(row: number) {
      return this.a[row];
   }

   setRowData(row: number, data: T) {
       this.a[row] = data;
       this.notifyRowDataChanged(row);
   }

   push(...values: T[]) {
       let size = this.a.length;
       Array.prototype.push.apply(this.a, values);
       this.notifyRowAdded(size, arguments.length);
   }

   remove(index: number, size: number) {
       let r = this.a.splice(index, size);
       this.notifyRowRemoved(index, size);
   }

   get length(): number {
       return this.a.length;
   }

   values(): IterableIterator<T> {
       return this.a.values();
   }

   entries(): IterableIterator<[number, T]> {
       return this.a.entries()
   }
}
```

## Implements

- `Iterable`\<`T`\>

## Methods

### \[iterator\]()

> **\[iterator\]**(): `Iterator`\<`T`\>

Defined in: [api/node/typescript/models.ts:195](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L195)

#### Returns

`Iterator`\<`T`\>

#### Implementation of

`Iterable.[iterator]`

***

### filter()

> **filter**(`filterFunction`): [`FilterModel`](/latest/docs/node/api/classes/filtermodel/)\<`T`\>

Defined in: [api/node/typescript/models.ts:118](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L118)

Returns a new Model that only contains the rows of this model for which
`filterFunction` returns true.

#### Parameters

##### filterFunction

(`data`) => `boolean`

returns true if a row should be visible.

#### Returns

[`FilterModel`](/latest/docs/node/api/classes/filtermodel/)\<`T`\>

a new [FilterModel](/latest/docs/node/api/classes/filtermodel/) that wraps the current model.

***

### insertRow()

> **insertRow**(`_index`, `_data`): `void`

Defined in: [api/node/typescript/models.ts:191](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L191)

Implementations of this function must add a row at the specified index, pushing all next
rows to the right.
Throws when the model rejects the modification; the default implementation
always throws.

#### Parameters

##### \_index

`number`

index of the row to insert.

##### \_data

`T`

new data item to store in a new row.

#### Returns

`void`

***

### map()

> **map**\<`U`\>(`mapFunction`): [`MapModel`](/latest/docs/node/api/classes/mapmodel/)\<`T`, `U`\>

Defined in: [api/node/typescript/models.ts:108](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L108)

Returns a new Model where all elements are mapped by the function `mapFunction`.

#### Type Parameters

##### U

`U`

#### Parameters

##### mapFunction

(`data`) => `U`

maps the data from T to U.

#### Returns

[`MapModel`](/latest/docs/node/api/classes/mapmodel/)\<`T`, `U`\>

a new [MapModel](/latest/docs/node/api/classes/mapmodel/) that wraps the current model.

***

### pushRow()

> **pushRow**(`data`): `void`

Defined in: [api/node/typescript/models.ts:169](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L169)

Adds a line to the model with the provided data.
Throws when the model rejects the modification.
The default implementation calls [Model.insertRow](/latest/docs/node/api/classes/model/#insertrow) with the row count.

#### Parameters

##### data

`T`

new data item to store in a new row.

#### Returns

`void`

***

### removeRow()

> **removeRow**(`_index`): `void`

Defined in: [api/node/typescript/models.ts:179](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L179)

Implementations of this function must remove the row at the specified index.
Throws when the model rejects the modification; the default implementation
always throws.

#### Parameters

##### \_index

`number`

index of the row to remove.

#### Returns

`void`

***

### reverse()

> **reverse**(): [`ReverseModel`](/latest/docs/node/api/classes/reversemodel/)\<`T`\>

Defined in: [api/node/typescript/models.ts:137](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L137)

Returns a new Model with the same rows as this model, in reverse order.

#### Returns

[`ReverseModel`](/latest/docs/node/api/classes/reversemodel/)\<`T`\>

a new [ReverseModel](/latest/docs/node/api/classes/reversemodel/) that wraps the current model.

***

### rowCount()

> `abstract` **rowCount**(): `number`

Defined in: [api/node/typescript/models.ts:144](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L144)

Implementations of this function must return the current number of rows.

#### Returns

`number`

***

### rowData()

> `abstract` **rowData**(`row`): `T` \| `undefined`

Defined in: [api/node/typescript/models.ts:150](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L150)

Implementations of this function must return the data at the specified row.

#### Parameters

##### row

`number`

index in range 0..(rowCount() - 1).

#### Returns

`T` \| `undefined`

undefined if row is out of range otherwise the data.

***

### setRowData()

> **setRowData**(`_row`, `_data`): `void`

Defined in: [api/node/typescript/models.ts:159](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L159)

Implementations of this function must store the provided data parameter
in the model at the specified row.
The default implementation throws, rejecting the modification.

#### Parameters

##### \_row

`number`

index in range 0..(rowCount() - 1).

##### \_data

`T`

new data item to store on the given row index

#### Returns

`void`

***

### sort()

> **sort**(`compareFunction`): [`SortModel`](/latest/docs/node/api/classes/sortmodel/)\<`T`\>

Defined in: [api/node/typescript/models.ts:129](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L129)

Returns a new Model with the same rows as this model, ordered according to
`compareFunction`.

#### Parameters

##### compareFunction

(`a`, `b`) => `number`

compares two rows the same way the callback passed to
                        Array.prototype.sort does.

#### Returns

[`SortModel`](/latest/docs/node/api/classes/sortmodel/)\<`T`\>

a new [SortModel](/latest/docs/node/api/classes/sortmodel/) that wraps the current model.