The method of connecting a Vue click event to Vue tables 2 (JSX)

When using Vue version 2 along with https://github.com/matfish2/vue-tables-2, I'm facing an issue binding the click event on JSX (specifically on templates->edit):

var tableColumns = ['name', 'stock', 'sku', 'price', 'created_at']
var options = {
  compileTemplates: true,
  highlightMatches: true,
  pagination: {
    dropdown: true,
    chunk: 10
  },
  filterByColumn: true,
  texts: {
    filter: 'Search:'
  },
  datepickerOptions: {
    showDropdowns: true
  },
  templates: {
    edit: function (h, row) {
      return <button v-on:click={this.showItem(row.id)} class="btn btn-primary btn-sm"><i class="glyphicon glyphicon-edit"></i></button>
    },
    delete: function (h, row) {
      return <a href="javascript:void(0);" class="btn btn-danger btn-sm"><i class="glyphicon glyphicon-erase"></i></a>
    }
  }
}

export default {
  name: 'ItemList',
  data: function () {
    return {
      options: options,
      columns: tableColumns
    }
  },
  methods: {
    showItem: function (id) {
      console.log(id)
    }
  }
}

I tried changing to JSX on-click, but Vue is not able to recognize it. The .babelrc file already includes:

{
    "presets": ["es2015"],
  "plugins": [
    "transform-runtime",
    "transform-vue-jsx"
  ]
}

Answer №1

Learning this lesson was a challenge for me, as I lacked familiarity with JSX while searching for a solution. Instead of using onClick, opt for on-click.

Here is the corrected code:

ES6

edit: (h, row) => {
  return <button on-click={ () => this.showItem(row) } class="btn btn-primary btn-sm"><i class="glyphicon glyphicon-edit"></i></button>
},

ES5:

edit: function (h, row) {
  return <button on-click={ this.showItem.bind(this, row) } class="btn btn-primary btn-sm"><i class="glyphicon glyphicon-edit"></i></button>
},

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

Error: The module could not be located due to the inability to resolve the path to './router'. This issue is specifically related to Vue router

Looking at the search.js file: import {createRouter, createWebHistory} from "vue-router"; import SearchIndex from './components/omdb/SearchIndex.vue' const routes = [ { path: '/', name: 'welcome&apos ...

The error message "Trying to access an undefined object when trying to navigate using 'this.props.navigation.navigate' in the _onViewApp function at line 17

I've come across this question in several places, but I'm still struggling to find a solution despite the advice given. The majority of the code I have implemented so far is based on the following article - https://medium.com/@austinhale/buildin ...

Encountered a syntax hiccup in my PHP and JavaScript codes

Below is my PHP code snippet: echo ("<td><img src='edit.jpg' width='20' alt='Edit' title='EDIT DATA' onClick=\"swipe2('" + . mysql_result($result, $i, 'no'). + '');'style= ...

Click on the nearest Details element to reveal its content

Is there a way to create a button that can open the nearest details element (located above the button) without relying on an ID? I've experimented with different versions of the code below and scoured through various discussions, but I haven't be ...

Transform the features of a website into a sleek iOS application using Swift

Can I take an HTML website coded with JavaScript and integrate its functionality into my app using WebKit or another method? By using WebKit or WebViews, I can load an entire webpage into my app, automatically bringing along its functionality. However, i ...

JavaScript Selenium code encountering an "Element not interactable" error with input textbox element

Currently, I am attempting to utilize Selenium in order to automate inputting a location into the search bar on weather.com. Initially, I wrote the code in Python and it seems to be functioning properly: // this works driver = webdriver.Chrome(ChromeDriver ...

What is the significance of "new" being omitted in the upcoming jQuery script?

After diving into the JQuery documentation, I discovered that it is possible to create the event object like this: //Instantiate a new jQuery.Event object without using the "new" keyword. var e = jQuery.Event( "click" ); I find it puzzling why we don&apo ...

Error message: The variable datepicker_instActive is not defined within Jquery-ui Datepicker

Having trouble with a Rails + Angular app where I've implemented the jquery-ui datepicker. The console is showing an error that says: TypeError: datepicker_instActive is undefined if(!$.datepicker._isDisabledDatepicker( datepicker_instActive.inline? ...

Evaluation of HTML5 file upload functionality with unit testing

How can I perform JavaScript unit testing for file uploads using the HTML 5 File API? Let's consider the following code: <form method="POST" enctype="multipart/form-data"> <input type="file" id="fileselec ...

Having trouble getting my subarrays to push correctly into the parent array in ReactJS. What am I doing wrong

I am currently working on implementing the bubblesort algorithm using ReactJS. My state includes an array of 3 objects initially sorted in descending order by the 'num' property. I have a button on my interface that triggers the bubblesort functi ...

ASP.net is encountering difficulty locating a specific JavaScript function

Seeking assistance to comprehend the issue, I have devoted countless hours scouring Google for a resolution. Current tools in use: ASP.NET entity framework Within a view, the following code is utilized: <button type="button" id="btnGraf ...

Vue, the versatile filtering tool for data tables in HTML

I am currently using Vue to populate an HTML table with data retrieved from an axios call. The table layout is perfect, but I am interested in finding a way (without converting the entire structure to datatables) to enable filtering on each column header. ...

What is the best way to attach functions to specific jQuery objects and exclude others?

Imagine having an unordered list <ul>: <ul class="products"> ... </ul> You want to use jQuery to select it and then add custom functions to that specific object. For instance, you wish to include an addProduct(productData) function ...

Troubleshooting a 404 error for an existing object: What to do?

I encounter a 404 'Not Found' error when attempting to edit a mark through my form. I am puzzled by the source of this error because in order to access this form, I require the brand ID (which can be found in the URL). Upon accessing my modifica ...

Navigating through a 3D world with just the tilt of

I am currently developing an immersive VR web application using three.js. I have integrated the device orientation controls from the Google Cardboard three.js demo for camera navigation. Now, I am looking to incorporate keyboard controls for additional fu ...

Tips for incorporating 'and' in the 'on' clause of 'join' in knex.js

I need assistance implementing the following SQL code in knex.js: select c.id,c.parent_id,c.comment,u.username,c.postid from comments as c join post_details as p on (p.id = c.postid and c.postid=15)join users as u on (u.id = c.userid); I attempt ...

The event listener for 'ended' is triggering multiple times

I am in the process of developing a music app, and my goal is to have the next song automatically play when the current one ends. However, I am facing an issue where the EventListener seems to be triggered multiple times in succession - first once, then tw ...

Formatting time on the x-axis in a Chart.js graph

I've been attempting to create a scatter plot with Chart.js using some data, but no matter what I try from various internet resources, the x-axis continues to display as integers instead of dates. Here's a screenshot for reference. UPDATE: I dis ...

Limiting the Size of Highcharts Maps with maxWidth or maxHeight

My attempt to adjust the maxWidth of my world map using highcharts isn't altering its size. I have experimented with the following: responsive: { rules: [{ condition: { maxWidth: 500 }, ... As recomme ...

Which HTML element does each script correspond to?

Are there any methods to identify the script used in certain HTML elements? For instance, if I wish to determine the script responsible for creating a drop-down menu, I can locate the HTML and CSS for the menu but not the JavaScript (or other scripts). I ...