How can you organize an array into rows and columns for easier viewing?

Hopefully everything is clear. I am open to making changes if needed to address any important points.

In one of my components, the array items are displayed within cards as shown below:

<b-card-group deck class="mb-3">
    <b-card border-variant="danger" bg-variant="dark" text-variant="white" class="text-center" v-for="b in breakdownNumbers" :key="b"> {{b}}
    </b-card>
  </b-card-group>

The array itself is located in another component:

data() {
    return {
      bns: []
    }
  }
  methods: {
    addBns (bns) {
        for (var b of bns) {
          if(this.bns.indexOf(b) === -1) {
            this.bns.push(b)
          }
        }
      },

My current problem is that if there are 30 items added, all will be displayed in a single row causing the cards to shrink. My goal is to have only 5 cards displayed per row and then continue on the next row once the limit is reached.

I am looking for a simple solution to achieve this layout.

This is how it currently looks with 5 items added. When a user clicks New Breakdown, I want the new item to start on a new row below the initial 5 items in the bns[] array.

https://i.stack.imgur.com/IoEOn.jpg

After making some modifications, I now have the following code:

methods: {
    addBns (bns) {
        for (var b of bns) {
          if(this.bns.indexOf(b) === -1) {
            this.bns.push(b)
          }
        }
        var bns = _.chunk(bns,3)
        console.log(bns)
      },

The console displays the split but I am unsure about the next steps. The array is still sent to the display component like before:

<build-breakdown-select :buildNumber="buildNumber" :breakdownNumbers="bns" @breakdowns="addBns"/>

The actual display of the cards is handled within the <build-breakdown-select> component.

Answer №1

This method seems to offer a straightforward solution. Begin by adding the chunk module from lodash, which is extremely compact:

<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e28e8d8683918aa2d6ccd3d5ccd6">[email protected]</a>/chunk.js"></script>

Next, divide your array into chunks. For instance:

var items = ['a', 'b', 'c', 'd', 'e', 'f']
var items = _.chunk(items, 3)

The output of this operation will be an array structured like this:

var items = [['a', 'b', 'c'], ['d', 'e', 'f']]

That's it. You can now utilize it with the v-for directive, along with a secondary nested v-for directive. Here's an example:

<row v-for="row, ridx in items" :key="ridx">
  <cell v-for="cell, cidx in row" :key="cidx"></cell>
</row>

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

Enhance the functionality of the custom transaction form in NetSuite by incorporating new actions

I'm currently working on incorporating a new menu option into the "Actions" menu within a custom transaction form in NetSuite. While I can successfully see my selection in the Actions Menu on the form, I'm running into an issue with triggering th ...

A guide on incorporating Bootstrap 5 into Nuxt.js

Initially, my attempt to incorporate bootstrap-vue was unsuccessful as it only supports bootstrap 4 and vue2. Now, I am pursuing the integration of bootstrap 5 without making any modifications by following these steps: To begin, I will install the bootstr ...

Showing the ng-controller contents post executing AJAX request using the "as" method

My goal is to display the contents of ng-repeat after making an AJAX call using $http. <table ng-controller="TableController as tc"> <tr> <th>Date</th> <!-- other headers are here --> ...

Sending form data using javascript without refreshing the page

I have a wall/social system similar to Facebook where users can post statuses. I want to add the functionality for users to like, dislike, and comment on a status without the page reloading. How can I achieve this with the form below? if(empty($_GET[&apos ...

Error message: No instance of an app record ID was found in Vue.js Console

Occasionally, an unfamiliar error message pops up in the console of my Vue.js application and I'm unsure about its cause. I am currently running Vue 2.6.11 alongside oidc-client and a vue sidebar menu. Here are the details I can share: https://i.sst ...

Incorporate JavaScript to include a new class and attribute

Apologies for any language barriers.. I grasp that it involves using addClass and removeClass, but I am uncertain about how to write the terms. I need to ensure that if the screen resolution is less than 768 pixels, then the attributes id="dLabel" type="b ...

What is the best method for designing a filtering menu with a "Narrow By" option?

Looking to create a sidebar menu similar to the functionality on mcmaster.com. This specific feature allows users to efficiently filter products and toggle through different options dynamically. Upon selecting the "metric" option, the entire page adjusts t ...

Utilizing Smart Table for Data Binding with JSON Dataset

I need help binding a JSON file to a smart table. How can I use the loop function for iteration? The design of the smart table is displaying, but the data from the JSON file is not binding. Here is the JSON file: [ { "year": 2013, "id ...

Issue arises when attempting to submit multiple form fields with identical 'name' attributes, preventing the fields from being posted

Currently, I am facing a challenge with old HTML/JavaScript code. Some parts I have control over, while others are generated from an external source that I cannot manage. There is a form created dynamically with hidden fields. The form is produced using a ...

Retrieve information from a JSON file and dynamically showcase it within a REACT component

Hey there! I'm a newbie in the world of programming and currently working on creating a carousel using Bootstrap and React. My aim is to make the Carousel images dynamic by fetching data from a local .json file. However, my current implementation seem ...

Developing with Angular 1.4.8 and JavaScript involves the process of building a constructor function to inherit properties into a third object

DEVELOPER TOOLS Using Angular 1.4.8 and lodash QUERY: REVISIT To clarify my query: Create an object (articles) Apply a constructor Import the properties of a third object, but place it in proto folder to prevent cluttering the root with a large colle ...

Troubleshooting: Page Unable to Import/Execute Linked JavaScript on WebStorm with Node.js Backend

I've been following W3School's jQuery tutorial, but I'm encountering some issues with importing scripts to an HTML document hosted on a Node server in WebStorm. I have properly installed and enabled the jQuery libraries under Preferences &g ...

Add a parameter to the iframe that is dynamically loaded into the DOM after a user clicks on

I am attempting to automatically play a YouTube video within an iframe. The iframe is only loaded into the DOM when I activate a trigger element: a.colorbox.cboxElement How can I add the parameter ?autoplay=1 to the end of the iframe URL upon click, consi ...

Create a visual representation of an image by sketching a detailed line profile using HTML5's

I am attempting to create an intensity profile for an image, using the x-axis as the line's length on the image and the y-axis as the intensity values along the length of the line. How can I achieve this on an HTML5 canvas? I have tried the code below ...

Having trouble getting Firebase phone number authentication to work with Vue.js

I am currently in the process of developing a new Vue.js application using the Webpack template. Within this app, I have implemented a /sign-in route that displays a component named SignIn. To authenticate users, I am utilizing Firebase Phone Number authen ...

Using the @ Symbol in Javascript ES6 Module Imports

One of the folders in my node_modules directory is called @mymodule, and within it, there is another folder named 'insidefolder'. The path to this folder looks like this: node_modules/@mymodule/insidefolder When trying to import insidefolder us ...

Protractor is able to achieve successful test results without actually executing the tests

SYMPTOMS When running Protractor, the tests pass successfully, but the pages do not load. Instead, they appear blank with "data:text/html" in the address bar (refer to the screenshot). Although the tests show as passed, there are 0 assertions being made. ...

The attempt to run 'createElement' on the 'Document' has resulted in failure. The tag name inputted ('{{laravue.currentview }}') is not recognized as a valid name

I recently started using the Laravue library from https://github.com/laravue/laravue. However, when I tried to run it on my local machine, I encountered the following error: Uncaught DOMException: Failed to execute 'createElement' on 'D ...

What is the most effective way to use checkboxes to apply multiple filters to an array of objects?

Let me simplify the description. Within the App component, I fetch data from a JSON file and store it in the flightsList array. My goal is to filter this array based on checkboxes selected in the FlightOptions and Airlines components. The issue that I&a ...

Unique background image is assigned to each div sharing the same class

Seeking a way to assign unique random backgrounds to each div with the .item class. Initially attempted using PHP and CSS, for example: <?php $bg = array('bg1.jpg', 'bg2.jpg', 'bg3.jpg', 'bg4.jpg', 'bg5.jpg ...