Display a single table with multiple rows using Vue.js

Discovering Vue:

<!DOCTYPE html>
<html>
<head>
  <title>Exploring Vue Components</title>
  <script src="https://cdn.jsdelivr.net/npm/vue"></script>
  <style>
  </style>
</head>
<body>
    <div id="app">
        <friends v-for="friend in friends" :friend="friend"></friends>
    </div>
  <script>
      Vue.component('friends', {
        props    : ['friend'],
        template : `<table>
                    <tr><td>{{ friend.fname }}</td><td>{{ friend.lname }}</td></tr>
                  </table>`,
      })

    const vm = new Vue({ 
        el   : '#app',
        data : {
            friends : [
              {fname : 'Cornelius', lname : 'Johnson'},
              {fname : 'John',      lname : 'Waybe'},
              {fname : 'Neo',       lname : 'Anderson'},
            ],
        }
    })
  </script>
</body>
</html>

This code functions correctly but displays multiple tables instead of a single table with rows.

I attempted the following approach:

    <div id="app">
        <friends></friends>
    </div>

 Vue.component('friends', {
        props    : ['friends'],
        template : `<table>
                    <tr v-for="friend in friends" :friend="friend"><td>{{ friend.fname }}</td><td>{{ friend.lname }}</td></tr>
                  </table>`,
      });

    const vm = new Vue({ 
        el   : '#app',
        data : {
            friends : [
              {fname : 'Cornelius', lname : 'Johnson'},
              {fname : 'John',      lname : 'Waybe'},
              {fname : 'Neo',       lname : 'Anderson'},
            ],
        }
    })

However, it only displays an empty table.

Is there a way to render just one table along with its rows?

Answer №1

Make sure to assign the friends variable to the friends prop =).

<!DOCTYPE html>
<html>
<head>
  <title>My Vue Journey</title>
  <script src="https://cdn.jsdelivr.net/npm/vue"></script>
  <style>
  </style>
</head>
<body>
    <div id="app">
        <friends :friends="friends"></friends>
    </div>
  <script>
      Vue.component('friends', {
        props    : ['friends'],
        template : `<table>
                <tr v-for="friend in friends"><td>{{ friend.fname }}</td><td>{{ friend.lname }}</td></tr>
              </table>`,
      })

    const vm = new Vue({ 
        el   : '#app',
        data : {
            friends : [
              {fname : 'Alice', lname : 'Smith'},
              {fname : 'Bob',      lname : 'Brown'},
              {fname : 'Eve',       lname : 'Gray'},
            ],
        }
    })
  </script>
</body>
</html>

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

Can a THREE.Texture be created using a byte array instead of a file path?

In my server client system, the server parses a model file and sends vertex data to the client using a socket. I run into trouble when the model contains textures. I am able to read the texture (PNG file) into a byte array and send it to the client via soc ...

Determining if a mouse is currently hovering over an element using Javascript

I am working on a script that logs a message to the console if the user keeps their mouse over an element for more than 2 seconds. Here is the code snippet I have so far: var $els = document.querySelectorAll('#myDiv'); for(i = 0; i < $els ...

The function Array.map is unavailable, causing the datalist to not update as expected

I've been closely following a tutorial series on YouTube, but I keep encountering an error during the process. The link to the tutorial is available here: https://www.youtube.com/watch?v=re3OIOr9dJI&t=11s&ab_channel=PedroTech. The purpose of ...

Creating a tree structure from JSON data involves adjusting the entries as demonstrated below, utilizing programming languages for implementation

Data Entry: const info = [ { type: 'Electronics', category: 'cellphone', price: 800, profit: 50, }, { type: 'Clothing', category: 'shoes', price: 100, profit: 30, }, { ty ...

Using a semicolon right after "id" is recognized as an unexpected token

I encountered an issue while attempting to run my express.js program. The main server fails to start and displays the following error message. id; ^ SyntaxError: Unexpected token ; at new Script (vm.js:80:7) at createScript (vm.js:274:10) ...

The ideal method to transmit reactive data effectively is through Vue.js

Implementing vue.js. I've created an authentication file (auth.js) that stores the user information upon detecting a change in authentication state. There are other sections of the website that need to update when the user information changes. What is ...

Shared codes are compatible with both C and Javascript programming languages

Within the scope of this project, data will be retrieved from the server in either JSON or XML format. There are two separate client applications that will handle the data processing, with one being web-based and written in JavaScript, while the other is ...

Using a locally hosted HTML page to update a JSON file stored on my computer

In my current project, I need to reorganize data stored in an array named unknown. Each item in this array needs to be moved either to a new array called good or another one called bad. let data = { "bad":[], "unknown": ["b", "a", "d", "g", "o", ...

Can someone please explain how I can implement a multi-submenu feature using JavaScript?

HTML CODES: <header> <nav> <ul> <li class="asmenu"><a href="#">Menu1 <i class="fa fa-caret-down"></i></a> <ul class="submenu deact ...

Reactjs is having issues with Datatable functions

I am making use of the Datatable Library for creating tables easily. After fetching data with the Fetch API and rendering it to the table, everything seems to work smoothly. However, I am facing issues with DataTable Functions such as sorting, searching, ...

What sets apart `Object.merge(...)` from `Object.append(...)` in MooTools?

This question may seem simple at first glance, but upon further inspection, the MooTools documentation for the 'append' and 'merge' methods appears to be identical. Here is the code snippet provided in the documentation: var firstObj ...

The node.js oauth-1.0a implementation is successful in authenticating with Twitter API v1.1, however, it encounters issues

Having come across this function for generating an oauth-1.0a header: // auth.js const crypto = require("crypto"); const OAuth1a = require("oauth-1.0a"); function auth(request) { const oauth = new OAuth1a({ consumer: { key ...

Utilizing Shadow Root and Native Web Components for Seamless In-Page Linking

An illustration of this issue is the <foot-note> custom web component that was developed for my new website, fanaro.io. Normally, in-page linking involves assigning an id to a specific element and then using an <a> with href="#id_name&quo ...

What is the best method for transmitting pagination information and an array from a Laravel controller to a Vue component?

Here is an example of my controller code: public function index() { $products = $this->product->list(); dd($products); return view('admin.product.index',compact('products')); } After running dd($products);, you ...

"Having issues with Django not properly applying the JavaScript and CSS files I've linked in

I have completed my project and organized all the necessary files, including index.html, css, js, and settings.py within the appropriate folders. I am encountering an issue with applying a pen from the following source: CodePen index.html <!DOCTYPE h ...

What is the best way to retrieve information from Cloud Firestore, apply filtering and mapping techniques, and then deliver the refined data

My current challenge involves extracting data from Firestore, filtering it, and then mapping it as shown in the code snippet below: return Inventory .filter(x =>x["sub_category"] === item && x["category"] === category) .map(( ...

Invoking AngularJS Function from Login Callback Script

Just getting started with angularjs and I have a logincallback function that is used for external login. This function returns the returnUrl, closes the externallogin pop up, and redirects back to the main page. function loginCallback(success, returnUrl) ...

Updating the content in an HTML document using jQuery

In my workflow, I am utilizing jquery ajax to retrieve information from a PHP file. data = <option>Peter</option><option>Maria</option> Following that, I aim to leverage jquery to insert this data after the initial option. <s ...

When converting a .ts file to a .js file using the webpack command, lengthy comments are automatically appended at the end of

As a backend developer, I recently delved into UI technologies and experimented with converting TypeScript files (.ts) to JavaScript files (.js) using the webpack command. While the conversion works well, the generated .js file includes lengthy comments at ...

What is the most effective method for building this item?

Looking to create an object, let's call it "car": function car(name, speed, options){ this.name = name; this.speed = speed; this.options = options; } When it comes to the "options", I thought of using an array: var carMustang = new car("Musta ...