Unspecified functionalities within VueJs

data() {
    return {
        arrayToBeSorted: [1, 2, 3, 4, 5, 6, 7, 8],
        isManipulatingArray: false,

        shuffleButtonText: "Shuffle!",

        sortAlgorithms: [
            { id: 1, name: "Bubble sort", fn: bubbleSort },
            { id: 2, name: "Selection sort", fn: selectionSort },
            { id: 3, name: "Insertion sort", fn: insertionSort },
        ],
    }
  },
  methods: {
      sort: function (Algorithm) {
          this.Algorithm.fn();
      },
      show: function() {
        console.log("shuffle!");
      },
      bubbleSort: function() {
        console.log("bubble!");
      },
      selectionSort: function() {
        console.log("selection!");
      },
      insertionSort: function() {
        console.log("insertion!");
      },
  }
<div class="buttons-container">
         <button class="button" v-for="Algorithm in sortAlgorithms" :key="Algorithm.id" v-on:click="Algorithm.fn">{{ Algorithm.name }}</button>
</div>

Upon clicking the button, I expect the corresponding method to be triggered using the sort function. However, an error is occurring stating that the function is not defined.

Answer №1

Consider utilizing the this context

Find more information here

new Vue({
  el: '#app',
  data() {
    return {
      arrayToBeSorted: [1, 2, 3, 4, 5, 6, 7, 8],
      isManipulatingArray: false,

      shuffleButtonText: "Shuffle!",

      sortAlgorithms: [{
          id: 1,
          name: "Bubble sort",
          fn: this.bubbleSort
        },
        {
          id: 2,
          name: "Selection sort",
          fn: this.selectionSort
        },
        {
          id: 3,
          name: "Insertion sort",
          fn: this.insertionSort
        },
      ],
    }
  },
  methods: {
    sort: function(Algorithm) {
      Algorithm.fn();
    },
    show: function() {
      console.log("shuffle!");
    },
    bubbleSort: function() {
      console.log("bubble!");
    },
    selectionSort: function() {
      console.log("selection!");
    },
    insertionSort: function() {
      console.log("insertion!");
    },
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
  <div class="buttons-container">
    <button class="button" v-for="Algorithm in sortAlgorithms" :key="Algorithm.id" v-on:click="Algorithm.fn">{{ Algorithm.name }}</button>
  </div>
</div>

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

Multiple ngFor loops causing only the final item to be displayed in the inner loop

Can someone assist with my code where I loop through firebase RTDB reference to retrieve a list and then use those results in a subsequent firestore query? The console logs the correct data, but my code only displays the last item in the loop inside ngFor. ...

Vue - template or render function is not declared

Just a quick question, but I'm having trouble figuring out what's going wrong; Set up a vue application with webpack, but now I'm seeing this error: "[Vue warn]: Failed to mount component: template or render function not defined.&quo ...

Performing a subtraction operation using HTML5 and JavaScript

While I wish I could write about something more exciting than value subtraction, unfortunately that's not the case. In a game scenario where asteroids are approaching the player, who has the ability to move around, the intention is for the player&apos ...

Tips for integrating a fully revamped design for social network share buttons

Is it possible to customize social network like buttons similar to the ones shown in the attached image? ...

Ensure Angular Reactive Forms do not include empty fields when submitting the form

Is there a way to retrieve only the fields with values entered by the user from a form and exclude empty fields in the resulting JSON object? Currently, the JSON object still includes empty quotation marks for empty inputs. Thank you! Current: { "user ...

The functionality of Skrollr on mobile is currently not working properly due to the inability to prevent default actions

Encountering the following error on all chromium browsers while using the Skrollr library and accessing the website through mobile: "[Intervention] Unable to preventDefault inside passive event listener due to target being treated as passive. See < ...

Is it possible to execute the 'min' task in Grunt multiple times?

Is there a way to execute a task twice with different configurations in Grunt? For example, I have two groups of source files in my project that I want to minify into separate output files. The file structure looks like this: project srcA file ...

The error message "consts is not defined in React Carousel renderArrow" indicates

While working with the react-elastic-carousel package, I encountered an issue when attempting to implement my own custom arrows. This is the code snippet I used, which was borrowed from the documentation: function App() { return ( <div> ...

What is the process for retrieving the result of a promise at a later time?

var screencastId = 'abc' var a = youtube.get(screencastId); a.then(function(screencast) { // Great, the screencast information is now available. console.log(screencast); }); // How can I access the `screencast` variable below? connection.be ...

React doesn't properly display JSON data

I am facing an issue with the code below that is supposed to display data from a JSON file. Instead of showing the desired data, it only displays the h1 with curly braces. Here is the faulty code: import prod from "../../data/produtos.json"; exp ...

Socket.io is most effective when reconnecting

I am currently in the process of developing a React application that connects to a Node.js API and I am trying to integrate the Socket.io library. Following various online tutorials, my code now looks like this: API: import express from 'express&apo ...

Access the menu featuring a pair of distinct buttons

I have a simple dropdown menu that can be triggered by clicking on the icon NewIcon in DropdownMenu.Trigger. Implementing this dropdown menu is straightforward, as I am using the radix library () <DropdownMenu.Root open={isOpen} onOpenChange={setIsOpen ...

The function get_template_directory_uri() returned an unexpected string error

Exploring WP themes has been an interesting journey for me. Currently, I am working on creating a shortcode for some html/script and encountering an issue with enqueuing the js file. My initial query is about whether I am loading this from the correct loc ...

Grunt Watch is currently not executing any tasks for JavaScript or CSS

I'm encountering an issue with my grunt-watch not running for the JS or CSS configurations The Grunt file is located in a 'tools' folder. The project structure is as follows: -index.html -js -css -tools --package.json --node modules --grun ...

Connecting two fields with a line on click in AngularJS

In the top section, there are 10 fields and in the bottom section, there are another 10 fields. I want to be able to connect two fields with a line when they are clicked (one from the top section and one from the bottom section). This connection should app ...

Error: Property ID was not followed by a colon

There is a Syntax Error: missing : after property id near '<img src="loading.gif" />';` <script> $(document).ready(function() { $("#client").on("change", function() { var clientid=$("#client").val(); ...

The state of XMLHttpRequest always remains in a perpetual state of progress, never

I have come across an MVC Core application. One of the methods in this application currently has the following structure: public IActionResult Call(string call) { Response.ContentType = "text/plain"; return Ok(call); } In addi ...

I am having trouble inserting a table from a JSON object into an HTML file

getJSON('http://localhost:63322/logs', function(err, data) { if (err !== null) { alert('Something went wrong: ' + err); } else { //var myObj = JSON.parse(data); // document.getElementById("demo").innerHTML = myObj.ad_soy ...

Struggling to change the div content with ajax response transmitted through php

I would like to apologize in advance if this question has already been addressed elsewhere. After conducting a search, I came across several relevant posts that have guided me to this point. Below is the form snippet containing 1 input box, a button, and ...

Redirecting from HTTP to HTTPS with node.js/Express

Are there steps I can take to modify my web application to operate on HTTPS instead of HTTP using node.js/express? I require it to run on HTTPS due to the use of geolocation, which Chrome no longer supports unless served from a secure context like HTTPS. ...