TinyBase logoTinyBase

ResultTableInHtmlTable

The ResultTableInHtmlTable component renders the contents of a single query's ResultTable in a Queries object as an HTML <table> element, and registers a listener so that any changes to that result will cause a re-render.

ResultTableInHtmlTable(props: ResultTableInHtmlTableProps & HtmlTableProps): ComponentReturnType
TypeDescription
propsResultTableInHtmlTableProps & HtmlTableProps

The props for this component.

returnsComponentReturnType

A rendering of the ResultTable in a <table> element.

See the <ResultTableInHtmlTable /> demo for this component in action.

The component's props identify which ResultTable to render based on query Id, and Queries object (which is either the default context Queries object, a named context Queries object, or by explicit reference).

This component renders a ResultTable by iterating over its Row objects. By default the Cells are in turn rendered with the CellView component, but you can override this behavior by providing a component for each Cell in the customCells prop. You can pass additional props to that custom component with the getComponentProps callback. See the ResultCustomCell type for more details.

This component uses the useRowIds hook under the covers, which means that any changes to the structure of the Table will cause a re-render.

You can use the headerRow and idColumn props to control whether the Ids appear in a <th> element at the top of the table, and the start of each row.

Examples

This example creates a Provider context into which a default Queries object is provided. The ResultTableInHtmlTable component within it then renders the ResultTable in a <table> element with a CSS class.

import {createQueries, createStore} from 'tinybase';
import {Provider} from 'tinybase/ui-react';
import React from 'react';
import {ResultTableInHtmlTable} from 'tinybase/ui-react-dom';
import {createRoot} from 'react-dom/client';

const App = ({queries}) => (
  <Provider queries={queries}>
    <Pane />
  </Provider>
);
const Pane = () => (
  <ResultTableInHtmlTable queryId="petColors" className="table" />
);

const queries = createQueries(
  createStore().setTable('pets', {
    fido: {species: 'dog', color: 'brown'},
    felix: {species: 'cat', color: 'black'},
  }),
).setQueryDefinition('petColors', 'pets', ({select}) => select('color'));
const app = document.createElement('div');
createRoot(app).render(<App queries={queries} />);
console.log(app.innerHTML);
// ->
`
<table class="table">
  <thead>
    <tr>
      <th>Id</th>
      <th>color</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>fido</th>
      <td>brown</td>
    </tr>
    <tr>
      <th>felix</th>
      <td>black</td>
    </tr>
  </tbody>
</table>
`;

This example creates a Provider context into which a default Queries object is provided. The ResultTableInHtmlTable component within it then renders the ResultTable with a custom component and a custom props callback for the color Cell. The header row at the top of the table and the Id column at the start of each row is removed.

import {Provider, ResultCellView} from 'tinybase/ui-react';
import {createQueries, createStore} from 'tinybase';
import React from 'react';
import {ResultTableInHtmlTable} from 'tinybase/ui-react-dom';
import {createRoot} from 'react-dom/client';

const App = ({queries}) => (
  <Provider queries={queries}>
    <Pane />
  </Provider>
);
const Pane = () => (
  <ResultTableInHtmlTable
    queryId="petColors"
    customCells={customCells}
    headerRow={false}
    idColumn={false}
  />
);

const FormattedResultCellView = ({queryId, rowId, cellId, bold}) => (
  <>
    {bold ? <b>{rowId}</b> : rowId}:
    <ResultCellView queryId={queryId} rowId={rowId} cellId={cellId} />
  </>
);
const customCells = {
  color: {
    component: FormattedResultCellView,
    getComponentProps: (rowId) => ({bold: rowId == 'fido'}),
  },
};

const queries = createQueries(
  createStore().setTable('pets', {
    fido: {species: 'dog', color: 'brown'},
    felix: {species: 'cat', color: 'black'},
  }),
).setQueryDefinition('petColors', 'pets', ({select}) => select('color'));
const app = document.createElement('div');
createRoot(app).render(<App queries={queries} />);
console.log(app.innerHTML);
// ->
`
<table>
  <tbody>
    <tr>
      <td><b>fido</b>:brown</td>
    </tr>
    <tr>
      <td>felix:black</td>
    </tr>
  </tbody>
</table>
`;

Since

v4.1.0