Feed row designs into Vue Bootstrap tables from the main component using slot functionality

I am working on a component named "List" that includes a Vue Boostrap table:

<template>
  <div>
    <b-table :items="items">
      <!--<template slot="createdAt" slot-scope="row"> usually vue boostrap table row templates live here-->
        <!--{{row.item.createdAt|dateFormatter}}-->
      <!--</template>-->
      <slot name="tableTemplates"></slot> <!-- but i want to pass the templates from my parent template -->
    </b-table>
  </div>
</template>

For this setup, I am passing the table items from another Component called "Orders". Additionally, I need to pass row templates to the b-table component within Vue Boostrap.

However, I am facing challenges with implementing this using slots as it involves a template inside another template:

<template>
  <div>
    <list :items="items">
      <template slot="tableTemplates">
        <!--templates inside templates do not work-->
        <template slot="id" slot-scope="row">
          <span v-b-tooltip.hover :title="row.item.id">{{row.item.id|uuidFormatter}}</span>
        </template>
        <template slot="createdAt" slot-scope="row">
          {{row.item.createdAt|dateFormatter}}
        </template>
        <template slot="customer" slot-scope="row">
          {{row.item.customer}}
        </template>
      </template>
    </list>
  </div>
</template>

Answer №1

If you want to modify IVO's answer, consider making your List component more versatile.
Here is a potential approach to achieve that:

List component template:

<template>
  <b-table striped hover :items="items">
    <template :slot="slot.field" slot-scope="row" v-for="slot in slots">
      <slot :name="slot.name" :tbl="row"></slot>
    </template>
  </b-table>
</template>

List component props:

  props: {
    items: {
      type: Array
    },
    slots: {
      type: Array
    }
  },

In any parent components using the List, you can define a templates data property containing an array of instructions for templates to be passed to the List component. In this example, an array of objects is used to pass dynamic slot names and table data fields.

Parent component templates data property:

  data() {
    return {
      ...
      templates: [
        {
          name: "templateName",
          field: "dataFieldName"
        },
        ...
      ]
    };
  },

Parent component template, utilizing List:

  <list :items="items" :slots="templates">
    <template slot="customSlotName" slot-scope="data">
        // Include custom template here...
    </template>
  </list>

This way, all custom template logic remains in the parent component (e.g., Order.vue) rather than in List.vue.

Check out the updated codesandbox for reference: https://codesandbox.io/s/244p9rx10n

Answer №2

Have you attempted this approach? Extract the scope from the table's slot and then pass it into your nested slot:

<template>
  <div>
    <b-table :items="items">
      <template slot-scope="row">
        <slot name="tableTemplates" :tbl-props="row"></slot>
      </template>
    </b-table>
  </div>
</template>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

developing a grid system with JavaScript

I'm looking for an API that can help me build a datagrid similar to the one found at the following link: ...

Using Backbone to Handle Different Data Formats

I have a unique text file containing date-time data in the format below: 2014-03-14T16:32 2014-03-15T13:04 2014-03-16T06:44 ... I want to use this static file as a read-only data source for my backbone collection. However, the current format is not suita ...

Here is the process to make a GET request to an API with input parameters using JQuery and JavaScript in an Asp.net view when a particular tag

Seeking assistance in displaying the count of orders on my view using a special tag. I have written a stored procedure in SQL that returns the order count based on the input type. To showcase all types of orders, I have designed multiple boxes on my form. ...

Attempting to remove certain selected elements by using jQuery

Struggling to grasp how to delete an element using jQuery. Currently working on a prototype shopping list application where adding items is smooth sailing, but removing checked items has become quite the challenge. Any insights or guidance? jQuery(docume ...

What advantages does leveraging GraphQL with React offer compared to using GraphQL with Vue, Ember, or Angular?

Curious if there are any advantages to combining GraphQL, created by Facebook, with React? Or is it better to use a different JavaScript framework like Vue, Angular, or Ember instead? ...

The prefixes for Ruby on Rails routes are not properly preprocessed in the .erb.js file

I'm currently working with Rails 4 and encountering an issue with the following file: // apps/assets/javascripts/products.js.erb var getColoursAndMaterialsData = function(onSuccess) { var fd = formdata(); $.post( '<%= data_products_ ...

How do browsers typically prioritize loading files into the cache?

Out of curiosity, I wonder if the categorization is determined by file names or byte code? It's possible that it varies across different browsers. Thank you! ...

What could be causing the malfunction of the .setAttribute() and .removeAttribute functions in my JavaScript program?

Below is a flashcard game I have developed: Once a user enters the correct answer in the text box, "Correct!" is expected to be displayed in bold green font. The CSS attributes are then supposed to be removed shortly after. Here is the HTML code for the i ...

Unusual happenings when adjusting camera settings in three.js

After creating a basic three.js application that displays a cube and includes buttons to set fixed camera positions, I encountered some unexpected behavior. You can view a demo of my code here: https://jsfiddle.net/ph0ropg7/9/ In my application, I have im ...

Angular UI-Router: Customizable template for dynamic content

My goal is to dynamically load a navbar based on the user, but I am facing difficulties accessing my $rootScope. $stateProvider /*Login*/ .state('login', { url: '/', data: {pageTitle: 'Sign in.'}, ...

Utilize dynamic Google Maps markers in Angular by incorporating HTTP requests

Struggling to find a solution for this issue, but it seems like it should be simple enough. Initially, I fetch my JSON data using the code snippet below: testApp.controller("cat0Controller", function($scope, $http){ var url = "../../../Data/JSONs/randomd ...

Implementing a function in ReactJS that is activated by multiple buttons

Apologies for the unclear title. My issue revolves around having three different button tags, each of which should send a unique argument to the function selectSupplier(). However, no matter which button is clicked, only the last value ("ultramar") is be ...

What is the best way to access query string parameters within NextJS middleware?

In the context of NextJS middleware, I have successfully obtained the nextUrl object from the request, which includes details like the pathname. However, I am now wondering how to extract query string parameters directly within the middleware. Although I c ...

What is the order of reflection in dynamic classes - are they added to the beginning or

Just a general question here: If dynamic classes are added to an element (e.g. through a jQuery-ui add-on), and the element already has classes, does the dynamically added class get appended or prepended to the list of classes? The reason for my question ...

Linking Java objects with Node.js variables

In this snippet of code, I am utilizing the 'java' module in node.js to create Java objects. Specifically, I am creating four Java objects and aiming to consolidate them as a single object by using the variable 'args' to store these Jav ...

I have successfully implemented an onChange function with its corresponding set of parameters. However, I now desire to extend its functionality by incorporating

I have an onchange function that triggers when the "pending" option is selected in a select dropdown menu. This function adds a predefined value to an input field. However, I also want this functionality to apply when the page loads. Currently, the "pendin ...

Best practices for handling errors with the async pipe

When it comes to async pipe error handling, is there a best practice that you follow? Should errors be handled in the service (even if it requires using global error handling) or is it more appropriate to handle them in the component? What approach do you ...

the value of text in an href tag and jquery

My question is about extracting text from an anchor tag <a href="javascript:cs();">Alberta</a> I am attempting to retrieve the text "Alberta" from this anchor tag using JavaScript or jQuery Here is my current code snippet: function cs() { ...

Real-time data updates not working in jQuery data table

I am currently dealing with a data table and using ajax to fetch data for display on my HTML DOM page. However, I have encountered an issue after implementing server-side processing for the data table. Even though I can successfully retrieve records in my ...

Order of AngularJS Scope.on and Scope.emit Invocation in a Filter

One of the challenges I am facing involves watching a value in one controller that is changed in another controller within my filters. The goal is to determine whether an emit should be triggered based on the updated value. Here's the current situatio ...