What are some ways I can extract a value using Vue.js?

I've been working with Vue for a few days and have run into an issue.

I am using jQuery AJAX to load text content in the template, but I need to truncate the title and description with ellipsis. Here is the method I wrote:

methods:{
               titleELLIPSIS:function(){
                    var title = self.articleList.title;//AJAX data
                    var titleLength = title.length; 
                    var maxWidth = 15;
                    var newTitle = title.split("",maxWidth);
                    return title(function(ELLIPSIS){
                        if(titleLength>maxWidth){
                            for(var j=newTitle.length-1;j>0;j--){
                                delete newTitle[j];
                                var ELLIPSIS = self.innerHTML = newTitle.join('')+'...';
                                if(newTitle.length<=maxWidth){ break;}
                                return ELLIPSIS;
                            }
                        }
                    })

                }
}

And here is my template:

<h2 class="ellipsis-2">{{titleELLIPSIS}}</h2>

How can I display the truncated title in an h2 element?

Please share some ideas with me.

Also, just to note, the AJAX call is successful as other data is displaying correctly.

Answer №1

If you're searching for a way to handle computed properties, consider moving titleEllipsis into the computed section:

computed: {
   titleELLIPSIS:function(){
        var title = self.articleList.title;//Obtained through AJAX
        var titleLength = title.length; 
        var maxWidth = 15;
        var newTitle = title.split("",maxWidth);
        return title(function(ELLIPSIS){
            if(titleLength>maxWidth){
                for(var j=newTitle.length-1;j>0;j--){
                    delete newTitle[j];
                    var ELLIPSIS = self.innerHTML = newTitle.join('')+'...';
                    if(newTitle.length<=maxWidth){ break;}
                    return ELLIPSIS;
                }
            }
        })

    }
}

Answer №2

titleELLIPSIS is considered a method, so it needs to be actively called in order to work properly.

<h2 class="ellipsis-2">{{ titleELLIPSIS() }}</h2>

The way you are currently using it implies that it is a data or computed property.

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

Using Knex.js to perform a case-insensitive search on an object with the whereIL

Still searching for a solution to this problem. I am attempting to pass an object with filters as keys and values. ex. const filters = { 'id': 12, 'first_name': john } function findBy(filter) { return db('quotes') ...

React is up and running smoothly on my local machine, but unfortunately encountering issues on Vercel

I have encountered an issue while trying to deploy my website on Vercel. Despite the build logs showing a successful compilation, I am receiving a "failed to compile" error: [16:43:24.951] Running build in Washington, D.C., USA (East) – iad1 [16:43:25.05 ...

Encountering an issue: Module """ not located at webpackMissingModule

I'm facing an issue while trying to webpack my express application. Specifically, I encounter the following problem whenever I attempt to access the / page: Encountering Error: Cannot find module "." at webpackMissingModule Below is a snippet of c ...

React Component for Toggling a Click Event on a Select Tag

I'm currently developing a react app where clicking on a specific text triggers the display of a select tag with several items. However, I've encountered two issues: Upon clicking the text, the select tag appears on ALL list items instead of ju ...

Efficiently centering content in a grid layout using automatic fit repetition for optimized responsiveness

I've implemented a responsive grid where each item has its own hidden details section that is revealed upon clicking the item. The structure of the HTML/CSS setup is as follows: <div class="grid"> <div class="item"> ...

Is it possible to dynamically insert a ng-mouseover in AngularJS using Javascript?

It seems like there is an issue with this code: var run_div = document.createElement('div'); run_div.className = 'whatever'; run_div.textContent = 'whatever'; run_div.setAttribute('ng-mouseover', 'console.log(&b ...

Surprising results from combining ng-mousedown with ng-click

I am working with the following HTML structure: <label ng-mousedown="mousedownHandler($event)" ng-click="clickHandler($event)"> <input type="checkbox" /> </label> Within my scope, I have the following methods defined: $scope ...

Interacting with APIs in Svelte applications

I'm fairly new to utilizing Svelte and JavaScript. I've been attempting to construct a page that incorporates numerous API components from external websites, but I'm facing some challenges. I'm uncertain about where exactly to place the ...

The paragraph was unable to start in a new line as expected

After spending the past couple of hours scouring the internet and trying various solutions, I have come up empty-handed. My dilemma revolves around attempting to create a line break within a paragraph, but no matter what I try, it doesn't seem to work ...

Switch out two for loops with the find or filter method in JavaScript

In my unique approach, I am showcasing a variety of product details lists based on availability in various shops. To achieve this, I have implemented the following method. for (let i = 0; i < this.prodList.length; i++) { let setContent = false; for ...

What causes ng-options to return an empty array in AngularJS when using $element.find('option')?

For my project in Angular 1.4, I am utilizing the ngOptions directive to dynamically fill a <select> element with <option> elements based on an object structure like this: {black: '#000000', red: '#FF0000', ...} The implem ...

The passport local strategy functions properly when tested with Postman, but encounters a "missing credentials" error when used with axios

I am currently working on creating a login system using passport and a local strategy. Strangely, when I attempt to send the request through axios it doesn't seem to work, although it functions properly with Postman. When using axios, I receive an er ...

Adding the output from a promise to an array

I am attempting to clear out and then refill an array with values retrieved from a promise. However, I've noticed that the values don't always get added back in the same order. $scope.$watch ('timeRange', function (newValue, oldValue, ...

Error: JSON at position 1 is throwing off the syntax in EXPRESS due to an unexpected token "

I'm currently utilizing a REST web service within Express and I am looking to retrieve an object that includes the specified hours. var express = require('express'); var router = express.Router(); /* GET home page. ...

What is the best way to replicate this with sinon?

Check out this test I've written for express middleware. Instead of using global state for the variables, I'm utilizing sinon to mock the callbacks. it('should return HTML', async () => { const val = [] const req = {} ...

My code is not functioning properly with Jquery attr in my function

My function runs every five seconds. However, after it runs, the prewhour variable does not seem to fetch the latest value, even though I have updated it in my source code. I have tried testing: prewhour2 = $("#hour").attr("data-now"); In Google Chrome ...

Is it possible to pass a prop down through 2 levels of nested children components?

I am facing an issue with passing a prop in my component. The id is passed as a prop like this: <comments myId="1"></comments> In the comments component, I have defined the prop like this: props: [ 'myId', ], Within the commen ...

What is the best way to determine if the form has been submitted?

I am working on a React form and need to determine when the form has been successfully submitted in order to trigger another request in a separate form. const formRef = React.useRef<HTMLFormElement>(null); useEffect(() => { if (formRef &a ...

After Effects Script: Apply various character styles from main text to subtext

I'm currently working on an After Effects script that can transfer the style and text of a text layer to another text layer in a different comp. The script works perfectly if the parent text layer only has one style, but I need it to handle a situatio ...

What is the process for removing a specific column (identified by its key value) from a JSON table using JavaScript and Typescript?

[{ "name": "employeeOne", "age": 22, "position": "UI", "city": "Chennai" }, { "name": "employeeTwo", "age": 23, "position": "UI", "city": "Bangalore" } ] If I remove the "Position" key and value from the JSON, the updated r ...