Ways to truncate text and include a "Read More" button within a v-for loop

The text in the "card-text" class is being retrieved from a Json file, but it's too long. I want to shorten the text and include a "Read More" button that expands the text when clicked on, but only for the specific card that was clicked. Is there a way to achieve this?

<template>
  <div class="container">
    <Modal
      v-if="showModal && GET_TRAILER.payload !== null"
      v-on:close="closeModal"
      :trailerAdress="GET_TRAILER.payload"
    ></Modal>
    <div>
      <div class="row" v-for="(obj,index) in GET_MOVIE" :key="index">
        <div class="card col-12 col-lg-3" style="width: 10rem;" v-for="movie in obj.payload" :key="movie.id">
          <img class="card-img-top" :src="`https://image.tmdb.org/t/p/w500${movie.poster_path}`" />
          <div class="card-body">
            <h5 class="card-title">{{movie.original_title}}</h5>
            <p class="card-text">
              {{movie.overview}}
            </p>

            <div class="buttonPosition">
              <button
                v-on:click="OpenTrailerModal(movie.id)"
                type="button"
                class="btn btn-success"
              >Watch Trailer</button>
            </div>
          </div>
        </div>
      </div>
    </div>
    <button @click="MoreMovies" type="button" class="btn btn-warning moreBtn">Load More Movies</button>
  </div>
</template>

Answer №1

Check out the code snippet below :

<template>
      <div class="container">
        <Modal
          v-if="showModal && GET_TRAILER.payload !== null"
          v-on:close="closeModal"
          :trailerAdress="GET_TRAILER.payload"
        ></Modal>
        <div>
          <div class="row" v-for="(obj,index) in GET_MOVIE" :key="index">
            <div class="card col-12 col-lg-3" style="width: 10rem;" v-for="movie in obj.payload" :key="movie.id">
              <img class="card-img-top" :src="`https://image.tmdb.org/t/p/w500${movie.poster_path}`" />
              <div class="card-body">
                <h5 class="card-title">{{movie.original_title}}</h5>

    ---------------------- Incorporate this section ------------------------------

                <div class="card-text">
                 <p v-if="movie.readMore"> 
                   {{ movie.overview | limitDisplay }}
               <a v-if="movie.overview.length > 100" @click="toggleReadMore(obj.payload, movie.id, false)">Read More<a> 
                 </p>

                <p v-if="!movie.readMore">
                {{ movie.overview }}
                <a @click="toggleReadMore(true)">Read Less<a> 
                </p>
               </div>

    ---------------------------------------------------------------

                <div class="buttonPosition">
                  <button
                    v-on:click="OpenTrailerModal(movie.id)"
                    type="button"
                    class="btn btn-success"
                  >Watch Trailer</button>
                </div>
              </div>
            </div>
          </div>
        </div>
        <button @click="MoreMovies" type="button" class="btn btn-warning moreBtn">Load More Movies</button>
      </div>
    </template>

Add the following methods and filters to your script :

methods: {

    toggleReadMore(payload, movie_id, value){

    payload = payload.map(function(item){
          if(item.id == movie_id){
              item['readMore'] = value;
              return item
          }
   });
}

},

filters : {

limitDisplay: function(value) {
  return value.substring(0, 100) + "...";
}

}

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

Difficulty encountered when trying to update intricate model using Angular UI modal

My current project involves managing a model containing nested arrays that represent different sections of a floor plan. Each section contains an array of booth objects. To optimize user interaction, I've created a view that displays all the booths on ...

Troubleshooting issue with refreshing selectpicker in Bootstrap-select and Vue.js

Incorporating the bootstrap-select plugin (found at ) into a ruby on rails app with Vue.js as the javascript framework has been my latest project. The goal is to have two select options where one selects a category and the other displays all available tea ...

Guide to iterating through an object and generating child components in React.js

Struggling with a React.js loop through child component issue as a newcomer to the language. I have an object and want to create child components from its values. Any help would be greatly appreciated. var o = { playerA: { name: 'a', ...

Guide to sending multiple forms from an HTML page to another PHP page

What is the best way to submit multiple forms on a single HTML page to a PHP page and process all the form elements in PHP upon clicking one button? Any suggestions are appreciated. Thank you in advance. <form id="form1" name="form1"action="abc.php" ...

Dealing with numerous entries in an HTML form using PHP

Currently, I am learning the basics of HTML, PHP, and Javascript. I recently worked on creating a user profile form that includes fields like Full Name, Email Id, and Mobile number. I also want to incorporate a section for "Hobbies/Interests" where users ...

Webpack referencing incorrect node_modules directory

I am currently working on a Vue client project that incorporates a Vue library project. The Vue library project also utilizes some third-party packages such as vue-material. These two projects are connected through the client project's Package.json f ...

Error: The system is unable to destructure the 'username' property from 'req.body' since it is not defined

Encountering a persistent issue with an error that I've been trying to resolve for days without success. The problem arises when attempting to register a user in the project, resulting in error messages being displayed. Here's a snippet of the co ...

In React.js, the switch case statement consistently defaults to the default case

Currently, I am working on utilizing <select> tags to update information across components using useContext(). However, I am encountering an issue where the default case is consistently being returned despite the fact that the logged values match the ...

VueJS3 and Vuetify are in need of some additional CSS classes

We are currently in the process of upgrading our application from old VueJS+Vuetify to VueJS3. However, we have encountered some classes defined in the template that are not available in the new version. These classes include xs12 (which is intended to co ...

ReactJS: Oops! Looks like there's an issue with the element type - it's invalid. We were expecting a string

I am in the process of setting up a basic server-side rendered React application. Listed below are the steps I have taken: Step 1: Creating a new React app using create-react-app: npx create-react-app my-ssr-app Step 2: Installing necessary dependencies: ...

Please refrain from including HTML code within the div element

When I input the following text inside a textarea: <strong>test</strong> and then display it within a div, the text appears in bold instead of showing the HTML code as entered. How can I prevent the HTML code from being rendered and instead d ...

Implementing a JavaScript/CSS popup element within a PHP document

I recently attempted to add a popup div to my front page by following instructions from this guide Given that I am not well-versed in coding, I found it challenging to implement the code into my PHP file (which is for a WordPress site). Uncertain about wh ...

Guidelines for accurately and appropriately creating divisions in JavaScript using mathematical principles

The program occasionally generates mathematically incorrect divisions, such as 2/0, and does not accept rounded answers like 0.33 or 0.33333 for "1/3". What modifications should I make to the code? Thank you for your assistance and time. document.getEl ...

Problem with Cordova Android build

Currently, I am utilizing vue-cordova to incorporate cordova into my Vue.js framework. After successfully adding the android platform and generating the APK file, a new challenge arose. Despite days of development work, running cordova build android only ...

Having trouble with Vue Router - always redirecting to the root path

I have encountered a puzzling issue with my code. I have two versions, one written in JS which is functioning correctly, and another written in TS which is not working as expected. Here is the JS version: import Alice from "@/Alice.vue"; import Bob from "@ ...

Information derived from a provided URL

I am currently developing a Spotify stats app, and I am facing an issue with creating a context to store the token provided by the app in the URL when a user logs in. TokenContext.js: import React, { useEffect, useState } from "react"; // token ...

What are some methods for displaying images retrieved from an API onto an HTML webpage?

Problem: The image is not appearing on my HTML page. How can I fix this issue? Please see below for details. https://i.sstatic.net/XYoHg.png Here are the code snippets I am using to display the image: <div class="col-md-7"> <div c ...

All fields in the form are showing the same error message during validation

This is the HTML code: The form below consists of two fields, firstname and lastname. Validation Form <form action="" method="post"> Firstname : <input type="text" placeholder="first name" class="fname" name="fname" /> <span class= ...

Exploring the functionality of async functions that utilize the await feature within the Karma and Jasmine JavaScript

Attempting to test an async function using Karma and Jasmine in an AngularJS v1.5.0 project. This async function, labeled as (a), contains 2 await statements: async function a(){ let var1 = await b() console.log(var1) let var2 = await c() } fu ...

Transforming JSON data into an interactive table with HTML

After spending four days searching for a solution, I am still unable to figure out my problem. My goal is to convert a JSON string data retrieved from a URL into a dynamic table using JQuery and JavaScript. To achieve this, I need to load the JSON string ...