Vue从表行获取id

时间:2017-10-28 14:30:40

标签: vue.js

如何在此表格行中获取所选项目。我想要当我点击该行时它给了我cowId然后我可以使用它。我有没有办法以一种非常简单的方式做到这一点,而无需太多修改。然后我可以通过axios发送thecowwid到我的api删除

    <div id="ArtificialInsemination" class="container">
        <button v-on:click="viewRecords">View Record</button>
        <table class="table table-striped">
            <thead>
            <tr>
                <th>Cow Id</th>
                <th>Bull Name</th>
                <th>Semen Origin</th>
                <th>Insemination Time</th>
                <th>Pd Delivery Date</th>
                <th>Actions</th>
            </tr>
            </thead>
            <tbody>
            <tr v-for ="artificialInseminationRecord in artificialInseminationRecords">
                <td>{{ artificialInseminationRecord.cowId }}</td>
                <td>{{ artificialInseminationRecord.bullUsedName }}</td>
                <td>{{ artificialInseminationRecord.semenOrigin }}</td>
                <td>{{ artificialInseminationRecord.inseminationTime }}</td>
                <td>{{ artificialInseminationRecord.pdStatusDate }}</td>
                <td><button v-on:click="DeleteArtificialInseminationRecords" >Delete</button></td>
            </tr>
            </tbody>
        </table>
    </div>

当表格中的某一行点击

时,此VUE获取COW ID
<script>
    //class initialization

    var ArtificialInsemination = new Vue({
        el:'#ArtificialInsemination',
        data: {
            url:'http://localhost/dairyfarm/index.php',
            artificialInseminationRecords: [],
            cowId: ''

        },
        //invoke methods
        methods: {
          viewRecords:function () {
              var data = new FormData()
              data.append('function','viewRecords')
              axios.post(this.url,data)
                  .then( function (response ) {
                  this.artificialInseminationRecords = response.data.data
              }.bind(this)).catch(function (error) {

              })

            }, saveInseminationRecords:function () {
              var data = new FormData()
              data.append('function','saveInseminationRecords')
              axios.post(this.url,data)
                  .then( function (response ) {
                  this.artificialInseminationRecords = response.data.data
              }.bind(this)).catch(function (error) {

              })

            }, DeleteArtificialInseminationRecords:function () {
                this.cowId = 'GET COW ID HERE'
              var data = new FormData()
              data.append('function','DeleteArtificialInseminationRecords')
                data.append('cowId',this.cowId)
              axios.post(this.url,data)
                  .then( function (response ) {
              }.bind(this)).catch(function (error) {

              })

            },
            create: function(){
              this.viewRecords()
            }.bind(this),
        }
    })

</script>

1 个答案:

答案 0 :(得分:2)

完整的例子。希望它有所帮助。

&#13;
&#13;
const store = new Vuex.Store({
  state: {
    users: null
  },
  mutations: {
    updateUsers (state, payload) {
      state.users = payload
    }
  },
  actions: {
    async loadUsers ({commit}, payload) {
      var response = await axios.get(payload.src)
      commit('updateUsers', response.data )
    }
  }
})

Vue.component('user-list', {
  template: '#user-list',
  props: ['src'],
  methods: {
    removeUser (id) {
      alert('You are deleting user id: ' + id)
      // axios.delete('https://your.rest.api/users/' + id)
    }
  },
  created () {
    this.$store.dispatch('loadUsers', {src: this.src})
  }
})

new Vue({
  el: '#app',
  store
})
&#13;
table {
  border: 1px solid black;
  border-collapse: collapse;
}
th, td {
  border: 1px solid black;
  padding: 3px;
}
td:last-child {
  text-align: center;
}
&#13;
<div id="app">
  <user-list src="https://jsonplaceholder.typicode.com/users">
  </user-list>
</div>

<template id="user-list">
  <table>
    <thead>
      <tr>
        <th>ID</th>
        <th>Nick</th>
        <th>Full name</th>
        <th>Actions</th>
      </tr>
    </thead>
    <tr v-for="user in $store.state.users" :key="user.id">
      <td>{{ user.id }}</td>
      <td>{{ user.username }}</td>
      <td>{{ user.name }}</td>
      <td><button @click="removeUser(user.id)">X</button></td>
    <tr>
  </table>
</template>

<script src="https://unpkg.com/vue@2.5.2/dist/vue.min.js"></script>
<script src="https://unpkg.com/vuex@3.0.0/dist/vuex.min.js"></script>
<script src="https://unpkg.com/axios@0.17.0/dist/axios.min.js"></script>
&#13;
&#13;
&#13;