sorter-table-vis

API Documentation

Complete API reference for the Sorter Table Visualization library.

Table of Contents

Constructor

new sorterTable(data, columnNames, changed, options)

Creates a new sorterTable instance.

Parameters:

Returns: sorterTable instance

Example:

const table = new sorterTable(
  [{ id: 1, name: 'Alice' }],
  ['id', 'name'],
  (event) => console.log(event),
  { height: '600px' }
);

Data Management

updateData(newData, options)

Updates the table data. Returns a Promise that resolves when update is complete.

Parameters:

Returns: Promise<boolean>

Example:

await table.updateData(newData, {
  updateTypes: true,
  resetState: false
});

resetTable(useInitialData, options)

Resets the table to initial state or specified state.

Parameters:

Returns: void

Example:

table.resetTable(true, {
  resetSorting: false
});

preprocessData(data, columnNames)

Preprocesses data before use. Called automatically during construction.

Parameters:

Returns: Array<Object> - Preprocessed data

Selection

setSelectedData(selectedIndices)

Selects rows by their visible index in the table.

Parameters:

Returns: void

Example:

table.setSelectedData([0, 2, 5]); // Select first, third, and sixth visible rows

setSelectedDataByIds(ids, idPropertyName)

Selects rows by their ID property.

Parameters:

Returns: void

Example:

table.setSelectedDataByIds([1, 2, 3], 'userId');

getSelection()

Gets currently selected row indices.

Returns: Array<number> - Array of selected row indices

Example:

const selected = table.getSelection();
console.log(`${selected.length} rows selected`);

getSelectionRule()

Gets a human-readable description of the current selection.

Returns: string - Selection rule description

clearSelection()

Clears all row selections.

Returns: void

selectRow(rowElement)

Selects a specific row element (internal use, but can be called).

Parameters:

Returns: void

unselectRow(rowElement)

Unselects a specific row element.

Parameters:

Returns: void

Filtering

filter()

Applies the current selection as a filter. Only selected rows remain visible.

Returns: void

Example:

table.setSelectedData([0, 1, 2]);
table.filter(); // Table now shows only rows 0, 1, 2

applyCustomFilter(filterFunction, options)

Applies a custom filter function to the data.

Parameters:

Returns: void

Example:

table.applyCustomFilter((row) => {
  return row.age > 25 && row.salary > 50000;
}, {
  customRule: 'Age > 25 and Salary > 50000'
});

setFilteredDataById(ids, idPropertyName, options)

Filters table to show only rows with specified IDs.

Parameters:

Returns: void

Column Operations

setColumnType(columnName, type)

Sets the data type for a column. Types affect sorting and visualization.

Parameters:

Returns: void

Example:

table.setColumnType('birthDate', 'date');
table.setColumnType('category', 'ordinal');

getColumnType(data, column)

Gets the inferred type for a column.

Parameters:

Returns: string - Column type

getColumnValues(columnName, options)

Gets all values for a column with caching and sampling support.

Parameters:

Returns: Array<{value: any, index: number}> - Array of value objects

Example:

const values = table.getColumnValues('salary', { useSampling: false });
const salaries = values.map(v => v.value);

getColumnValuesArray(columnName)

Gets column values as a simple array (optimized).

Parameters:

Returns: Array<any> - Array of values

shiftCol(columnName, direction)

Moves a column left or right.

Parameters:

Returns: void

Example:

table.shiftCol('name', 'left');  // Move name column left
table.shiftCol('age', 'right');  // Move age column right

selectColumn(columnName)

Selects a column (highlights it).

Parameters:

Returns: void

Grouping & Aggregation

groupBy(groupColumns, aggregators)

Groups data by specified columns and applies aggregators.

Parameters:

Returns: boolean - Success status

Example:

table.groupBy(['category', 'region'], {
  sales: {
    fn: (values) => values.reduce((a, b) => a + b, 0),
    label: 'Total Sales'
  },
  count: {
    fn: (values) => values.length,
    label: 'Count'
  }
});

Default Aggregators:

ungroup()

Removes grouping and restores original data.

Returns: boolean - Success status

Example:

table.ungroup(); // Restore original ungrouped data

UI Methods

getNode()

Gets the root DOM node of the table. Call this to render the table.

Returns: HTMLElement - Container div element

Example:

const tableNode = table.getNode();
document.getElementById('container').appendChild(tableNode);

setContainerSize(options)

Updates the container dimensions.

Parameters:

Returns: void

Example:

table.setContainerSize({
  width: '800px',
  height: '600px'
});

rebuildTable()

Rebuilds the table DOM. Call after data changes.

Returns: void

Example:

table.updateData(newData);
table.rebuildTable(); // Usually called automatically

updateHistograms()

Updates histogram visualizations for all columns.

Returns: void

undo()

Undoes the last operation (sort, filter, group, etc.).

Returns: void

Example:

table.filter();
table.undo(); // Restore previous state

Event Callbacks

changed Callback

The callback function passed to the constructor is called whenever the table state changes.

Event Object Structure:

{
  type: string,        // Event type: 'sort', 'filter', 'groupBy', 'ungroup', 'undo', etc.
  dataInd?: Array,     // Current data indices
  columns?: Array,     // Column names
  sort?: Object,       // Current sort state
  // ... type-specific properties
}

Event Types:

Sort Event

{
  type: 'sort',
  column: 'columnName',
  direction: 'up' | 'down',
  dataInd: [0, 1, 2, ...],
  sort: { columnName: { how: 'up'|'down', priority: number } }
}

Filter Event

{
  type: 'filter',
  indeces: [0, 1, 2, ...],
  ids: [{ id: 1 }, { id: 2 }, ...],
  rule: 'Filter description'
}

GroupBy Event

{
  type: 'groupBy',
  groupByColumns: ['category', 'region'],
  aggregatedRows: 50,
  originalRows: 1000
}

Ungroup Event

{
  type: 'ungroup',
  restoredRows: 1000
}

Undo Event

{
  type: 'undo',
  action: 'sort' | 'filter' | 'groupBy' | 'state',
  dataInd: [0, 1, 2, ...],
  columns: ['col1', 'col2', ...],
  sort: { ... }
}

Example:

function onTableChange(event) {
  switch(event.type) {
    case 'sort':
      console.log(`Sorted by ${event.column}`);
      break;
    case 'filter':
      console.log(`Filtered to ${event.indeces.length} rows`);
      break;
    case 'groupBy':
      console.log(`Grouped into ${event.aggregatedRows} groups`);
      break;
  }
}

Types & Interfaces

Column Definition

interface ColumnDefinition {
  column: string;      // Column name (required)
  alias?: string;      // Display name
  unique?: boolean;    // Is column unique
  type?: string;       // Data type: 'string', 'number', 'date', 'boolean', 'ordinal'
}

Aggregator

interface Aggregator {
  fn: (values: Array<any>, context: {
    ordinalOrder?: Array,
    column: string
  }) => any;
  label?: string;      // Display label
}

Cell Renderer Function

type CellRenderer = (value: any, rowData: Object) => HTMLElement | Text;

Parameters:

Returns: HTMLElement or Text node

Example:

const renderer = (value, rowData) => {
  const div = document.createElement('div');
  div.textContent = value;
  div.style.color = value > 100 ? 'red' : 'green';
  return div;
};

Configuration Options

interface SorterTableOptions {
  // Container dimensions
  height?: string;                    // Default: '400px'
  width?: string;                     // Default: '100%'
  
  // Rendering
  rowsPerPage?: number;               // Default: 50
  useWindowing?: boolean;             // Default: true
  rowHeight?: number;                 // Default: 30
  bufferRows?: number;                // Default: 10
  
  // Performance
  useWorkers?: boolean;               // Default: true
  workerPoolSize?: number;            // Default: 2
  samplingThreshold?: number;         // Default: 50000
  maxSampleSize?: number;             // Default: 50000
  
  // Binning
  maxOrdinalBins?: number;            // Default: 12
  continuousBinMethod?: 'scott' | 'fd' | 'sturges';  // Default: 'scott'
  dateInterval?: string;              // Default: 'day'
  minBinSize?: number;                // Default: 5
  
  // Custom renderers
  cellRenderers?: Record<string, CellRenderer>;
  
  // UI
  showDefaultControls?: boolean;      // Default: true
  
  // Callbacks
  onNearEnd?: () => void;             // Called when scrolling near end
}

Performance Considerations

Large Datasets (>50k rows)

The library automatically uses sampling for large datasets:

Memory Management

Web Workers

Web Workers are used for histogram calculations:

Error Handling

The library includes comprehensive error handling:

Browser Compatibility