# JSBaseFoundset

## Overview

`JSBaseFoundSet` provides a unified interface for working with foundsets, exposing shared functionalities for regular and view foundsets. It supports key operations such as iteration, selection, and data source retrieval. The multi-select mode allows multiple records to be handled simultaneously. This abstraction ensures consistency across various foundset types.

## Features

`JSBaseFoundSet` allows iteration over records with the `forEach` method, which handles dynamic loading while respecting Servoy’s lazy-loading mechanism. Callback functions can interact with records while accommodating changes like inserts and deletions. Exceptions ensure stability when the foundset is modified during iteration.

The class provides methods to retrieve details about the data source and selected records. Methods like `getDataSource` return the underlying data source, while `getSelectedIndex` and `getSelectedIndexes` provide details about currently selected records. The size of the cached row identifiers can be retrieved using `getSize`.

## Selection Management

`JSBaseFoundSet` includes methods for managing record selection. Developers can set or retrieve the currently selected record index with `setSelectedIndex` and `getSelectedIndex`. For multi-select scenarios, `setSelectedIndexes` and `getSelectedIndexes` enable handling multiple selected records.

For more information, refer to the [FoundSet](https://docs.servoy.com/reference/servoycore/dev-api/database-manager/jsfoundset) documentation.

## Properties Summarized

| Type                                                                           | Name                        | Summary |
| ------------------------------------------------------------------------------ | --------------------------- | ------- |
| [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean) | [multiSelect](#multiselect) |         |

## Methods Summarized

| Type                                                                         | Name                                                          | Summary                                                                                                           |
| ---------------------------------------------------------------------------- | ------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------- |
| [Object](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/object) | [forEach(callback)](#foreach-callback)                        | Iterates over the records of a foundset taking into account inserts and deletes that may happen at the same time. |
| [Object](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/object) | [forEach(callback, thisObject)](#foreach-callback-thisobject) | Iterates over the records of a foundset taking into account inserts and deletes that may happen at the same time. |
| [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) | [getDataSource()](#getdatasource)                             |                                                                                                                   |
| [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) | [getName()](#getname)                                         |                                                                                                                   |
| [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number) | [getRecordIndex(record)](#getrecordindex-record)              | Get the record index.                                                                                             |
| [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number) | [getSelectedIndex()](#getselectedindex)                       | Get the current record index of the foundset.                                                                     |
| [Array](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/array)   | [getSelectedIndexes()](#getselectedindexes)                   | Get the indexes of the selected records.                                                                          |
| [Array](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/array)   | [getSelectedRecords()](#getselectedrecords)                   |                                                                                                                   |
| [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number) | [getSize()](#getsize)                                         |                                                                                                                   |
| void                                                                         | [setSelectedIndex(index)](#setselectedindex-index)            | Set the current record index.                                                                                     |
| void                                                                         | [setSelectedIndexes(indexes)](#setselectedindexes-indexes)    | Set the selected records indexes.                                                                                 |

## Properties Detailed

### multiSelect

**Type**\
[Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean) true if the foundset is in multi-select mode; false otherwise.

## Methods Detailed

### forEach(callback)

Iterates over the records of a foundset taking into account inserts and deletes that may happen at the same time.\
It will dynamically load all records in the foundset (using Servoy lazy loading mechanism). If callback function returns a non null value the traversal will be stopped and that value is returned.\
If no value is returned all records of the foundset will be traversed. Foundset modifications( like sort, omit...) cannot be performed in the callback function.\
If foundset is modified an exception will be thrown. This exception will also happen if a refresh happens because of a rollback call for records on this datasource when iterating.\
When an exception is thrown from the callback function, the iteration over the foundset will be stopped.

**Parameters**

* [Function](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/function) **callback** The callback function to be called for each loaded record in the foundset. Can receive three parameters: the record to be processed, the index of the record in the foundset, and the foundset that is traversed.

**Returns:** [Object](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/object) Object the return value of the callback

**Sample**

```js
foundset.forEach(function(record,recordIndex,foundset) {
 	//handle the record here
 });
```

### forEach(callback, thisObject)

Iterates over the records of a foundset taking into account inserts and deletes that may happen at the same time.\
It will dynamically load all records in the foundset (using Servoy lazy loading mechanism). If callback function returns a non null value the traversal will be stopped and that value is returned.\
If no value is returned all records of the foundset will be traversed. Foundset modifications( like sort, omit...) cannot be performed in the callback function.\
If foundset is modified an exception will be thrown. This exception will also happen if a refresh happens because of a rollback call for records on this datasource when iterating.\
When an exception is thrown from the callback function, the iteration over the foundset will be stopped.

**Parameters**

* [Function](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/function) **callback** The callback function to be called for each loaded record in the foundset. Can receive three parameters: the record to be processed, the index of the record in the foundset, and the foundset that is traversed.
* [Object](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/object) **thisObject** What the this object should be in the callback function (default it is the foundset)

**Returns:** [Object](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/object) Object the return value of the callback

**Sample**

```js
foundset.forEach(function(record,recordIndex,foundset) {
 	//handle the record here
 });
```

### getDataSource()

**Returns:** [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) the data source of the foundset as a string.

### getName()

**Returns:** [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) the name of the foundset.

### getRecordIndex(record)

Get the record index. Will return -1 if the record can't be found.

**Parameters**

* [JSBaseRecord](https://docs.servoy.com/reference/servoycore/dev-api/database-manager/jsbaserecord) **record** Record

**Returns:** [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number) int index.

**Sample**

```js
var index = foundset.getRecordIndex(record);
```

### getSelectedIndex()

Get the current record index of the foundset.

**Returns:** [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number) int current index (1-based)

**Sample**

```js
//gets the current record index in the current foundset
var current = foundset.getSelectedIndex();
//sets the next record in the foundset
foundset.setSelectedIndex(current+1);
```

### getSelectedIndexes()

Get the indexes of the selected records.\
When the foundset is in multiSelect mode (see property multiSelect), a selection can consist of more than one index.

**Returns:** [Array](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/array) Array current indexes (1-based)

**Sample**

```js
// modify selection to the first selected item and the following row only
var current = foundset.getSelectedIndexes();
if (current.length > 1)
{
	var newSelection = new Array();
	newSelection[0] = current[0]; // first current selection
	newSelection[1] = current[0] + 1; // and the next row
	foundset.setSelectedIndexes(newSelection);
}
```

### getSelectedRecords()

**Returns:** [Array](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/array) an array of currently selected records in the foundset.

### getSize()

**Returns:** [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number) the number of records in the foundset.

### setSelectedIndex(index)

Set the current record index.

**Parameters**

* [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number) **index** index to set (1-based)

**Returns:** void

**Sample**

```js
//gets the current record index in the current foundset
var current = foundset.getSelectedIndex();
//sets the next record in the foundset
foundset.setSelectedIndex(current+1);
```

### setSelectedIndexes(indexes)

Set the selected records indexes.

**Parameters**

* [Array](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/array) **indexes** An array with indexes to set.

**Returns:** void

**Sample**

```js
// modify selection to the first selected item and the following row only
var current = foundset.getSelectedIndexes();
if (current.length > 1)
{
	var newSelection = new Array();
	newSelection[0] = current[0]; // first current selection
	newSelection[1] = current[0] + 1; // and the next row
	foundset.setSelectedIndexes(newSelection);
}
```

***
