javascript - How to bind vue click event with vue tables 2 (JSX)? - Stack Overflow

Using , with Vue version 2, I can't seem to bind the click event on JSX (on templates->edit):va

Using , with Vue version 2, I can't seem to bind the click event on JSX (on templates->edit):

var tableColumns = ['name', 'stock', 'sku', 'price', 'created_at']
var options = {
  pileTemplates: 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)
    }
  }
}

Changed to JSX on-click, but Vue cannot recognize that. .babelrc already has:

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

Using https://github./matfish2/vue-tables-2, with Vue version 2, I can't seem to bind the click event on JSX (on templates->edit):

var tableColumns = ['name', 'stock', 'sku', 'price', 'created_at']
var options = {
  pileTemplates: 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)
    }
  }
}

Changed to JSX on-click, but Vue cannot recognize that. .babelrc already has:

{
    "presets": ["es2015"],
  "plugins": [
    "transform-runtime",
    "transform-vue-jsx"
  ]
}
Share Improve this question edited Dec 1, 2017 at 17:43 Matanya 6,3469 gold badges49 silver badges82 bronze badges asked Oct 2, 2016 at 13:07 user2002495user2002495 2,1468 gold badges31 silver badges62 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 8

I learned this the hard way, since I'm not familiar with JSX when I was looking for the solution for this. However, don't use onClick, but on-click instead.

Here you go:

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>
},

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742383158a4433522.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信