Building a table in Quasar by utilizing nested objects

I am currently facing an issue while using Quasar to create Q-Tables. I am struggling with incorporating nested objects with dynamic key names. Below is the content of my table:

      data: [
        {
          'FrozenYogurt' : {
            'topping': 'strawberry'
          }
        },
        {
          'IceCreamSandwich' : {
            'baseFlavor': 'chocolate'
          }
        },
        {
          'CreamPuff' : {
            'sourceBakery': 'Starbucks'
         }
      ]

Here are my columns:

      columns: [
        {
          name: 'key',
          required: true,
          label: 'Property',
          align: 'left',
          field: row => row.name,
          format: val => `${val}`,
          sortable: true
        },
        { name: 'key.key', align: 'center', label: 'Property', field: 'key', sortable: true },
        { name: 'key.value', label: 'Value', field: 'key.value', sortable: true, style: 'width: 10px' },
      ],

In this scenario, my desired table structure should be as follows:

Product          Property      Value

FrozenYogurt     topping       strawberry
IceCreamSandwich baseFlavor    chocolate
CreamPuff        sourceBakery  Starbucks

Attached is my vue file for reference:

    <q-table
      :data="data"
      :columns="columns"
      row-key="key"
      binary-state-sort
    >
      <template v-slot:body="props">
        <q-tr :props="props">
          <q-td key="key" :props="props">
            {{ props.key }}
            <q-popup-edit v-model="props.key">
              <q-input v-model="props.row.key" dense autofocus counter></q-input>
            </q-popup-edit>
          </q-td>
          <q-td key="key.key" :props="props">
            {{ props.key.key }}
            <q-popup-edit v-model="props.key.key" title="Update Property" buttons>
              <q-input v-model="props.key.key" dense autofocus></q-input>
            </q-popup-edit>
          </q-td>
          <q-td key="key.value" :props="props">
            <div class="text-pre-wrap">{{ props.key.value }}</div>
            <q-popup-edit v-model="props.key.value">
              <q-input type="textarea" v-model="props.key.value" dense autofocus></q-input>
            </q-popup-edit>
          </q-td>
        </q-tr>
      </template>
    </q-table>

While my vue file may appear incorrect at first glance, I am unsure of how to access the object key names and values properly.

Is there a solution to this issue?

Thank you!

Answer №1

To display the data in a specific format, you can simply create a computed property that organizes the data accordingly.

computed:{

  getData(){

    return this.data.map(function(item){
      console.log(item)
      return {name:Object.keys(item)[0],Property:Object.keys(item[Object.keys(item)[0]])[0],Value:Object.values(item[Object.keys(item)[0]])[0]}
    })
  }
} 

View the functioning code on Codepen - https://codepen.io/Pratik__007/pen/RwPQgNr?editors=1010

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

Steps to trigger a JavaScript popup from an iframe window to the parent window

Currently I am developing a web application using JSP with JavaScript. Within the parent window, an iframe is being utilized. The issue arises when a button in the iframe page is clicked, as it opens a popup window within the iframe itself. However, my obj ...

Moving the layout container towards the left: a quick guide

I am currently attempting to display the legend contents in a horizontal alignment within the layout container. The issue is that while the layout containing the legend aligns horizontally as desired, it extends beyond the screen border. I do not want the ...

How can I make a drop-down field in AngularJS show the current value after populating options in a select control using ng-options?

This conversation centers around an app that can be visualized as, https://i.stack.imgur.com/Y1Nvv.png When a user clicks on one of the stories on the left side, the corresponding content is displayed on the right-hand side. Each story includes a title ...

Guide to effectively utilizing jQuery Deferred within a loop

I have been working on a function that needs to loop through a dataset, updating values as it goes along using data from an asynchronous function. It is crucial for me to know when this function finishes running the loop and completes all updates. Here is ...

Jquery's intermittent firing of .ajax within if statement

So, I've inherited a rather messy codebase that I need to modernize. The task involves upgrading from an old version of prototype to the latest jQuery 3.2.1 within a dated website built on php and smarty templates (not exactly my favorite). When a lo ...

A guide to retrieving data in React with useSWR whenever the state changes

In a specific scenario, I need to be able to choose users from a dropdown menu. Once a user is selected, the app should display that user's data. Upon loading the page, the app fetches the data for the first user in the array allUsers?.[0]: const [us ...

Vuetify's dialog feature allows for multiple videos to be played at once using a v-for

I'm currently facing an issue with my v-for loop that goes through a media array in order to play a video. The problem is that when I open my v-dialog, both models are displayed on top of each other. Additionally, the video keeps playing even after th ...

In what way can you reach an unfamiliar form within a controller?

I am working with multiple dynamically generated forms, each associated with a different model. In my controller, I need to iterate through all the errors within the forms. I assign form names based on the models. <form name="{{myForm}}" novalidate> ...

Assigning a value to an attribute as either a "string" or null within JSON Schema while specifying a maximum length

I'm currently working on crafting a JSON schema that supports a nullable attribute. I am aiming to have the ability for specific JSON structures like this one be considered valid: { "some_name" : null } This is how my schema looks like: { "type" ...

Adjusting the color of a cell based on its value

Currently, I am in the process of converting a CSV file to an HTML table by utilizing a tool available at . However, I am facing a challenge in modifying the background color of cells based on their values. I would greatly appreciate any help or guidance w ...

Retrieving a boolean value (from a JSON file) to display as a checkbox using a script

Currently, I am utilizing a script to fetch data from a Google Sheet $.getJSON("https://spreadsheets.google.com/feeds/list/1nPL4wFITrwgz2_alxLnO9VBhJQ7QHuif9nFXurgdSUk/1/public/values?alt=json", function(data) { var sheetData = data.feed.entry; va ...

What is the reason behind Chrome's fullscreen mode turning the background black?

I created a webpage with full-screen functionality and made sure to use the correct CSS properties to ensure that my gradient background covers the entire screen perfectly. However, I encountered an issue when clicking the full-screen button in Chrome - th ...

Using jQuery to iterate over a multi-dimensional array and showing the child elements for each parent array

I am facing an issue with a multidimensional array that contains objects and other arrays. While it is simple to loop through the parent array and display its contents in HTML, I encounter a problem when there are multiple layers of arrays nested within ea ...

The page does not seem to be reloading properly due to a redirect issue

I am currently updating some data and then attempting to reload the current page. After discovering the status code that allows me to switch the redirect method from put to get (303), I noticed that it is functioning correctly. However, despite seeing "fi ...

There has been a mistake: The module '.... ode_modules erser-webpack-plugindistindex.js' cannot be found. Please ensure that the package.json file contains a correct "main" entry

After setting up Vue.js and executing npm run serve, I encountered the following error: PS C:\Amit\Demo\VUEDemo\myvue> npm run serve > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="98f5e1eeedfdd8a8b6 ...

Unable to smoothly expand Div in React using Tailwind

I'm facing a challenge in animating a div that contains input elements. I want it to expand smoothly - initially, the text area is small and the other two inputs and buttons are hidden. When clicking on the text area, the input and button should be re ...

Unable to convert data to a JSON string

I've created a tool for building dynamic tables: <div class="container"> <div class="row"> <div class="col"> <hr> <h3>Add your data</h3> <button id="addTd" type="button" class="btn btn-pr ...

Incorrectly loading static images in React SSR

I am currently working on a React SSR app and my folder structure looks like this: My static express middleware successfully handles static files and images in the tag on the client side: However, when I attempt to renderToPipeableStream for server-side ...

JavaScript Polling Based on Time

I am currently working on developing an alarm clock feature using the Date() object. My aim is to have a function trigger when the time from the date object matches the time set by the user. Users set their alarms by selecting a specific time, but I' ...

What is the best way to set up routing with vue-leaflet?

I've been working on coding an E-Bike Sharing website, and was given a JSON file containing charging station locations throughout NYC. My objective is to create a map where users can input two addresses or coordinates (start and end positions), and th ...