LoadFunction
Copying and pasting? We've got you covered! You can find the full source code of this tutorial here.
Asynchronously Loading Data 🌐
Manually setting data in a bim-table may be ok in some situations. However, there are cases where the data must be fetch from an external resource, or you need to do some heavy processing to compute the data and want to display a loading skeleton to inform users about an on-going computation. You should be happy as we got you covered! All of that is possible in the bim-table component and you will learn how in this tutorial.
🖖 Initializing the Library and Setting the Table
As always, let's first initialize the UI library. Remember you only have to do it once in your entire app.
// You have to import from "@thatopen/ui"
import * as BUI from "../../../..";
BUI.Manager.init();
After the library has been initialized, create a table or get an existing one from the HTML document. In this case, let's create one programatically:
const table = document.createElement("bim-table");
When you load data asynchronously, is very likely you do it on demand. That means, by default the table won't have any data. You can inform end-users about an empty table using a slot. For it, let's create the corresponding element as follows:
const missingDataElement = BUI.Component.create(
() => BUI.html`
<!-- The most important thing is to have the slot attribute set as missing-data -->
<!-- Other than that, the element can hold anything inside -->
<!-- Be aware you can use this missing-data slot no matter if you are not loading data asynchronously -->
<div slot="missing-data"
style="display: flex; flex-direction: column; align-items: center; width: 8rem; margin: auto;">
<bim-label>No data loaded!</bim-label>
<bim-button @click=${() => table.loadData()} label="Load Data" style="width: 100%;"></bim-button>
</div>
`,
);
table.append(missingDataElement);
Basically what happens is the table detects if the data property is empty. If it is, then the element with slot="missing-data"
will be displayed. The element can hold anything, and will be shown/hidden based on the data array. In this case, we added a handy button inside the missing data slot element the user can click to load data. As you see, the button triggers the loadData method in the table; which triggers the load function you have set in the table. We haven't set any load function yet, so let's do it now!