Bootstrap-Vue模态:始终删除最后一个ID

时间:2019-03-04 21:18:19

标签: mysql node.js vue.js axios

我的应用程序中有一个作业页面,每次尝试删除作业时,我都会尝试使用其ID来删除作业,它正在删除列表中的最后一个ID,而不是在此处选择的ID未被删除。我正在使用REST API执行删除操作,即使单击列表中的第一个ID,它也会在删除时显示最后一个ID。

这是工作详细信息页面

<template>
 <div>
    <h2 class="mb-4 font-weight-light">Job Postings</h2>
    <div class="d-flex align-items-center justify-content-between">
      <b-input-group class="w-30">
        <b-form-input v-model="filter" placeholder="Type to Search" />
        <b-input-group-append>
          <b-btn :disabled="!filter" @click="filter = ''">Clear</b-btn>
        </b-input-group-append>
      </b-input-group>
      <b-button variant="primary" class="d-flex align-items-center" v-b-modal.addJob><i class="material-icons mr-1"></i> Add Job</b-button>
    </div>
    <b-table responsive hover :items="jobs" :fields="fields" :filter="filter" no-sort-reset sort-by="postedOn" :sort-desc="true" class="mt-3 f6">
        <template slot="job_postingURL" slot-scope="data">
          <a class ="pink darken-3" :href="`${data.value}`" target="_blank">{{ data.value }}</a>   
        </template>
       <template slot="modify" slot-scope="row">
       <div slot="modal-footer" class="w-100">
        <div class="" > 
            <div>
              <b-button @click="showModal" variant="danger">Delete</b-button>
              <b-modal ref="myModalRef" hide-footer hide-header>
              <div>
                  <h3 class="font-weight-light">Do you want to delete this job?</h3>
              </div>
              <div class="float-right pt-4">
                <b-btn type="submit" variant="outline-danger"  @click="deleteJob(row.item.ID)">Delete</b-btn> 
             </div>
             <div class="float-right pr-2 pt-4">
               <b-btn  type="submit" variant="outline-danger"  style="padding-left: 10px" @click="hideModal">Cancel</b-btn>
             </div>
              </b-modal>
            </div>
        </div>
        </div>
       </template>
    </b-table>
    <add-job></add-job>
</div>
</template>

<script>
import AddJob from '@/components/jobs/AddJob'
import JobService from '../../services/JobService'
import axios from 'axios'
import { mapGetters } from 'vuex'
import { orderBy } from 'lodash'
export default {
  components: {
    AddJob
  },
    data () {

        return {
            fields: [
              { Key: 'ID', label: 'ID', sortable: false},
              { key: 'job_title', label: 'Job title', sortable: true },
              { key: 'job_name', label: 'Company name', sortable: true },
              { key: 'job_location', label: 'Location', sortable: true },
              { key: 'job_postingURL', label: 'Job posting URL', sortable: false },
              { key: 'job_postingOn', label: 'Posted on', sortable: true},
              { key: 'job_postingBy', label: 'Posted by', sortable: true },
              { key: 'modify', sortable: true}
            ],
            filter: null,
            jobs: [
              {  
                  ID: this.ID,           
                  job_title: '',
                  job_name: '',
                  job_location: '',
                  job_postingURL: '',
                  job_postingOn: '',
                  job_postingBy: ''
              },
          ],
          active: false,
      value: null,
        }
    },
    // this method is to get the data from database
   async created () {
    try {
      this.jobs = await JobService.getJobs();
    } catch(err) {
      this.error = err.message;
    }
  },
  computed: {
        ...mapGetters(['firstName', 'lastName'])
    }, 
  methods: {
    showModal() {
        this.$refs.myModalRef.show()
      },
     focusMyElement (e) {
      this.$refs.focusThis.focus()
    },

    hideModal () {
      this.$root.$emit('bv::hide::modal','myModal')
      this.$refs.myModalRef.hide()
    },
      deleteJob (ID) {
        axios.delete(`http://localhost:5000/api/jobs/${ID}`)
          .then((res) => {
            this.job_title = ''
            this.job_name = ''
            this.job_location = ''
            this.job_postingURL = ''
            this.job_postingOn = ''
            this.job_postingBy = ''
            this.getJobs()
          console.log(res)
          })
          .catch((err) => {
            console.log(err)
          })
      },
      onCancel () {
        this.value = 'Cancle'
      }
  }
}
</script>

1 个答案:

答案 0 :(得分:2)

您正在生成多个模态,为作业中的每个项目生成一个模态,但是具有相同的引用。

当您打开模式以确认删除时,使用的是最后一个,因此您批准删除最后一个项目。

您只能在循环外部创建一个模态,并使用ID作为参数。单击删除按钮时,请设置ID。

下面是更新的代码。

对操作码的更改:

  • b-modal移动到模板顶部
  • ID添加为组件数据,它将用于在删除按钮和确认模式之间进行通信
  • 更改了showModal的签名以接收您要删除的作业的ID
  • 更改了deleteJob的签名以使用组件实例中的ID

<template>
 <!--moved modal at the top-->
 <b-modal ref="myModalRef" hide-footer hide-header>
   <div>
    <h3 class="font-weight-light">Do you want to delete this job?</h3>
   </div>
   <div class="float-right pt-4">
    <b-btn type="submit" variant="outline-danger"  @click="deleteJob">Delete</b-btn> 
   </div>
   <div class="float-right pr-2 pt-4">
    <b-btn  type="submit" variant="outline-danger"  style="padding-left: 10px" @click="hideModal">Cancel</b-btn>
    </div>
 </b-modal>

 <div>
    <h2 class="mb-4 font-weight-light">Job Postings</h2>
    <div class="d-flex align-items-center justify-content-between">
      <b-input-group class="w-30">
        <b-form-input v-model="filter" placeholder="Type to Search" />
        <b-input-group-append>
          <b-btn :disabled="!filter" @click="filter = ''">Clear</b-btn>
        </b-input-group-append>
      </b-input-group>
      <b-button variant="primary" class="d-flex align-items-center" v-b-modal.addJob><i class="material-icons mr-1"></i> Add Job</b-button>
    </div>
    <b-table responsive hover :items="jobs" :fields="fields" :filter="filter" no-sort-reset sort-by="postedOn" :sort-desc="true" class="mt-3 f6">
        <template slot="job_postingURL" slot-scope="data">
          <a class ="pink darken-3" :href="`${data.value}`" target="_blank">{{ data.value }}</a>   
        </template>
       <template slot="modify" slot-scope="row">
       <div slot="modal-footer" class="w-100">
        <div class="" > 
            <div>
              <b-button @click="showModal(row.item.ID)" variant="danger">Delete</b-button>
            </div>
        </div>
        </div>
       </template>
    </b-table>
    <add-job></add-job>
</div>
</template>

<script>
import AddJob from '@/components/jobs/AddJob'
import JobService from '../../services/JobService'
import axios from 'axios'
import { mapGetters } from 'vuex'
import { orderBy } from 'lodash'
export default {
  components: {
    AddJob
  },
    data () {

        return {
            fields: [
              { Key: 'ID', label: 'ID', sortable: false},
              { key: 'job_title', label: 'Job title', sortable: true },
              { key: 'job_name', label: 'Company name', sortable: true },
              { key: 'job_location', label: 'Location', sortable: true },
              { key: 'job_postingURL', label: 'Job posting URL', sortable: false },
              { key: 'job_postingOn', label: 'Posted on', sortable: true},
              { key: 'job_postingBy', label: 'Posted by', sortable: true },
              { key: 'modify', sortable: true}
            ],
            filter: null,
            jobs: [
              {  
                  ID: this.ID,           
                  job_title: '',
                  job_name: '',
                  job_location: '',
                  job_postingURL: '',
                  job_postingOn: '',
                  job_postingBy: ''
              },
          ],
          active: false,
          value: null,
          ID: null
        }
    },
    // this method is to get the data from database
   async created () {
    try {
      this.jobs = await JobService.getJobs();
    } catch(err) {
      this.error = err.message;
    }
  },
  computed: {
        ...mapGetters(['firstName', 'lastName'])
    }, 
  methods: {
    // added id param to showModal
    showModal(id) {
        this.ID = id
        this.$refs.myModalRef.show()
      },
     focusMyElement (e) {
      this.$refs.focusThis.focus()
    },

    hideModal () {
      this.$root.$emit('bv::hide::modal','myModal')
      this.$refs.myModalRef.hide()
    },
      // delete job relies on ID being set before the call
      deleteJob () {
        axios.delete(`http://localhost:5000/api/jobs/${this.ID}`)
          .then((res) => {
            this.job_title = ''
            this.job_name = ''
            this.job_location = ''
            this.job_postingURL = ''
            this.job_postingOn = ''
            this.job_postingBy = ''
            this.getJobs()
          console.log(res)
          })
          .catch((err) => {
            console.log(err)
          })
      },
      onCancel () {
        this.value = 'Cancle'
      }
  }
}
</script>