TinyBase logoTinyBase β

RelationshipInHtmlTable

RelationshipInHtmlTable(
  this: void,
  internals: Brand<"ComponentInternals">,
  props: (
    RelationshipInHtmlTableProps &
    HtmlTableProps
  ),
): {
  $on?: (type: string, callback: (e: any) => void): () => void;
  $set?: (props: Partial<(
    RelationshipInHtmlTableProps &
    HtmlTableProps
  )>): void;
}
TypeDescription
thisvoid
internalsBrand<"ComponentInternals">
props( RelationshipInHtmlTableProps & HtmlTableProps )

The props passed to the component.

returns{ $on?: (type: string, callback: (e: any) => void): () => void; $set?: (props: Partial<( RelationshipInHtmlTableProps & HtmlTableProps )>): void; }

See the <RelationshipInHtmlTable /> (Svelte) demo for this component in action.

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

This component renders the two Table objects by iterating over their related 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 CustomCell type for more details.

Note the use of dotted 'tableId.cellId' string pairs when specifying custom rendering for the cells in this table, since Cells from both the relationship's 'local' and 'remote' Table objects can be rendered and need to be distinguished.

This component uses the useRowIds and useRemoteRowId hooks under the covers, which means that any changes to the structure of either Table resulting in a change to the relationship will cause a re-render.

You can use the headerRow and idColumn props to control whether labels and 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 Relationships object is provided. The RelationshipInHtmlTable component within it then renders the two Tables linked by a relationship in a <table> element with a CSS class. Note the dotted pairs that are used as column headings.

App.svelte
<script>
  import {Provider} from 'tinybase/ui-svelte';
  import {RelationshipInHtmlTable} from 'tinybase/ui-svelte-dom';

  export let relationships;
</script>

<Provider {relationships}>
  <RelationshipInHtmlTable
    relationshipId="petSpecies"
    className="relationship"
  />
</Provider>
import {flushSync, mount} from 'svelte';
import {createRelationships, createStore} from 'tinybase';
import App from './App.svelte';

const relationships = createRelationships(
  createStore()
    .setTable('pets', {fido: {species: 'dog'}, cujo: {species: 'dog'}})
    .setTable('species', {wolf: {price: 10}, dog: {price: 5}}),
).setRelationshipDefinition('petSpecies', 'pets', 'species', 'species');
const app = document.body.appendChild(document.createElement('div'));
flushSync(() => mount(App, {target: app, props: {relationships}}));
console.log(app.innerHTML);
// ->
`
<table class="relationship">
  <thead>
    <tr>
      <th>pets.Id</th>
      <th>species.Id</th>
      <th>pets.species</th>
      <th>species.price</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th title="fido">fido</th>
      <th title="dog">dog</th>
      <td>dog</td>
      <td>5</td>
    </tr>
    <tr>
      <th title="cujo">cujo</th>
      <th title="dog">dog</th>
      <td>dog</td>
      <td>5</td>
    </tr>
  </tbody>
</table>
`;

This example creates a Provider context into which a default Relationships object is provided. The RelationshipInHtmlTable component within it then renders the two Tables linked by a relationship with a custom component and a custom props callback for the species.price Cell. The header row at the top of the table and the Id column at the start of each row is removed.

FormattedCellView.svelte
<script>
  import {CellView} from 'tinybase/ui-svelte';

  export let tableId;
  export let rowId;
  export let cellId;
  export let store;
  export let bold = false;
</script>

{#if bold}<b>{rowId}</b>{:else}{rowId}{/if}:<CellView
  {tableId}
  {rowId}
  {cellId}
  {store}
/>
App.svelte
<script>
  import {Provider} from 'tinybase/ui-svelte';
  import {RelationshipInHtmlTable} from 'tinybase/ui-svelte-dom';
  import FormattedCellView from './FormattedCellView.svelte';

  export let relationships;

  const customCells = {
    'species.price': {
      component: FormattedCellView,
      getComponentProps: (rowId) => ({bold: rowId == 'dog'}),
    },
  };
</script>

<Provider {relationships}>
  <RelationshipInHtmlTable
    relationshipId="petSpecies"
    {customCells}
    idColumn={false}
    headerRow={false}
  />
</Provider>
import {flushSync, mount} from 'svelte';
import {createRelationships, createStore} from 'tinybase';
import App from './App.svelte';

const relationships = createRelationships(
  createStore()
    .setTable('pets', {fido: {species: 'dog'}, cujo: {species: 'wolf'}})
    .setTable('species', {wolf: {price: 10}, dog: {price: 5}}),
).setRelationshipDefinition('petSpecies', 'pets', 'species', 'species');
const app = document.body.appendChild(document.createElement('div'));
flushSync(() => mount(App, {target: app, props: {relationships}}));
console.log(app.innerHTML);
// ->
`
<table>
  <tbody>
    <tr>
      <td><b>dog</b>:5</td>
    </tr>
    <tr>
      <td>wolf:10</td>
    </tr>
  </tbody>
</table>
`;

Since

v4.1.0

element

The custom element version of the component. Only present if compiled with the customElement compiler option Read more.

z_$$bindings

Does not exist at runtime, for typing capabilities only. DO NOT USE Read more.