> For the complete documentation index, see [llms.txt](https://docs.carto.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.carto.com/carto-for-developers/reference/carto-widgets-reference/models/gettable.md).

# getTable

A Table model is a data model that represents a list of arbitrary data rows, with support for pagination and sorting. Suitable for displaying tables and lists.

## **Usage**

```typescript
const formula = await dataSource.getTable({
    columns: ['column_A','column_B'],
    sortBy: 'column_A',
    sortDirection: 'desc',
    sortByColumnType: 'number',
    limit: 10, // 10 rows per page
    offset: 30 // return the third page
    // + base options...
});
```

## **Options**

The `getTable` model inherits all options from the [base options](/carto-for-developers/reference/carto-widgets-reference/models.md#model-base-options), plus:

```typescript
export interface TableRequestOptions extends BaseRequestOptions {
  columns: string[];
  sortBy?: string;
  sortDirection?: 'asc' | 'desc';
  sortByColumnType?: 'number' | 'string' | 'date';
  limit?: number;
  offset?: number;
}
```

* **columns:** an array containing the names of the columns that will be requested for each row.
* **sortBy** (optional): the name of the column that will be used for sorting.
* **sortDirection** (optional): whether to sort the table results in ascending (`asc`) or descending (`desc`) order
* **sortByColumnType** (optional): the data type of the column that will be used for sorting: `number`, `string` or `date`.
* **limit** (optional): the number of rows for each page.
* **offset** (optional): the number of rows to skip, useful in combination with the limit to build pagination.

## **Response**

The response is composed of two items:

* `rows`: A list of rows, where each row is an object that uses key-value pairs for each column.
* `metadata`: An object containing additional information such as the `total` number of rows in the table, useful for pagination.

```typescript
type TableModelResponse = {
  rows: Record<string, number | string>[];
  totalCount: number;
};
/* example response 
  {
    rows: [
      { id: 1, name: "Point A", risk: 30, population: 50000 },
      { id: 2, name: "Point B", risk: 25, population: 45000 },
      { id: 3, name: "Point C", risk: 35, population: 60000 },
    ],
    totalCount: 3
  } 
*/    
```

The response can be then mapped to a table-like HTML element, a list, or a charting library.

<figure><img src="https://3029946802-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FybPdpmLltPkzGFvz7m8A%2Fuploads%2Fgit-blob-800e43d82168d7e1ea202ddadeb371683e8efc63%2FScreenshot%202024-10-07%20at%2020.23.22.png?alt=media" alt="" width="563"><figcaption></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.carto.com/carto-for-developers/reference/carto-widgets-reference/models/gettable.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
