What steps do I need to take to create a dynamic rowspan within Vuetify?

I am currently working on creating a table that resembles the image provided in the link below.

https://i.sstatic.net/xWa1I.png

I understand that I can achieve this by using nested data and multiple iterations with the help of the template feature offered by Vuetify. More information on this is also available here.

Alternatively, I could create the table with normal data containing no more than two entries, similar to the example shown here.

However, the data I have been given consists of more than five columns with multiple instances of duplicated data.

Therefore, I am attempting to determine the necessary index and length for implementing rowspan in my table.

Is there a method to identify the index, eliminate duplicate entries, and consolidate them into one or incorporate them into a rowspan within this type of table structure?

View the sample code here.

Answer №1

In order to determine the rowspan for each item in a table, we need to calculate how many rows it should span based on where its value first appears. The resulting data structure could resemble the following:

const rows = [
  {val1: 'a', val1Rowspan: 3}, // Value 'a' appears first and spans 3 rows
  {val1: 'a'}, // Same value as above, no rowspan needed
  {val1: 'a'}, // Same value as above, no rowspan needed
  {val1: 'b', val1Rowspan: 1}, // Value 'b' appears first and spans 1 row
  {val1: 'c', val1Rowspan: 2}, // Value 'c' appears first and spans 2 rows
  {val1: 'c'}, // Same value as above, no rowspan needed
]

This logic can be implemented effectively using v-for in Vue.js:

<table>
  <tr v-for="row of rows">
    <td
      v-if="row.val1Rowspan"
      :rowspan="row.val1Rowspan"
    >{{ row.val1 }}</td>
  </tr>
</table>

const { createApp, ref } = Vue;

const App = { 
  template: `
    <table>
  <tr v-for="(row, index) of rows">
    <td>{{index}}</td>
    <td
      v-if="row.val1Rowspan"
      :rowspan="row.val1Rowspan"
    >{{ row.val1 }}</td>
  </tr>
</table>
  `,
  data() {
    return {
    rows:   [
      {val1: 'a', val1Rowspan: 3},
      {val1: 'a'},
      {val1: 'a'},
      {val1: 'b', val1Rowspan: 1},
      {val1: 'c', val1Rowspan: 2},
      {val1: 'c'},
    ]
    }  
  }
}
const app = createApp(App)
app.mount('#app')
td{
  border: 1px solid black;
  padding: 8px;
}
<div id="app"></div>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>

To calculate the rowspans, a nested loop can be utilized where the outer loop sets the current value, the inner loop tallies the elements with the same value, assigns that count as the rowspan, and then advances to the first element with the next value. This process needs to be repeated for each column requiring a rowspan.

Does this explanation resonate with you? Does this method suit your requirements?

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

Creating dynamic routes in express to enable flexible and customizable paths

Exploring the dynamic usage of paths in Express has been on my mind. Specifically, I have been employing lodash to locate a path in a separate file using regex methods. routes.js const json = require('./routes.json') const _ = require('l ...

Learn how to dynamically clear and update source data in jQuery autocomplete for enhanced functionality

Here is a snippet of my jQuery code. In this code, 'year' refers to the form input tag ID, and 'years' is an array variable containing all the year values. I have set this array as the source for autocomplete, which works fine. However, ...

The Bootstrap-vue table stops displaying details when the data is refreshed

I am utilizing a bootstrap-vue table that is connected to a computed property pulling data from my vuex store. Each row contains a show_details button that expands a second row, following the guidelines here: The issue arises when there are changes in th ...

Accessing information from Next.js API endpoint in a Next.js web application

Currently, I am in the process of developing a web application using Next.js APP Router for both the frontend and backend components. The frontend takes care of rendering the user interface, while the backend comprises API routes. I require some guidance o ...

I encountered an error while trying to synchronize my Redux state with the component state

Upon clicking the advanced sports search button, I need to display a drawer containing my API values. Currently, when mapping my Redux state with component state, an error is occurring stating "Actions must be plain objects. Use custom middleware for async ...

What methods can I use to minimize the duration of invoking the location.reload() function?

When I'm using window.location.reload() in my onClick() function, it's taking too long to reload. I tried modifying the reload call to window.location.reload(true) to prevent caching, but it's still slow. The issue seems to be with location. ...

Utilizing a JavaScript variable as an argument in Fluid URI ViewHelper: A step-by-step guide

I am trying to create an Ajax call from a Fluid template using the Fluid URI action ViewHelper. However, I am facing an issue where one of the arguments needs to be dynamically obtained from a Javascript variable. How can I ensure that the ViewHelper is re ...

What is the method for obtaining the dimensions of a shape drawn on Google Maps?

Is there a way to retrieve the dimensions of a shape drawn using the Google Maps API? I would like to obtain the measurements and include labels on top of the shape. Take a look at this image for reference The event argument in the overlay event does no ...

Passing parent props to child components in Vue.js - A comprehensive guide!

Trying to understand the process of passing a prop from parent to child components. If I include the prop attribute with the #id within the child component tag, like Image cid="488484-49544894-584854", it functions correctly. However, I am interested in u ...

Activate CSS element with a click

Is there a way to add a click event to a CSS class so that when it is clicked, it changes to a different class? I am specifically looking to change the character class on li items when they are clicked. Here is the code snippet: <!DOCTYPE html> <h ...

Tips for transforming a JSON response into an array with JavaScript

I received a JSON response from an API: [ { "obj_Id": 66, "obj_Nombre": "mnu_mantenimiento_de_unidades", "obj_Descripcion": "Menu de acceso a Mantenimiento de Unidades" }, { "obj_Id": 67, "ob ...

What is the best way to clear the content of a contenteditable element in React?

I have a scenario where I am rendering an array of items, each with a contenteditable field. MainComponent.js import { useState } from "react"; import Item from "./Item"; import "./styles.css"; export default function MainC ...

What is the best way to conceal the dt tag when the dd tag contains no value

Is there a way to hide the "Subject" if the "subject_title" field is empty? <dt class="col-sm-6 text-dark" >Subject</dt> <dd class="col-sm-6">{{$course_dtl->subject_title }}</dd> For example, I would li ...

Issue with finding module in TypeScript component import during Jest test

Despite having the correct path to my styled objects, I'm puzzled by the error message that has popped up: Error: Cannot find module '../../shared/models' from 'Astronaut.tsx' import { moonHoldings } from '../../shared/models ...

Notification system for managing recurring tasks

Situation I am currently working on an application where users are assigned daily tasks such as "wash the window, clean the floor," and so on. Each task has a specific recurrence interval and must be completed at least once within that time frame. For e ...

Having trouble with JQuery's .prop function not unchecking a checkbox?

I have been experimenting with different solutions in order to uncheck a checkbox when another checkbox is checked. Unfortunately, none of the methods I've tried seem to be working effectively... Currently, my code looks like this: $("#chkBox1").cli ...

Customizing Vuetify for a right-to-left style

I'm a beginner with vuetify and I'm looking to create an rtl v-text-field with a top-right caption. I haven't been able to achieve this using the inspector tool. Here is what I currently have: https://i.sstatic.net/GaNOh.png Any assistance ...

When attempting to invoke a JavaScript function on JSP, there seems to be no

I am currently developing a web application and delving into the intricacies of how Ajax functions within it. I am encountering an issue where nothing occurs when I attempt to select a category, and no errors are being reported. Within my JSP page: < ...

Guide on implementing router view in VUEJS3 for a google chrome extension within a production build

I've been working on creating a Chromium extension using VueJS3 and everything looks great locally. However, when I export it as a Chrome extension, only the first component in my App.js is displayed. What could be causing this issue? ❤︎ Here is ...

Controller and Directive both setting up isolated scope for a shared element

Having trouble with adding the ng-intro-options attribute to the HTML element. The Issue var App = angular.module('MyApp', ['infinite-scroll', 'angular-intro']); App.controller('MyController', ['$scope', ...