Can Vue allow for the inclusion of HTML elements to store data seamlessly?

One example involves displaying array elements in an <ul>, with each element starting with <li> and ending with </li>. Here is the attempted code:

clearedtaskslist: function(){
this.template='<ul>'      
for(let i=0;i<this.archive.length;i++)
{
  this.template+= '<li>' + this.archive[i].task '</li>'
}
this.template+='</ul>'
}

Is it possible to implement this functionality in vue?

Edit:

I need to display data from various events, but using v-for only results in the last event data being printed. For instance, I have to show completed tasks and the user who completed them every time someone triggers the related event.

clearedtaskslist: function(){
      
for(let i=0;i<this.archive.length;i++)
{
  this.template += 'Task:  ' + this.archive[i].task 
  this.template +=  ' user: ' + this.archive[i].taskuserID + '----||----'
}

}

This is how I retrieve the data and concatenate strings. However, I need to break the line after each cycle (and I am unsure how to do it).

<div style="margin: 0 auto; margin-top: 25px;" class="w-50">
   <h5 v-if="archive.length>0"><i>Completed tasks</i></h5>
  <div style="margin:0 auto;"> <p>{{template}}</p> </div>
</div>

This is where the data is displayed. The goal is to style the data effectively without relying on cumbersome string concatenation. Is there a way to achieve this in Vue?

Answer №1

After analyzing the scenario, I crafted an illustrative example resembling your situation. However, please keep in mind that adjustments may be necessary to tailor it to fit your specific requirements, given my limited understanding of your application's scope.

<template>
  <div class="hello">
    <div class="buttons">
      <button
        v-for="(user, index) of getUniqueUsers"
        :key="user+index"
        @click="selectedUser=user"
      >User: {{user}}</button>
    </div>
    <ul v-if="selectedUser">
      <li
        v-for="(task, index) of archive.filter((user) => user.taskuserID==selectedUser)"
        :key="task+index"
      >
        <p>User: {{task.taskuserID}}</p>
        <p>Task: {{task.task}}</p>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  name: "HelloWorld",
  data() {
    return {
      selectedUser: null,
      archive: [
        {
          taskuserID: "1",
          task: "task1"
        },
        {
          taskuserID: "2",
          task: "task3"
        },
        {
          taskuserID: "1",
          task: "task3"
        },
        {
          taskuserID: "4",
          task: "task4"
        },
        {
          taskuserID: "5",
          task: "task1"
        }
      ]
    };
  },
  computed: {
    getUniqueUsers() {
      const uniqueUsers = [
        ...new Set(this.archive.map(user => user.taskuserID))
      ];
      return uniqueUsers;
    }
  }
};
</script>

<style scoped>
.buttons {
  display: flex;
  justify-content: space-around;
}
</style>

  • The example incorporates a fictitious archive data property, which is an array of objects storing taskuserID and task details.
  • A computed property was utilized to extract unique taskuserID values from the archive array.
  • By iterating through the sorted array of unique users using v-for, individual buttons are generated for each user selection.
  • Upon clicking a button, the subsequent loop displays all tasks associated with the chosen taskuserID by filtering the archive array based on the selected user value.

Explore Working Example

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

Broaden the default className attribute of the component

Greetings! I'm currently using Bootstrap with React and I am trying to figure out how to extend my component by passing className props deeper. Within my atom component, I have two separate files. The first one contains the component declaration. B ...

The request made to `http://localhost:3000/auth/signin` returned a 404 error, indicating that

My goal is to access the signin.js file using the path http://localhost:3000/auth/signin Below is my code from [...nextauth].js file: import NextAuth from "next-auth" import Provider from "next-auth/providers/google" export default N ...

A guide on converting HTML and CSS to a PDF file using JavaScript

I'm facing a challenge where I need to create a custom quote in pdf using data retrieved in JavaScript and sent in HTML. However, the issue is that no library supports CSS for the PDF generation process. After some research, I came across HTML2CANVAS ...

Problem with integration of Bootstrap datetime picker in AngularJS directives

I am currently utilizing the Bootstrap Datetime picker to display data from a JSON file in calendar format. The data is first converted into the correct date format before being shown on the calendar, with both a To date and From date available. scope.onH ...

What is the process of passing a JavaScript variable to PHP with AJAX?

In the given scenario, I have an HTML file that is intended to display the content of a text file at div id="myDiv". The text file content will be read by PHP. However, there seems to be an issue in retrieving the content from the text file. I need assis ...

Setting up React Router in a nested directory with a flexible route structure

As a newcomer to react router, I am seeking guidance on setting it up in a specific scenario. Imagine we have a PHP application running on 'http://www.example.com'. Within this setup, there is a react application located at 'http://www.examp ...

best practices for sending multiple requests using node js

I have a list of 4 URLs, each containing JSON data. How can I iterate through these URLs? Example: URLs = [ "", "", "", ""]; Each URL contains JSON data for one student as follows: { date: 08/05/2014 stude ...

generate a collection using a string of variables

I'm looking for a way to pass a string as the name of an array to a function, and have that function create the array. For example: createArray('array_name', data); function createArray(array_name, data){ var new_array = []; // pe ...

Transforming the unmanaged value state of Select into a controlled one by altering the component

I am currently working on creating an edit form to update data from a database based on its ID. Here is the code snippet I have been using: import React, {FormEvent, useEffect, useState} from "react"; import TextField from "@material ...

Making an asynchronous request from jQuery to a Slim Framework API endpoint

I'm currently working on incorporating Slim into my project and I'm facing some challenges with setting up the AJAX call correctly: $.ajax({ url: "/api/addresses/", type: 'POST', contentType: 'application/j ...

Blogger Extension - Cross Domain Communication

As I work on creating a blogger plugin, my goal is to send information from the blog page for analytic purposes and then display the results on the visitor's page. Initially, I tried sending the page content using an ajax call but encountered an error ...

Is it possible to recreate the initial JavaScript source file using a minified version alongside its associated source-map file?

Currently, I am tackling a project that involves statically analyzing JavaScript code. The challenge lies in the fact that for certain libraries, I am limited to only having a minified version of the file along with its corresponding source-map. Are ther ...

Identifying whether a Property in an Object is another Object

I'm planning to utilize JSON for stringifying the value of a property within an object, which will then be stored as a Tag on Google Calendar using Google Apps Script. The value in question is actually a double-nested Object (look at extraAttributes w ...

Is it possible to use null and Infinity interchangeably in JavaScript?

I've declared a default options object with a max set to Infinity: let RANGE_DEFAULT_OPTIONS: any = { min: 0, max: Infinity }; console.log(RANGE_DEFAULT_OPTIONS); // {min: 0, max: null} Surprisingly, when the RANGE_DEFAULT_OPTIONS object is logged, i ...

Angular: Utilizing Nested ng-repeat Alongside groupBy Feature on Initial Page Load or Refresh

With some help, I've made it this far on my project. However, I need to take one more step to achieve my goal. I want to group data based on an attribute that is currently passed through an ng-click action. Is there a way to automatically do this on p ...

Utilize JavaScript and jQuery to extract the selected text's context from a webpage

I'm looking to extract the contextual information surrounding a selected text on a web page, specifically the 25 words before and after it. I've tried using JavaScript and jQuery with the following code snippet but unfortunately, it doesn't ...

Activate trust proxy in Next.js

When working with Express.js, the following code snippet enables trust in proxies: // see https://expressjs.com/en/guide/behind-proxies.html app.set('trust proxy', 1); I am attempting to implement a ratelimit using an Express middleware that is ...

"Using Mongoose to push objects into an array in a Node.js environment

I am faced with a peculiar issue involving an array of images in my code. Here is the structure: var newImageParams = [ { image_string: "hello", _type: "NORMAL" }, { image_string: "hello", _type: "NORMAL" } ] M ...

Updating text color with Ajax response value

I need assistance with updating the text color of a sensor value displayed using html/ajax. Currently, the sensor value is being displayed successfully, but I want the text color to change based on the value. Here's an example: if value < 50 then f ...

Tips for arranging alternating elements in a column layout using an array data model

In the $scope there is a dynamic array and in the HTML template, there are three columns. The goal is to render elements from the array in the HTML like this: <div class="first-column"> <div><!--element of (index + 3) % 3 === 0 will be pl ...