Utilizing nested v-for in Vue.js with lodash's groupBy function

When fetching data from a database, I am using lodash groupby to group my data like so:

var vm = this
axios.get(this.buildURL())
.then(function(response) {
    Vue.set(vm.$data, 'model', response.data.model)
    vm.groupData = _.groupBy(vm.model.data,'categories')
})
.catch(function(error) {
    console.log(error)
}) 

After grouping the data, I can see the result in the groupData variable when using the Vue extension in Google Chrome:

groupData: object
 > news: Array[2]
   > 0: Object
     name: "test article"
   > 1: Object
     name: "test article 2"
 > entertainment: Array[1]   
   > 0: Object
     name: "test article 3"

I attempted to use nested v-for to display the data:

<tr v-for="(items,index) in groupData">
    {{index}} // category name
     <tr v-if="items != null" v-for="item in items">
         <td>{{item.name}}</td> //article name
     </tr>
</tr>

However, I encountered an error in the console:

app.js:5428 [Vue warn]: Property or method "items" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option.

Why is this happening and where did I go wrong?

Update: In the HTML, I am trying to create row grouping in a table to display the data like this:

--------------------------------
name | created_at | updated_at |
--------------------------------
news //category name
--------------------------------
test | 1/1/2017   | 2/1/2017   |
--------------------------------
test2| 1/1/2017   | 2/1/2017   |
--------------------------------
entertainment //category name
--------------------------------
test| 1/1/2017   | 2/1/2017   |
--------------------------------

Answer №1

After a bit of experimenting with the HTML code (knowing that the issue was with my HTML), I was able to come up with this solution:

<tbody v-for="(items,index) in groupData">
    <tr class="active border-double" v-if="isGroup">
      <td :colspan="thead.length"> // I created an array for the headers to determine the colspan
        {{index}}
      </td>
    </tr>
    <tr v-for="item in items" :item="item">
        <td>{{item.name}}</td>
    </tr>
</tbody>

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

What are some ways I can safeguard my CSS from injected elements?

I have created an HTML widget that is inserted into various websites without the use of iframes. However, I am encountering issues where the CSS of some sites is affecting the appearance of my elements, such as text alignment, underlining, and spacing. Is ...

The option value in mat-autocomplete is not displaying correctly on IOS devices

When I click on the first option in the dropdown menu, it does not display the selected option in the field. However, when I select the second option, then the value of the first option appears, and when I choose the third option, the value of the second o ...

Using Ajax/jQuery in combination with Mongodb

My experience with Ajax/jQuery is fairly new. I am currently working on creating a sample HTML page using Ajax/jQuery to retrieve all customers and search for a customer by ID. Each customer has three variables: ID, firstName, and lastName. I am looking t ...

Guide on displaying several items from Laravel to Vue through the JavaScript filter method?

I need help transferring multiple objects from laravel to vue and filling my vue objects with information retrieved from the database. This is the data received from laravel: https://i.sstatic.net/2Hnk5.png Vue objects to be populated: storeName: {}, ...

Navigate to another HTML division using data-toggle

I have 2 HTML documents index.html and services.html In services.html, I have the following code: <a class="nav-link" data-bs-toggle="tab" href="#ds-tab">Data Science</a> <a class="nav-link" data-bs- ...

Steps for retrieving the selected text from a select element within a Vue component

Currently, I am utilizing Laravel 7 in conjunction with Vue.js 2. One of my current objectives is to capture the selected text when a user changes the chosen option. This snippet showcases my component structure: <template> <div class=" ...

Adding options to a select element using JavaScript dynamically

Is there a way to create a dynamic select list for year values using JavaScript? <form action="something"> some other fields <select id="year"> </select> </form> ...

Utilizing Filters to Dynamically Highlight and Unhighlight HTML in Angular

Currently, I am experimenting with creating a series of filters that can dynamically highlight and remove highlighting from generated HTML: Highlight filter: app.filter('highlight', function ($sce) { return function (str, termsToHighlight) ...

What is the proper way to utilize the toISOString() function in JavaScript?

My current code uses currentDate.toISOString() to output the date in this format: "2013-01-15T12:08:54.135Z". However, I actually need the date to be formatted like this: "2013-01-15T12:08:54-06:00". The "-06:00" represents the timezone. ...

Displaying dates in Meteor Handlebars using `{{ timestamp }}` braces

Looking to shorten the timestamp output in Meteor's Handlebar bracers. How can you convert {{ timestamp }} from Thu Jul 25 2013 19:33:19 GMT-0400 (Eastern Daylight Time) to just Jul 25? Attempted using {{ timestamp.toString('yyyy-MM-dd') } ...

Creating a semi-circle donut chart with Highcharts using the Highcharts-ng library

I have been working on integrating a Highcharts Semi-circle donut chart into my angular application using the Highcharts-ng directive. However, I seem to be facing an issue where the plotOptions section is being skipped entirely, leading to a full circle p ...

Incorporate image into Vue.js form along with other information

I have been successfully sending the content of multiple fields in a form to the Database. Now I am looking to add an additional field for uploading images/files and including it with the other form fields, but I am unsure about how to accomplish this task ...

Dynamic JavaScript Animation

Check out this code snippet I currently have. Notice how the text seems to jump when rerun? Give it a try and see for yourself. The big question is: How can this be fixed? $("#aboutUsText").delay(1000).fadeOut(1000) $("#aboutUsText").attr("MyState", "1") ...

Issue with React Material-UI drawer not functioning properly when invoking a child component function from the parent

Here is a piece of code I have for the App.js file: import React, { Component } from 'react'; import logo from './logo.svg'; import './App.css'; import Child from './child'; class App extends Component { render() ...

What could be causing the error 404 message to appear when trying to retrieve video data?

I'm having trouble displaying a video in mp4 format from the code's folder. When I attempt to fetch the video by clicking on the button, it shows an empty space instead of displaying the video. Here is an example of what the output looks like. T ...

Creating a specialized .toString() function for an array

When trying to create a custom .toString() method on the Array prototype, how can you access the actual array that the method is called on? This approach seems to work: Array.prototype.toString = () => 'custom'; "" + [1,2,3]; // 'custom ...

There is an issue with the functionality of the checkboxes within the loop in Vue.js

When utilizing v-for, I have multiple materials that need to be assigned with a checkbox. However, upon clicking the checkbox, a group of materials is instantly highlighted. This issue arises because the Quantity parameter is the same for numerous material ...

Is it possible to trigger a JavaScript function by manipulating the URL of an HTML page?

Imagine this scenario: You have an HTML page located at example.com/test.html that contains several pre-defined JavaScript functions, including one named play(). How can I add JavaScript to the URL in order to automatically trigger the play() function wh ...

In AngularJs, use ng repeat and ng switch to dynamically generate and display tables

I need help rendering a table with two columns using angularjs directives. <table> <tr ng-repeat="message in messages" > <td ng-switch-on="message.network" ng-switch when="twitter" ng-controller="TweetController"> <span> ...

Encountering difficulty when attempting to load a Vue component within a Blade file in Laravel 8

I successfully loaded Vue components in previous versions of Laravel by following these steps: In app.js located within resources/app.js, I declared the component like so: Vue.component('upload-menu', require('./components/UploadMenu.vue&ap ...