Is it possible to insert items into Vue component data? I'm interested in creating a table following JavaScript filtering and manipulation

I'm working on a website that will have multiple pages, each containing tables. To streamline this process, I wanted to create a table component. However, I haven't added any data to the tables yet because I need to manipulate it using JavaScript.

My plan was to push each row object into the data property of my Vue Component after manipulating it with JavaScript, and then use v-for in the HTML to populate the table. But I can't seem to find any examples of pushing data into Vue components. Am I missing something?

If I can't push data directly into the components, does that mean I have to push it into the parent VM instead? And would that require creating a new data property for each instance of the table..?

I'm struggling to see how to connect Vue with the output from JavaScript in the bigger picture. Any advice or suggestions would be greatly appreciated.

Answer №1

It is not recommended to push data directly into a Vue component. A better approach would be to utilize Vuex for handling the data manipulation. By using Vuex, you can establish a closed-loop data system that updates state data with mutations and retrieves the updated data with a getter function. However, if you choose to initialize your data property correctly within the component, it is possible to handle everything within the component itself.

To address your question specifically, you can achieve this by following these steps:

 data () {
       return {
          myData: [],
          someDataObject: null
       }
    }
...
methods: {
   fillData () {
      this.myData.push(this.someDataObject);
   }
}

In your template section, implement the following code:

...
<div v-for(item in myData, index) :key:item item:item>
   <input type:'text' v-model="someDataObject">
   <button @click="fillData();"></button>
   <p>{{myData[0]}}</p>
</div>
...

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

Mastering the art of utilizing generic types efficiently within Higher Order Component structures

My parent component (Grid) passes props to a higher-order component (hoc), let me show you: <QuickBooksTable itemList={quickBooksList} /> Here is the QuickBooksTable component: export const QuickBooksTable = withIntegrationTable(props: : WithIntegra ...

JQuery scroll position, floating menu bar, and animation delay

I'm in the process of creating a menu bar using JQuery, and I am a beginner with this technology. My goal is to have the menu bar delay when scrolling down, but stick immediately to the top of the browser window when scrolling up. Currently, the delay ...

Removing an object in Three.js using scene.getObjectByName() method

When developing my game, I encountered a challenge. I'm loading 4 different buildings and creating 15 clones of each within a for loop. These clones are then added to an array called masterBuildingArray, which I iterate through using a forEach loop. T ...

perform validation middleware following completion of validation checks

Looking to establish an Express REST API, I aim to validate both the request parameters and body before invoking the controller logic. The validation middleware that I have put together is as follows: const { validationResult } = require('express-va ...

Angular does not allow the transfer of property values from one to another

As of late, I have delved into learning Angular but encountered an issue today. I have the following Component: import { Component, OnInit } from '@angular/core'; import { SharedService } from '../home/shared.service'; import { IData } ...

Challenges with the "//" syntax in UNIX

$('#lang_choice1').each(function () { var lang = $(this).val(); $('#lang_files').empty(); $.ajax({ type: "POST", url: '< ...

Tips for accessing the value within a function during an onClientClick event

In my ASP.NET ASPX application, I am trying to evaluate a parameter within a function during an `onclientclick` event. Additionally, I want to include `return false` in the `onclientclick` event to prevent the page from refreshing. The `onclientclick` eve ...

Using JQuery to locate and substitute occurrences of HTML elements throughout my webpage

Looking for assistance with a JQuery search and replace task involving multiple instances of HTML within a specific DIV element on my webpage. Specifically, I need to change certain items in the textbox to a simpler display format. Here is a snippet of th ...

How to Link Laravel 5 with Ajax?

I've implemented this Laravel code in my controller for the detach function. $input = Input::all(); $product= Products::findOrFail($input['product_id']); $product->tags()->detach($input['tag_id']); $product= Prod ...

Guide to retrieving information from an API and incorporating it into a fresh JSON structure

I am currently working on fetching data from an existing API endpoint and using a part of that data to create a new endpoint in Node.js with Express. Specifically, I am trying to retrieve the userId from https://jsonplaceholder.typicode.com/posts/1 and int ...

Adding a JavaScript global variable into a <div> element within the HTML body

Here is an example HTML tag: <div data-dojo-type="dojox.data.XmlStore" data-dojo-props="url:'http://135.250.70.162:8081/eqmWS/services/eq/Equipment/All/6204/2', label:'text'" data-dojo-id="bookStore3"></div> In ...

Should you opt for returning [something] or (nothing) in Javascript with ExpressJS?

Is there a distinct contrast between returning something and returning nothing? Let's take a look at an example: user.save(function(err){ if ( err && err.code !== 11000 ) { console.log(err); console.log(err.code); res.send(&apo ...

Guide on adding a timestamp in an express js application

I attempted to add timestamps to all my requests by using morgan. Here is how I included it: if (process.env.NODE_ENV === 'development') { // Enable logger (morgan) app.use(morgan('common')); } After implementing this, the o ...

Analyze Javascript code and monitor every variable alongside their corresponding values

After watching Bret Victor's engaging talk "Inventing on Principle" the other night, I was inspired to create a real-time JavaScript editor similar to the one he showcased. You can see a glimpse of it in action at 18:05 when he demonstrates binary sea ...

Retrieve the Typescript data type as a variable

I have the following components: type TestComponentProps = { title: string; } const TestComponent: React.FC<TestComponentProps> = ({ title, }) => { return <div>TestComponent: {title}</div>; }; type TestComponent2Props = { bod ...

Strategies for simplifying this extensive if/else block

My current if/else statement in React Native is functional but seems verbose. I am curious to know how other developers would optimize and shorten it. I feel like my code represents a beginner's approach, and I welcome suggestions on how to improve i ...

What is the best way to preload all videos on my website and across different pages?

I specialize in creating video websites using HTML5 with the <video> tag. On my personal computer, the website transitions (fadeIn and fadeOut) smoothly. However, on my server, each page seems to take too long to load because the videos start preloa ...

What is the reason behind router.base not functioning properly for static sources while running 'npm run build' in Nuxt.js?

Customizing Nuxt Configuration const BASE_PATH = `/${process.env.CATEGORY.toLowerCase()}/`; export default { router : { base : BASE_PATH }, } In addition, there is a static source for an image in the component: <img src="/mockups/macbookpro_01. ...

The antithesis of jQuery's .parents() selector

I am currently developing a userscript for a webpage with an old-fashioned design consisting mainly of tables. My goal is to access a long table of fields so that they can be filled in automatically by the script. To simplify, the structure is as follows: ...

Only include unique objects in the array based on a common property

I am currently working with the following array: [ {name: "Mike", code: "ABC123"}, {name: "Sarah", code: "DEF456"}, {name: "John", code: "GHI789"}, {name: "Jane", code: "JKL01 ...