Using Vue.js to dynamically update values in JavaScript code or tags

I've encountered an issue that I'm having trouble articulating, but I'll do my best to explain.

Upon loading the page, the following code snippet, which uses jinja-based placeholders, is executed:

<div class="ui container">
            <div class="ui tiny blue header">{{ i.ProductName }}</div>
            <h5 class="ui grey header">{{ i.Category }}</h5>
            <h5 class="ui red header">₹{{ i.ActualPrice }} <s>₹{{ i.StrikedPrice }}</s> </h5>
            <script type="text/javascript">var lastid = "{{lid}}"</script>
          </div>

The above code renders correctly, and I can retrieve the lastid as expected and set it as a global variable.

However, when the user clicks on "Load More," it triggers a Vue.js function like so:

<button v-on:click="loadmore" class="fluid ui button">Load More</button>

This event then calls a Vue.js function:

 <script>
    var app5 = new Vue({
    el: '#app-5',
    delimiters: ['[[',']]'],
    data: {
        message: [],
    },
    methods: {
        loadmore: function () {
          axios.get('http://127.0.0.1:5000/api/products/'+ld)
          .then(response => {this.message.push(...response.data);});
        }
    }
    });
    </script>

The Vue.js function pushes the data into a designated UI container as displayed below:

<div class="ui container">
              <div class="ui tiny blue header"> [[i.ProductName]]</div>
              <h5 class="ui grey header">[[i.Category]]</h5>
              <h5 class="ui red header">₹[[i.ActualPrice]] <s>₹[[i.StrikedPrice]]</s> </h5>
              <script type="text/javascript">lastid = [[lid]]</script>
            </div>

In this code snippet, there's a JavaScript tag where the value of lastid is not updating the global variable as intended. The lastid should be updated with the latest ID. Is there a solution to address this issue or any recommendations to change the current logic?

Answer №1

To enhance your app data, ensure to include the ld and make sure it is updated accordingly after receiving a response from axios:

data: {
    message: [],
    ld: 0, // default starting point for id
},
methods: {
    loadmore: function () {
        axios.get('http://127.0.0.1:5000/api/products/'+this.ld)
             .then(response => {
                 const messages = ...response.data;
                 this.message.push(messages);
                 this.ld = messages.pop().id;
             });

    }
}

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

Utilizing JQuery to Modify XML File Content

Looking to retrieve data from an XML file using jQuery and display it in a textbox? If you want changes made in the textbox to reflect back in the original XML file, there are ways to achieve this. Here is some code that can help: <html><head&g ...

Navigating through various Headers in ReactjsHandling different Headers in Reactjs

I am currently working on a project using Reactjs (Nextjs) and I need to have different headers for the home page compared to the rest of the pages. I have created a "Layout.js" file where I have placed the header and footer components, and then imported ...

Issue with Jest while testing a React component library that has been bundled without the React library

I have extensive experience building React applications and decided to create a React Component library. After researching different approaches, I chose to use Webpack and Babel for bundling without including React itself in the library. This decision was ...

FullCalendar dayClick event fails to trigger any action

I'm having trouble implementing the dayClick function in my fullCalendar. Despite setting up the calendar correctly, nothing happens when I click on a day. Here is the code I am using : var calendar; $(document).ready(function(){ app.init(); ...

Utilize jQuery to serialize the HTML input elements that are dynamically generated outside of the form

A proof of concept is in progress for an application that involves the generation of HTML elements based on user-configured fields. Here is a sample configuration: // Customer SAM $response = array( array ( NAME => CUSTOMER, TYPE = ...

Ways to verify the presence of isBrowser in Angular 4

Previously, isBrowser from Angular Universal could be used to determine if your page was being rendered in a browser (allowing for usage of features like localStorage) or if it was being pre-rendered on the server side. However, it appears that angular2-u ...

Create a URL hyperlink using javascript

I am looking to create a link to a page that updates its URL daily. The main URL is where X represents the day of the month. For example, for today, July 20th, the link should read: In my JavaScript section, I currently have code that retrieves the cur ...

The chart appears oversized in the vue js. How can I make it smaller in size?

I recently integrated a line chart from Chart JS into my Vue.js project, but the chart is taking up too much space on my webpage. I'm looking for ways to make it appear smaller and more compact. This is my first time working with charts in Vue.js, so ...

What is the best way to iterate over a multidimensional array in Angular/Ionic?

I've been struggling to find a solution tailored for looping in a .ts file instead of HTML. My main objective is to iterate over an array and compare the entered value with the keys. If there's a match, I want to retrieve the values stored withi ...

Extract and loop through JSON data containing various headers

Having no issues iterating through a simple JSON loop, however, the API I am currently utilizing returns a response with multiple headers. Despite my efforts in various methods to access the results objects, I still face some challenges. The response struc ...

Implementing custom fonts in Next.js using next-less for self-hosting

Seeking Solutions for Hosting Fonts in Next.js Application I am exploring the idea of self-hosting a font, specifically Noto, within my Next.js application that already utilizes the @zeit/next-less plugin. Should I rely on the npm package next-fonts to h ...

Tips for deleting multiple objects from an array in angular version 13

I am facing an issue where I am trying to delete multiple objects from an array by selecting the checkbox on a table row. However, I am only able to delete one item at a time. How can I resolve this problem and successfully delete multiple selected objects ...

Is utilizing React function components the most effective solution for this problem?

export default function loginUserUsing(loginType: string) { const [state, setState] = useState(loginType); function login() { // body of the login function } function oauth() { // body of the oauth function login(); ...

Is there a way to retrieve the Boolean value from an ng-show attribute without having to re-evaluate the expression?

I'm currently working on a project that involves displaying and hiding a lot of content dynamically using ng-show. Some of the expressions being evaluated are quite lengthy, like this... <div ng-show="some.object.with.nested.values && ...

jQuery is working perfectly on every single page, with the exception of just one

Check out my code snippet here: http://jsfiddle.net/9cnGC/11/ <div id="callus"> <div class="def">111-1111</div> <div class="num1">222-2222</div> <div class="num2">333-3333</div> <div class="num3">444-4444< ...

The query in the Mongo shell is not functioning as expected when used with mongoose

I have crafted a shell query that functions flawlessly when executed on mongo shell, but does not yield any results when run using mongoose in nodejs + typescript; Mongo shell db.userworks.aggregate([ { $match: { user: ObjectId("6 ...

Express JS backend causing CSS file to fail to load in React project

After doing some research on Stack Overflow, I came across a few solutions but none of them seemed to work for my specific situation. I am currently attempting to serve my React website from an Express backend server. Here is the layout of my folders: cli ...

Are Gatsby Server-Side Rendering APIs and Next.js SSR the equivalent in functionality?

After mastering Gatsby Js for building an ecommerce website using the SSR method, I am now contemplating between sticking with Gatsby or switching to Next for future scalability as web technology advances and user base expands. Which option would be bett ...

Is it possible to search for a value within an array of objects in MongoDB, where the value may be found in any object within that array?

Here is the schema: {"_id":"_vz1jtdsip", "participants":{ "blue":["finettix"] "red":["EQm"] }, "win":"red"," __v":0} I have multiple documents l ...

Leverage axios over fetch calls within Workbox

As I work on caching content for my project using Workbox, I've successfully saved all my js and css files, fonts, etc. However, I'm facing an issue with caching my project's content stored in my PC. When using axios to fetch data from my da ...