Generate a responsive list with a pop-up feature under each item (using Vue.js)

Currently, I believe that Vue may not be necessary since most tasks can be done using JavaScript and CSS.

I am attempting to design a list layout as follows:

   [A] [B]
   [C] [D]

When an item is clicked, the information about that specific item should appear below it, indicated by an arrow.

   [A]    [B]        [A]    [B]
   [^ A-Info]        [C]    [D]
   [C]    [D]        [D-Info ^]

To achieve this, I plan on creating a list of objects, each containing header info and details. I will use v-for to create the elements like this:

<template v-for="item in items">
 <div class="header" @click="isVisible = true">
 <div class="details" v-show="isVisible">
</template>

However, when an item is clicked, the layout ends up looking like this:

[A]
[^       ]
[B]    [C]
[D]

This happens due to the order of elements in the DOM object. One solution could involve creating two header objects and one details section underneath them, populated with information from the clicked header. However, this seems like a complex solution. Is there a simpler way to approach this issue using CSS or another method?

Answer №1

To offer a complete solution, it would be helpful to have a better understanding of your data structure. However, based on your description, it seems like you are looking for a tabbed layout, possibly with multiple rows of tabs. One way to achieve this is by utilizing the v-show directive to display a details section only when it corresponds to the selected item. Here's an example:

HTML:

<div id="app">
  <table>
    <template v-for="row in rows">
      <tr>
        <th
          v-for="item in row"
          v-text="item.label"
          :class="{ selected: item.id === selectedId }"
          @click="selectItem(item.id)"        
        ></th>
      </tr>
      <tr>
        <td      
          :colspan="row.length"      
          v-for="item in row"
          v-show="item.id === selectedId"
          v-html="item.details"
        ></td>
      </tr>
    </template>
  </table>
</div>

JavaScript:

new Vue({
  el: "#app",
  data: {
    selectedId: 1,
    rows: [
      [
        {
          id: 1,
          label: "Item A",
          details: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras ultricies vitae massa vel cursus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam dictum finibus iaculis.'
        },
        {
          id: 2,
          label: "Item B",
          details: 'Mauris placerat, est in finibus consectetur, ligula urna imperdiet velit, vel interdum lacus turpis a nisi. Etiam et neque sed dui maximus posuere.'
        }
      ],
      [
        {
          id: 3,
          label: "Item C",
          details: 'Vivamus consectetur maximus diam tincidunt bibendum. Fusce quis euismod risus.'
        },
        {
          id: 4,
          label: "Item D",
          details: 'Nam convallis diam sit amet purus lacinia, in euismod sem porttitor. Sed magna quam, consequat in aliquet ut, faucibus nec massa.'
        }
      ]
    ]
  },
  methods: {
    selectItem(id) {
        this.selectedId = id;
    }
  }
})

CSS:

/* Styles */

table {
  padding: 20px;
}
th {
  padding: 10px 20px;
  cursor: pointer;
}
th.selected {
  background-color: lightgray;
}
td {
  padding: 20px;
  background-color: lightgray;
}

You can access a working jsFiddle here to experiment with the code.

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

How can I convert an Array into a Dictionary using JavaScript?

Is there a clever method (perhaps using a map function) to restructure my data object from this: [ {id: 1, from: "1/1/2021", to: "1/2/2022"}, {id: 2, from: "1/3/2021", to: "1/4/2022"}, {id: 1, from: "1/5/2 ...

Firing up asynchronous VuexFire data fetching using promises and error catching

Exploring the functioning vuex action called init which fetches a settings and an accounts collection: actions: { init: firestoreAction(({ bindFirestoreRef, commit }, payload) => { bindFirestoreRef( 'settings', fb.settings.doc(pay ...

Even though I have successfully compiled on Heroku, I am still encountering the dreaded Application Error

Looking for help with a simple express/node application to test Heroku? Check out my app.js: const express = require('express') const app = express() const port = '8080' || process.env.PORT; app.get('/', function (req, res) ...

Error in AngularX TS: Trying to invoke a type that does not have a callable signature

Encountering an issue while working on a component, specifically during ng serve/build process. Please note that this error is different from any console errors, despite what some may think. The expected outcome is for the code to successfully build and ru ...

Is it achievable for a modal popup to automatically move to a specified location?

I need assistance with... Is it feasible to display an external webpage within a modal window and have that page automatically scroll to a specific section (such as an anchor point)? ...

Difficulty redirecting Ajax call to a timed-out, CAS-protected server

Our website's login system utilizes CAS for single sign-on. The CAS server is operating the JASIG CAS server at , while our web server runs on Rails at . Due to security reasons, the Rails server has a relatively short session timeout, resulting in o ...

Resizing Your WordPress Header Logo

Hi there! I am struggling to resize the logo in the header of my website www.cookingvinyls.com. Despite my efforts, I can't seem to override the hardcoded version. I attempted to modify the width and height lines in header.php, but unfortunately, it ...

Exploring MongoDB through User Interface Requests

As part of a project to develop a minimalist browser-based GUI for MongoDB, an interesting question has arisen. How can we accurately display the current state of the database and ensure it is continuously updated? Specifically, what methods can be utiliz ...

"Enhance Your Website with qTip2 Feature to Load Multiple AJAX Sites Simult

I'm currently utilizing the qTip2 library for my project and I've implemented their AJAX retrieval functions following this example: http://jsfiddle.net/L6yq3/1861/. To enhance my query, I have modified their HTML to include multiple links. The ...

The power of PHP's echo function comes alive as it seamlessly connects

I am currently working on developing a product catalog using PHP. Below is the HTML flex-container code: <div class="products"> <?php echo file_get_contents("http://192.168.64.2/CodeWay/Pages/product.php"); ?> & ...

pictures on the blog entries

I'm looking to customize the look of images within a section. I want to add borders to the images, but since they are being added dynamically as content in blog posts, I can't reference them directly. Can you recommend a way for me to achieve thi ...

Executing a python script on the client side with the help of Vue.js

Because of restricted computation capabilities, I need to execute a python script on the user's browser. Currently, my website utilizes Vue.js for the frontend and Django for the backend. Do you have any suggestions on how I can specifically run thi ...

Using the PHP mail() function to send an email with an attachment

I have developed two scripts for this specific HTML code along with a corresponding PHP script. Below is the HTML markup: <html> <body> <form action="http://senindiaonline.in/admar/non-series-numbers-db.php" method="post" target="_bl ...

checkbox causing the button to appear empty

Due to the inability of a ngIf and ngFor to coexist, I added an ng-container to facilitate the loop. However, after making this change, nothing seems to be working as expected without any clear reason. Below is the code snippet that is causing trouble: Vi ...

js include exclude switch a category

Although this question has been asked before, I have a query on completely removing an "id" from an element. Let's consider the following scenario: <div id="page"> some content here </div> I want to eliminate the "id" attribute from the ...

A guide to using Angular to emphasize text based on specific conditions met

Currently, I am developing a testing application that requires users to choose radio type values for each question. The goal is to compare the selected answers with the correct answers stored in a JSON file. To achieve this, I have implemented an if-else ...

What is the solution to removing the bottom margin from the jumbotron in the bootstrap framework?

I am building a website for my high school robotics team and I am new to HTML and CSS (bootstrap). I want to get rid of the gap between my banner and navbar on my site. Is there a way to remove that space? Check out our website: www.robotichive3774.com & ...

Enhancing Angular Directives with Dynamic Templates upon Data Loading

I am facing an issue with a directive that is receiving data from an api call. While the directive itself functions properly, the problem seems to be occurring because the directive loads before the api call is complete. As a result, instead of the expecte ...

Accordion menu with smooth CSS3 transitions

I created a unique accordion menu to replace the select form control, and I am interested in using CSS3 transitions to give it a smooth expand and contract effect. Feel free to check out my work on jsfiddle: http://jsfiddle.net/hKsCD/4/ In order to achi ...

Trouble transferring $rootScope.currentUser between AngularJS profile and settings page

I am in the process of setting up a site using Angular, Express, Node, and Passport. Currently, I am configuring Angular to monitor the $rootScope.currentUser variable with the following code: app.run(function ($rootScope, $location, Auth) { // Watch ...