Create Vue.js components

I am facing a challenge. On the server, I have a list of technologies, and in my HTML file, I want to display them dynamically:

<div
  v-for="(technology, index) in technologies"
  :key="technology.id"
>
  technology-card
   v-if="index < currentlyTechnologies"
   :technology="technology"
  />
</div>
<button
  type="button"
  @click="onToggleButtonClick"
>
  {{ toggleButtonText }}
</button>

In my Vue.js logic, I have implemented the following solution:

data () {
    return {
      currentlyTechnologies: 6
    }
  },
  computed: {
    toggleButtonText (): string {
      return this.currentlyTechnologies === this.technologies.length ? 'Show less' : 'Show more'
    }
  },
  methods: {
    onToggleButtonClick () {
      const limitTechnologies = 6

      this.currentlyTechnologies = this.currentlyTechnologies === limitTechnologies ? this.technologies.length : limitTechnologies
    }
  }

However, despite my efforts, all 18 elements are being rendered in the DOM. How can I ensure that only the visible elements are displayed on the page?

Answer №1

If I correctly understand your request, you need assistance with creating a computed property for technologies and changing the amount of technologies to display when a button is clicked.

<div
  v-for="(technology, index) in technologies"
  :key="technology.id"
>
  <technology-card
   :technology="technology"
  />
</div>
<button
  type="button"
  @click="onToggleButtonClick"
>
  {{ toggleButtonText }}
</button>

Here is a snippet from your .vue file:

data () {
    return {
      amountOfTechnologiesToDisplay: 6,
      technologiesFromServer: [
        // array of objects {}, {}, {}....
      ]
    }
  },
  computed: {
    toggleButtonText (): string {
      return this.currentlyDisplayedTechnologies === this.technologies.length ? 'Show Less' : 'Show More'
    },
    technologies(): array {
      return technologiesFromServer.slice(0, this.amountOfTechnologiesToDisplay);
      
    }

  },
  methods: {
    onToggleButtonClick (): void {
      const limitTechnologies = 6;

      this.amountOfTechnologiesToDisplay = this.amountOfTechnologiesToDisplay < this.technologiesFromServer.length ? this.technologiesFromServer.length : limitTechonogies;
    }
  }

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

What is the process for triggering an Event in VueJS 3 without using a component?

I have a tool that performs a function to delete certain elements. After this action is completed, I want to send a signal to the main section to update its information. <template> <div class="q-pa-md row items-start q-gutter-md"> ...

My program contains redundant sections that are being repeated multiple times, and I am unsure of how to remedy this issue

This particular payment gateway relies on a paid market for processing transactions. Unfortunately, there seems to be an issue where multiple error messages are being triggered during the payment verification process. The errors include: ❌ | An error h ...

Retrieve the identification number from the code snippet containing the "append(<tr><td="ID">..." template

I have a table that I'm populating with data from a firebase database using the append() function. $("#table_body").append("<tr><td>" + name + "</td>" + "<td>" + brand + "</td>" + ...

Why does Material-UI TableCell-root include a padding-right of 40px?

I'm intrigued by the reasoning behind this. I'm considering overriding it, but I want to ensure that I'm not undoing someone's hard work. .MuiTableCell-root { display: table-cell; padding: 14px 40px 14px 16px; font-size: 0. ...

building a reactive grid in ReactJS

I'm looking for an explanation of the code inside the constructor that sets the gridFull state. As a newcomer to JavaScript, I'm struggling to understand this particular line of code. constructor() { super(); this.speed = 100; ...

Smooth scrolling feature malfunctioning in mobile view

While working on my website, I noticed that the smooth-scroll feature works perfectly on desktop browsers. However, on mobile devices, when I click on a link, it does not scroll to the correct position. It always ends up much lower than expected. Any idea ...

Why does routing function correctly in a browser with AngularUI Router and Ionic, but not in Ionic View?

My Ionic App runs smoothly in the browser when using ionic serve. However, I encounter issues with routing when running the app in Ionic View (view.ionic.io) after uploading it with ionic upload. The index.html loads but nothing within <div ui-view=""& ...

The continuous declaration of scopes in AngularJS

Creating a website that features a variety of card games, each with its own controller. However, there are certain functions that are repeated across all the games. Is it feasible to consolidate this code into a single game? The inheritance process in Ja ...

An error message indicating that the page is currently being unloaded has appeared

While working on a NodeJS-ReactJS Isomorphic App, I encountered an issue when clicking on a Link. An error message popped up saying: Uncaught (in promise) Error: Request has been terminated Possible causes: the network is offline, Origin is not allowed by ...

What are the steps to implement Vuelidate 2 in a web browser?

Back in the stone age, we used to just drop in a .js file or a CDN reference and start coding. But now things seem much more complicated. I'm trying to use Vuelidate 2 in the browser, but I can't seem to figure it out. I've already done npm ...

Object contents shift upon my first gaze

While working on my JavaScript/ReactJS code, I encountered an issue where the Object (Obj) I was printing in the console contained keys as ids and values as true/false statuses, but when I opened the object, the values would automatically change. For exam ...

triggering Javascript upon page load

Looking to execute my Javascript only once during page load, and not during postbacks. Using Asp.net 3.5. ...

Unable to capture DOM events on the component within the hosting component

I have a Vue component with several objects called lines. I am creating a table from these lines using different components based on the type of line. Everything is working perfectly fine. Below is a simplified version of the component: <template> ...

transform a nested JavaScript object into a one-dimensional array

There is a JavaScript object in the following format: { "draw": "", "columns": [ { "data": "userid", "name": "", "searchable": true, "search": { "value": "", "regex": false } } ] } I want to tra ...

I am curious about how to switch between different modes in order to change the appearance, functionality, and interaction of a program. Currently, I am exploring this

const options_list = ['Exploring', 'Creating']; // an arraylist holding different activities for the bot to switch between. bot.on('ready', () => { setInterval(() => { const randomIndex = Math.floor(Math.random() * ( ...

Node.js equivalent of Java's byteArray

I have a Node.js REST API with images being sent from a Java application as byte arrays. Here is an image Below is the string representation of the image in byte array form: [B@c75af72 I need to decode this byte array to verify if it is indeed an ima ...

Angular component featuring an array of information

I have developed a custom directive app.directive('projects', ['projectServices', function(projectServices) { var projectController = function() { var proj = this projectServices.getProjects() ...

express-validator never accepts valid input

Currently, I am working on a project using the most recent version of nodejs and express. The basic site setup is complete, and now I am focusing on implementing user authentication based on what I've learned from this course. However, no matter what ...

Retrieving specific value from a Parent Controller in AngularJS using UI Router

I have a request to display the value stored in $scope.resAVal on my index.html page. This value is accessible within the RootCtrl. index.html <!DOCTYPE html> <html ng-app="plunker"> <head> <!-- any required JavaScript librarie ...

Error occurred during JSON transmission to REST API - Access-Control-Allow-Origin restriction encountered

I am looking to send JSON data to my REST API. I have successfully tested it using Postman, but now I want to send data from my frontend. However, I am unsure about where to add the header and how to do it. My backend is built with Spring Boot and my front ...