Hey there coding mates!
Currently diving into Vue 3 and the composition API with setup.
I've been crafting a custom "Table" component to display... well, tables. It requires a prop named "data", which is an array of objects (representing rows) containing fields like title, data, and a render() function. With the render() function, my goal is to enable the rendering of custom HTML within any column as needed, be it multiple data points, personalized text, bespoke components, etc. However, I hit a roadblock when dealing with custom components.
Here's what I have so far:
Within the Table component, I iterate over <tr v-for>
, cycling through props.data to render the columns. If it encounters a render() function, it passes row data to that function for rendering purposes. Here's an example of a render() function:
render(row) => {
return `This is some random text and: <my-custom-component>${row.someData}</my-custom-component>`;
}
Below is a snippet of the Table component:
<tr v-for="group in tableData">
<template v-for="column in props.columns">
<td v-if="column.render">
*** here I want to render whats inside render() function, including custom component ***
</td>
<td v-else :style="{ width: column.width ?? 'auto' }" v-html="group[column.data]"></td>
</template>
</tr>
This part isn't functioning properly. I keep getting a Vue warning stating "Failed to resolve component". I attempted using a dynamic component in Vue:
<component is="myCustomComponent"></component>
Unfortunately, it ends up rendering pure HTML like this:
<mycustomcomponent></mycustomcomponent>
I've scoured the web for solutions but haven't come across one yet. While I saw examples involving the h() function, integrating it into my code has proven challenging.
Additionally, it's worth mentioning that I populate the table with data post fetching it from the API.
So, what am I overlooking? Is achieving this feat even feasible? Given my limited experience with Vue, it's highly likely that I'm missing some key concept.