# GC.Data.Table

## Content

# Class: Table

[GC](../modules/GC).[Data](../modules/GC.Data).Table

## Table of contents

### Constructors

- [constructor](GC.Data.Table#constructor)

### Properties

- [columns](GC.Data.Table#columns)
- [name](GC.Data.Table#name)
- [options](GC.Data.Table#options)
- [views](GC.Data.Table#views)

### Methods

- [addView](GC.Data.Table#addview)
- [clearIndexes](GC.Data.Table#clearindexes)
- [createIndexes](GC.Data.Table#createindexes)
- [dropIndexes](GC.Data.Table#dropindexes)
- [fetch](GC.Data.Table#fetch)
- [getIndexes](GC.Data.Table#getindexes)
- [refreshRemoteData](GC.Data.Table#refreshremotedata)
- [removeView](GC.Data.Table#removeview)
- [search](GC.Data.Table#search)

## Constructors

### <a id="constructor" name="constructor"></a> constructor

• **new Table**(`name`, `dataSourceOption`)

Represents the table.

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `name` | `string` | The table name. |
| `dataSourceOption` | [`IDataSourceOption`](../modules/GC.Data#idatasourceoption) | The data source of table. |

## Properties

### <a id="columns" name="columns"></a> columns

• **columns**: [`IColumnCollection`](../modules/GC.Data#icolumncollection)

Represents the default columns of the table, it is only available after table is fetched. The key is column name, the value is column information.

___

### <a id="name" name="name"></a> name

• **name**: `string`

Represents the name of the table.

___

### <a id="options" name="options"></a> options

• **options**: [`IDataSourceOption`](../modules/GC.Data#idatasourceoption)

Represents the data source options of the table.

___

### <a id="views" name="views"></a> views

• **views**: [`IViews`](../interfaces/GC.Data.IViews)

Represents the view collection of the table. The key is view name, the value is GC.Data.View instance.

## Methods

### <a id="addview" name="addview"></a> addView

▸ **addView**(`name`, `columnInfos?`, `includeDefaultColumns?`, `options?`): [`View`](GC.Data.View)

Adds a view, which host table is current table.

**`property`** {string} name - The unique name of the column.

**`property`** {string} [value] - The value of the column, could be a field name of table from database, or formula which uses the fields names.

**`property`** {string | string[]} [caption] - The caption of the column.

**`property`** {number | string} [width] - The width of the column, support number in pixel, or star size.

**`property`** {GC.Data.StyleOptions} [style] - The column style options.

**`property`** {(GC.Data.CellValueRuleOptions | GC.Data.SpecificTextRuleOptions | GC.Data.FormulaRuleOptions | GC.Data.DateOccurringRuleOptions | GC.Data.Top10RuleOptions | GC.Data.UniqueRuleOptions | GC.Data.DuplicateRuleOptions | GC.Data.AverageRuleOptions | GC.Data.TwoScaleRuleOptions | GC.Data.ThreeScaleRuleOptions | GC.Data.DataBarRuleOptions | GC.Data.IconSetRuleOptions | GC.Data.SparklineRuleOptions)[]} [conditionalFormats] - The conditional rules array.

**`property`** {GC.Data.NumberValidatorOptions | GC.Data.DateValidatorOptions | GC.Data.TimeValidatorOptions | GC.Data.TextLengthValidatorOptions | GC.Data.FormulaValidatorOptions | GC.Data.FormulaListValidatorOptions | GC.Data.ListValidatorOptions} [validators] - The default data validator.

**`property`** {boolean} [isPrimaryKey] - Mark the column as primary key column.

**`property`** {boolean} [readonly] - Mark the column is readonly.

**`property`** {boolean} [required] - Mark the column is required when insert a new row.

**`property`** {Object} [defaultValue] - Provide the default value when insert a new row, could be a const or a formula.

**`property`** {GC.Data.HeaderStyleOptions} [style] - The column header style options.

**`example`**
```javascript
// Add a view by string array columns
productTable.addView("productView", [
    "id", "name", "reorderLevel", "unitPrice", "unitsInStock", "unitsOnOrder"
]);

// Add a view by customized columns
productTable.addView("productView", [{
    value: "id",
    caption: "ID",
    isPrimaryKey: true
}, {
    value: "name",
    caption: "NAME",
    required: true
}, {
    value: "quantityPerUnit",
    caption: "QUANTITY PER UNIT"
}, {
    value: "unitPrice",
    caption: "UNIT PRICE"
}, {
    value: "unitsInStock",
    caption: "UNITS IN STOCK",
    readonly: true
}, {
    value: "unitsOnOrder",
    caption: "UNITS ON ORDER"
}, {
    value: "reorderLevel",
    caption: "REORDER LEVEL"
}, {
    value: "discontinued",
    caption: "DISCONTINUED",
    defaultValue: false
});

// Add a view with relationship columns
var supplierRelationship = dataManager.addRelationship(productTable, "supplierId", "supplier", supplierTable, "id", "products");
productTable.addView("productWithSupplierView", [{
    value: "id",
    caption: "ID"
}, {
    value: "name",
    caption: "NAME"
}, {
    value: "supplier.companyName", // relationship
    caption: "SUPPLIER NAME"
}, {
    value: "supplier.contactName", // relationship
    caption: "SUPPLIER CONTACT NAME"
}, {
    value: "supplier.contactTitle", // relationship
    caption: "SUPPLIER CONTACT TITLE"
});

// Add a view with calc field columns
var supplierRelationship = dataManager.addRelationship(productTable, "supplierId", "supplier", supplierTable, "id", "products");
productTable.addView("productWithSupplierView", [{
    value: "id",
    caption: "ID"
}, {
    value: "name",
    caption: "NAME"
}, {
    caption: "TOTAL PRICE",
    value: "=(unitsInStock + unitsOnOrder) * unitPrice"
}, {
    caption: "SUPPLIER'S INFO",
    value: "=CONCAT(supplierTable.companyName, ', ', supplierTable.contactName)"
});

```

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `name` | `string` | The view name. |
| `columnInfos?` | `string`[] \| [`IColumn`](../modules/GC.Data#icolumn)[] | The column information, each column includes the following properties. |
| `includeDefaultColumns?` | `boolean` | Whether to include current table's default columns when column information are empty. Its default value is true. |
| `options?` | [`ViewOptions`](../modules/GC.Data#viewoptions) | The view options. |

#### Returns

[`View`](GC.Data.View)

Returns the view.

___

### <a id="clearindexes" name="clearindexes"></a> clearIndexes

▸ **clearIndexes**(): `void`

Clear all the indexed fields.

**`example`**
```javascript
// Clear all the indexed fields.
table.clearIndexes();
```

#### Returns

`void`

___

### <a id="createindexes" name="createindexes"></a> createIndexes

▸ **createIndexes**(`fields`): `void`

Create index for the fields.

**`example`**
```javascript
// Create indexes.
table.createIndexes(["name", "country", "project"]);
```

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `fields` | `string`[] | The index fields. |

#### Returns

`void`

___

### <a id="dropindexes" name="dropindexes"></a> dropIndexes

▸ **dropIndexes**(`fields`): `void`

Drop indexed fields.

**`example`**
```javascript
// Drop indexed fields.
table.dropIndexes(["name", "country", "project"]);
```

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `fields` | `string`[] | The indexed fields. |

#### Returns

`void`

___

### <a id="fetch" name="fetch"></a> fetch

▸ **fetch**(`reload?`): `Promise`<`any`\>

Requests the table data from local data source or remote data source by the data source option.

**`example`**
```javascript
// Use fetched data to build a tablesheet
productTable.fetch().then(function(data) {
    var productView = productTable.addView("productView");
    var tableSheet = spread.addSheetTab(0, "productTableSheet", GC.Spread.Sheets.SheetType.tableSheet);
    tableSheet.setDataView(productView);
});
```

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `reload?` | `boolean` | Whether to reload the data from server side forcibly. |

#### Returns

`Promise`<`any`\>

The resolving Promise thenable. You could get the data in Promise.then().

___

### <a id="getindexes" name="getindexes"></a> getIndexes

▸ **getIndexes**(): `string`[]

Get all the indexed fields.

**`example`**
```javascript
// Indexed fields exist.
table.getIndexes(); // returns ["name", "country", "project"]
// No indexed field.
table.getIndexes(); // returns []
```

#### Returns

`string`[]

The indexed fields.

___

### <a id="refreshremotedata" name="refreshremotedata"></a> refreshRemoteData

▸ **refreshRemoteData**(): `Promise`<`any`\>

Refreshes the remote data of the data manager table.
This method forces a reload of the underlying remote data source.
It uses the same reload flow as `fetch(true)`, but marks the operation as originating from `refreshRemoteData` for listeners or host integrations.

**`example`**
```javascript
const dm = spread.dataManager();
const products = dm.tables.products;
await products.refreshRemoteData();
```

#### Returns

`Promise`<`any`\>

A promise that resolves when the remote data refresh is complete.

___

### <a id="removeview" name="removeview"></a> removeView

▸ **removeView**(`name`): `void`

Removes a view.

**`example`**
```javascript
// Remove a view by name
dataManager.removeView("productView");
```

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `name` | `string` | The name of the view to be removed. |

#### Returns

`void`

___

### <a id="search" name="search"></a> search

▸ **search**(`value`, `field`): `any`[]

Search the records with search value, returns all the exact matching records.

**`example`**
```javascript
// Search value and succeed
table.search("SpreadJS", "group"); // returns [ {id: 1, project: "DataManager", group: "SpreadJS"}, {id: 3, project: "TableSheet", group: "SpreadJS"} ]
// Search value and failed
table.search("WPS", "project"); // returns []
```

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `value` | `any` | The search value. Always convert the non-string to string type. |
| `field` | `string` | The target field. |

#### Returns

`any`[]

The all matching records.
