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

Function not executed right away

One of the functions I have is called showLoading(). This function is responsible for displaying a spinner by adding a specific class to the body: function showLoading(){ //console.log("show loading"); loading = true; $("body").addClass("loadi ...

Using jQuery to add a class to an input option if its value exists in an array

Looking for a way to dynamically add a class to select option values by comparing them with an array received from an ajax call. HTML <select id="my_id"> <option value="1">1</option> <option value="2">2</option> ...

The created function in VueJS may sometimes result in an undefined outcome

I recently started a project with VueJs where I encountered an issue while making a GET request using the Axios library. The data was returned as expected, but I faced difficulties calling the loadUsers function inside mounted. Here is my code snippet: ex ...

Update your mappings for the city of Istanbul when utilizing both TypeScript and Babel

Currently, I am facing the challenge of generating code coverage for my TypeScript project using remap Istanbul. The issue arises due to the usage of async/await in my code, which TypeScript cannot transpile into ES5 directly. To circumvent this limitation ...

jqueryajax function returns a boolean value upon completion

Recently, I developed a container method to handle an ajax request: function postRating(formData) { $.ajax({ type: "POST", url: '/api/ratings', data: formData }) .done(function () { return true ...

The issue of the "port" attribute not working for remotePatterns in the Image component has been identified in Next.js 13's next.config.js

I've encountered an issue with the code snippet below. I'm attempting to utilize remotePatterns in my next.config.js file to enable external images. Strangely, when I set the port to an empty string "", it functions correctly. However, specifying ...

Text centered on hover for figure caption

Check out this fiddle http://jsfiddle.net/sLdhsbou/ where I am trying to center the "x" that appears on hover perfectly no matter what size the image is. Also, why does the transition not occur when moving the mouse outside of the figure, unlike when movi ...

Tips for ensuring that npm only creates one instance of a dependency when it is being used by multiple projects

Struggling a bit here. I am diving into npm and configuration files for the first time. The current challenge involves setting up a vue project (though it might not be directly related to the issue) with typescript and then reusing its code and components. ...

Once the promise program enters the if-condition, even though the condition itself is actually false

There seems to be an issue with retrieving the location code using the AccuWeather API before getting the weather data for a city. Despite the location array being empty, the program still proceeds to a branch that expects a non-empty array. router.get(& ...

Show JSON response using AJAX jQuery

After receiving the JSON Response, { "user_data": { "id": 22, "first_name": xxx, "last_name": xxx, "phone_number": "123456789", }, "question_with_answers" : [ { "id": 1, ...

Retrieve binary data of an image from an API and render it on an HTML page

I have been working on storing images in a MongoDB database and trying to display the response I receive from an Express API as an image on the client side. The image source URL looks like this: src="/image/data/5a44dde172aa021d107e7d33" When I try to wr ...

Tips for modifying the properties of variables within an array using JavaScript

I have an array that holds variables used to control a specific template. divisionsListToManipulate: ['showActivitiesSection', 'hideAssignActionplanDiv', 'displayProp ...

PHP jQuery buttons for popovers

With a simple click of a button, I can generate 8 presentations and then edit each one individually by clicking on its respective name. Within this editing process, there is another form where I would like to include additional buttons that allow me to cus ...

Previewing multiple images with multiple file inputs

I am trying to find a way to preview multiple images uploaded from different inputs. When the button is pressed, the content from a textarea containing <img src="$img" id="img1"> is written inside a div called seeimg. The IDs for these images range f ...

Ways to eliminate the blue selection box when dragging canvas objects in fabric framework

I've been trying to find a solution to remove the annoying blue highlight box that appears when dragging on the canvas while using fabric js, but so far I haven't had any luck. I've tried the following code, but it only works for text and n ...

Tips on avoiding page refresh when hitting the submit button:

I am working on a form that consists of one input field and one submit button. <form method='POST' action='' enctype='multipart/form-data' id="form_search"> <input type='hidden' name="action" id="form_1" va ...

Animating the change in Slider value with Material-UI in React

Recently delving into React, I encountered my first challenge that has been consuming my time for hours. I'm working with a Slider component from Material-UI and seeking to animate the value changes. Currently, when clicking on a button, the slider i ...

What is the best way to ensure an AJAX get-request waits for the page to finish rendering before providing a response?

I've been working on a Greasemonkey Script for a specific section of this website (Site1). Site1 offers various deals and discounts, and my script is designed to perform the following task: When a user visits an offer on Site1, the script checks with ...

Encountering an unforeseen issue with the config.kit.methodOverride while executing Svelte

While working on a Svelte project, I recently updated it to the latest version using the migration tools provided. However, now the project doesn't seem to interact with npm as expected. When I run npm i, it installs the node modules but throws an er ...

Access an attribute using slashes in Jquery

I've been struggling to use jQuery to select an object based on a unique filename attribute. However, I'm facing issues with escaping slashes when the selector is created using a variable. Despite spending hours trying different combinations, I s ...