2017-06-20 354 views
1

後的數組元素的更新值是否有可能,當我火updateCountry方法(在國家名單組件定義),更新值(如果成功調用)該數組元素,這樣該按鈕就會根據country.show的值動態改變文本的值?Vue公司2 - click事件

這是Vue的代碼,我目前所面對的:

Vue.component('country-list', { 
    template: ` 
    <tbody> 
     <tr is="country" v-for="(country, index) in countries.data"> 
      <td> 
       <input 
        type="text" 
        name="name" 
        class="form-control country-name" 
        :value="country.name" 
       > 
      </td> 
      <td> 
       <select name="show" class="form-control country-show" :value="country.show"> 
        <option value="0">No</option> 
        <option value="1">Yes</option> 
       </select> 
      </td> 
      <td> 
       <input 
        type="text" 
        name="order" 
        class="form-control country-order" 
        :value="country.order" 
       > 
      </td> 
      <td> 
       <button class="btn btn-primary"> 
        {{ country.show ? "Hide" : "Show" }} 
       </button> 
       <button class="btn btn-success" 
        @click="updateCountry" 
        :data-id="country.id">Update</button> 
      </td> 
     </tr> 
    </tbody> 
    `, 

    props: ['countries'], 

    methods: { 

     updateCountry(event) { 

      let self   = this; 

      let countryID = event.target.dataset.id; 

      let parent  = event.target.closest('.parent'); 

      let countryName = parent.getElementsByClassName('country-name')[0].value; 

      let countryOrder = parent.getElementsByClassName('country-order')[0].value; 

      let countryShow = parent.getElementsByClassName('country-show')[0].value; 

      axios.post('/country/insert', { 
       id: countryID, 
       name: countryName, 
       order: countryOrder, 
       show: countryShow 
      }) 
      .then(function (response) { 
       console.log(self); 
      }) 
      .catch(function (error) { 
       console.log(error); 
      }); 

     } 

    } 
}); 

Vue.component('country', { 
    template: `<tr class=parent><slot></slot></tr>` 
}); 


Vue.component('pagination-list', { 

    template: ` 
     <tfoot> 
      <tr align="center"> 
       <nav aria-label="Page navigation"> 
        <ul class="pagination"> 
         <li :class="countries.current_page == 1 ? 'disabled' : ''"> 
          <a 
           :class="countries.current_page == 1 ? 'disabled' : ''" 
           :href="countries.current_page == 1 ? '#' : countries.prev_page_url" 
           @click.prevent="pagination(countries.current_page - 1)" 
           aria-label="Previous"> 
           <span aria-hidden="true">&laquo;</span> 
          </a> 
         </li> 

         <li v-for="i in countries.last_page" 
         :class="countries.current_page == i ? 'active' : ''" 
         > 
          <a 
           :href="countries.current_page == i ? '#' : '/admin/countries?page='+i" 
           @click.prevent="pagination(i)" 
          >{{i}}</a> 
         </li> 

         <li> 
          <a 
           :href="countries.next_page_url" 
           @click.prevent="pagination(countries.current_page + 1)" 
           aria-label="Next"> 
           <span aria-hidden="true">&raquo;</span> 
          </a> 
         </li> 
        </ul> 
       </nav> 
      </tr> 
     </tfoot> 
    `, 

    props: ['countries'], 

    methods: { 
     pagination(page) { 
      this.$parent.getCountries(page); 
     } 
    } 
}); 


let App = new Vue({ 

    el: '#app-container', 

    data: { 
     countries: [] 
    }, 

    created() { 
     this.getCountries() 
    }, 

    methods: { 

     getCountries(page) { 

      let self = this; 

      let getParam = page ? '?page=' + page : ''; 

      axios.get('/admin/countries' + getParam) 
       .then(function (response) { 
        self.countries = response.data; 
       }) 
       .catch(function (error) { 
        console.log(error); 
       }); 

     }, 

     filterCountries(event) { 

      let name = event.target.value; 

      let self = this; 

      if(name.length > 2) { 

       axios.get('/country/search', { 
        params: { 
         name: name 
        } 
       }) 
       .then(function (response) { 
        self.countries = response.data; 

        console.log(self.countries); 
       }) 
       .catch(function (error) { 
        console.log(error); 
       }); 

      } 

      if((event.keyCode === 8 && name.length === 2) || !name.length){ 
       this.getCountries(); 
      } 
     } 
    } 

}) 
+0

你的意思是更新'country.show'的值嗎? – Bert

+0

可以是整個國家的數組,也可以是僅用於更新的數組元素。任何會將按鈕的文本從隱藏變爲顯示或反之亦然的東西。 – Sasha

回答

1

一樣,如果你使用v-model,它會削減一些你不得不做的事情,此代碼會更加Vue公司。例如,如果您更新country-list模板是這樣的:

<tbody> 
    <tr is="country" v-for="(country, index) in countries.data" :key="country"> 
     <td> 
      <input 
       type="text" 
       name="name" 
       class="form-control country-name" 
       v-model="country.name" 
      > 
     </td> 
     <td> 
      <select name="show" class="form-control country-show" v-model="country.show"> 
       <option value="0">No</option> 
       <option value="1">Yes</option> 
      </select> 
     </td> 
     <td> 
      <input 
       type="text" 
       name="order" 
       class="form-control country-order" 
       v-model="country.order" 
      > 
     </td> 
     <td> 
      <button class="btn btn-primary"> 
       {{ country.show ? "Hide" : "Show" }} 
      </button> 
      <button class="btn btn-success" 
       @click="updateCountry(country)" 
       :data-id="country.id">Update</button> 
     </td> 
    </tr> 
</tbody> 

然後你updateCountry方法可能只是這個

updateCountry(country) { 
    axios.post('/country/insert', country) 
    .catch(err => //do something on error) 
} 

因爲使用v-model,所有值都已經本地更新,和你只需將值發佈到服務器即可。由於您將實際的country傳遞給updateCountry方法,因此不需要從輸入中獲取值。

另請注意,我將:key="country"添加到您的v-for,因爲迭代組件時需要密鑰。如果你有一個country.id,那就更好了。但是,我不明白你爲什麼需要country組件。這一點完全沒有必要。

+0

我是Vue的新手(幾天前開始使用它,仍然在學習它的背後學習)(jquery是我去js庫多年,而Vue沒有像Jquery))。非常感謝今天幫助我:)。這是一個漫長的一天,你的幫助使它變得更容易:) – Sasha

+0

@Sasha你會選擇它:) Vue使很多事情對你更容易。樂於幫助。 – Bert