Show the bill of materials
In this tutorial you show the bill of materials (BOM) of the configuration: the parts that make up the configured product, with their quantities and prices. B2B shoppers and sales teams use it to check what exactly is being ordered. You add the ready-made <xpv-bom-breakdown> component and a table of your own.
The finished code is the bom.html page of the example apps.
Before you start: set up a bill of materials for the product in Expivi. Without one, the BOM is empty.
How the BOM works
useBomBreakdown(configurator) returns a BOM controller. It works like the price controller: after every change in the configuration, it asks the Expivi API for the new BOM. It has data, subscribe(), subscribeBusy(), formatPrice() and destroy().
The BOM has a list of parts. Each part has:
name,skuandidentifierquantityanduom(unit of measure, such aspcs)pricePerUnitandtotalPrice, which can benullthumbnailUrl, which can benullcomponents: the components the part consists of, each with aname,sku,quantityanduomattachments: files such as drawings, each with anameandurl
1. Register the BOM component
import { defineCustomElements as defineBomBreakdownElements } from '@expivi/sdk/bom-breakdown/components';
defineBomBreakdownElements();
2. Create one controller for the page
Create the controller once the configurator is READY, and give it to both the ready-made component and your own table.
- Vanilla JS
- Vue
- React
import '../shared/setup.js';
import { useBomBreakdown } from '@expivi/sdk';
import { defineCustomElements as defineBomBreakdownElements } from '@expivi/sdk/bom-breakdown/components';
import { mountBomTable } from '../components/bom-table.js';
import { createExpiviElement, mountProductConfigurator, startConfigurator } from '../shared/configurator.js';
import { renderShopHeader } from '../shared/navigation.js';
renderShopHeader('bom');
defineBomBreakdownElements();
startConfigurator({
onReady(configurator) {
mountProductConfigurator(configurator);
// One controller feeds both the custom table and the ready-made component.
const bomController = useBomBreakdown(configurator);
mountBomTable(document.querySelector('#bom-table'), bomController);
document
.querySelector('#bom-breakdown')
.append(createExpiviElement('xpv-bom-breakdown', { controller: bomController }));
},
});
<script setup lang="ts">
import { onBeforeUnmount } from 'vue';
import { useBomBreakdown, type ProductConfigurator } from '@expivi/sdk';
import BomTable from './BomTable.vue';
/** Rendered once the product is READY, so it can create its controller right away. */
const props = defineProps<{ configurator: ProductConfigurator }>();
// One controller feeds both the custom table and the ready-made component.
const bomController = useBomBreakdown(props.configurator);
onBeforeUnmount(() => bomController.destroy());
</script>
<template>
<section class="panel">
<h2 class="panel__title">Bill of materials (custom table)</h2>
<!-- A custom table, built with useBomBreakdown(). -->
<BomTable :controller="bomController" />
</section>
<section class="panel">
<h2 class="panel__title">Bill of materials (ready-made component)</h2>
<!-- The ready-made <xpv-bom-breakdown> component. -->
<xpv-bom-breakdown .controller="bomController" />
</section>
</template>
import { useBomBreakdown, type ProductConfigurator } from '@expivi/sdk';
import { useExpiviController } from '../hooks/useExpiviController';
import BomTable from './BomTable';
/** Rendered once the product is READY. */
export default function BomPanels({ configurator }: { configurator: ProductConfigurator }) {
// One controller feeds both the custom table and the ready-made component.
const bomController = useExpiviController(() => useBomBreakdown(configurator), [configurator]);
if (!bomController) return null;
return (
<>
<section className="panel">
<h2 className="panel__title">Bill of materials (custom table)</h2>
{/* A custom table, built with useBomBreakdown(). */}
<BomTable controller={bomController} />
</section>
<section className="panel">
<h2 className="panel__title">Bill of materials (ready-made component)</h2>
{/* The ready-made <xpv-bom-breakdown> component. */}
<xpv-bom-breakdown controller={bomController} />
</section>
</>
);
}
3. Build your own BOM table
- Vanilla JS
- Vue
- React
import { escapeHtml } from '../shared/escape-html.js';
/**
* Shows the bill of materials as a table and updates it whenever the configuration changes.
*
* @param {HTMLElement} container
* @param {import('@expivi/sdk').BomBreakdownController} bomController from useBomBreakdown(configurator)
* @returns {() => void} stops updating
*/
export function mountBomTable(container, bomController) {
function renderBom(bom) {
if (!bom?.parts.length) {
container.innerHTML = '<p class="panel__note">This configuration has no bill of materials.</p>';
return;
}
const rows = bom.parts
.map(
(part) => `
<tr>
<td>${escapeHtml(part.name ?? part.identifier)}</td>
<td>${escapeHtml(part.sku ?? '—')}</td>
<td class="bom-table__number">${part.quantity} ${escapeHtml(part.uom)}</td>
<td class="bom-table__number">${part.pricePerUnit === null ? '—' : bomController.formatPrice(part.pricePerUnit)}</td>
</tr>`,
)
.join('');
container.innerHTML = `
<table class="bom-table">
<thead>
<tr>
<th>Part</th>
<th>SKU</th>
<th class="bom-table__number">Quantity</th>
<th class="bom-table__number">Unit price</th>
</tr>
</thead>
<tbody>${rows}</tbody>
</table>`;
}
renderBom(bomController.data);
return bomController.subscribe(renderBom);
}
<script setup lang="ts">
import { onBeforeUnmount, ref } from 'vue';
import type { BOM, BomBreakdownController } from '@expivi/sdk';
/** Shows the bill of materials as a table and updates it whenever the configuration changes. */
const props = defineProps<{ controller: BomBreakdownController }>();
const bom = ref<BOM | null>(props.controller.data);
const unsubscribe = props.controller.subscribe((data) => (bom.value = data));
onBeforeUnmount(unsubscribe);
</script>
<template>
<p v-if="!bom?.parts.length" class="panel__note">This configuration has no bill of materials.</p>
<table v-else class="bom-table">
<thead>
<tr>
<th>Part</th>
<th>SKU</th>
<th class="bom-table__number">Quantity</th>
<th class="bom-table__number">Unit price</th>
</tr>
</thead>
<tbody>
<tr v-for="part in bom.parts" :key="part.identifier">
<td>{{ part.name ?? part.identifier }}</td>
<td>{{ part.sku ?? '—' }}</td>
<td class="bom-table__number">{{ part.quantity }} {{ part.uom }}</td>
<td class="bom-table__number">
{{ part.pricePerUnit === null ? '—' : controller.formatPrice(part.pricePerUnit) }}
</td>
</tr>
</tbody>
</table>
</template>
import { useEffect, useState } from 'react';
import type { BOM, BomBreakdownController } from '@expivi/sdk';
/** Shows the bill of materials as a table and updates it whenever the configuration changes. */
export default function BomTable({ controller }: { controller: BomBreakdownController }) {
const [bom, setBom] = useState<BOM | null>(controller.data);
useEffect(() => {
setBom(controller.data);
return controller.subscribe(setBom);
}, [controller]);
if (!bom?.parts.length) {
return <p className="panel__note">This configuration has no bill of materials.</p>;
}
return (
<table className="bom-table">
<thead>
<tr>
<th>Part</th>
<th>SKU</th>
<th className="bom-table__number">Quantity</th>
<th className="bom-table__number">Unit price</th>
</tr>
</thead>
<tbody>
{bom.parts.map((part) => (
<tr key={part.identifier}>
<td>{part.name ?? part.identifier}</td>
<td>{part.sku ?? '—'}</td>
<td className="bom-table__number">
{part.quantity} {part.uom}
</td>
<td className="bom-table__number">
{part.pricePerUnit === null ? '—' : controller.formatPrice(part.pricePerUnit)}
</td>
</tr>
))}
</tbody>
</table>
);
}
Result
Both the table and the ready-made component list the parts. Change an option that affects the parts, such as a size, and both update.
The ready-made component shows the parts as cards, with thumbnails, components and attachments. Build your own when you need other columns, other texts or, for example, an export to CSV.