Home > Software design >  How can when i click radio button and label show corresponding value in Vuejs 2
How can when i click radio button and label show corresponding value in Vuejs 2

Time:06-21

I have a piece of data like this and I want to filter it to display by value of radio button. My Data

 data(){
   return{
     checks:['Developer','Tester', 'Designer', 'Support',],
     infojobs:[{
       genres: 'Developer',
       position:'Senior Java Engineer, Big Data',
       exprerience:'3-5 Years',
       salary:'',
       headequarters:'',
       content1:'A',
       content2:'B',
       content3:'C'}]

I need to filter it by job genres and this is my interface:

jobs.vue

            <div >
                <h3>Filter</h3>
                <div  id="group-filter" :>
                    <div >
                        <input type="radio"  name="fav_language" value="">
                        <label for="">Tất cả</label><br>
                    </div>
                    <div  v-for="(check, index) in checks" :key="index">
                        <input type="radio"  name="fav_language" value="" >
                        <label for="">{{check}}</label><br>
                    </div>
                   
                </div>
            </div>
            <div >
                <div >
                    <h5>22 tin tuyển dụng</h5>
                    <div >
                        <input type="search" placeholder="Nhập từ khóa để tìm kiếm">
                        <button><img src="../assets/recruit/search.svg" alt=""></button>
                    </div>
                </div>
                <div  v-for="(item, index) in infojobs" :key="index">
                    <h3 >{{item.position}}</h3>
                    <div >
                        <div >
                            <div>
                                <img src="../assets/recruit/years.svg" alt="">
                                <b>{{item.exprerience}}</b>
                            </div>
                            <div>
                                <img src="../assets/recruit/luong.svg" alt="">
                                <b>{{item.salary}}</b>
                            </div>
                            <div>
                                <img src="../assets/recruit/diadiem.svg" alt="">
                                <b>{{item.headequarters}}</b>
                            </div>
                        </div>
                        <div>
                            <h6>2 ngày trước</h6>
                        </div>
                    </div>
                    <div >
                        <div >
                            <ul>
                                <li>{{item.content1}}</li>
                                <li>{{item.content2}}</li>
                                <li>{{item.content3}}</li>
                            </ul>
                        </div>
                        <a href="/detail">
                            <button  >Xem chi tiết</button>
                        </a>
                    </div>

CodePudding user response:

You can set v-model on your radio inputs and use computed property to filter data:

new Vue({
  el: "#demo",
  data() {
    return {
      checks: ['Developer','Tester', 'Designer', 'Support'],
      infojobs: [{genres: 'Developer', position:'Senior Java Engineer, Big Data',      exprerience:'3-5 Years', salary:'', headequarters:'', content1:'A', content2:'B',   content3:'C'}],
      selected: null,
      display: ''
    }
  },
  computed: {
    filtered() {
      if (this.selected) return this.infojobs.filter(i => i.genres === this.selected)
      return this.infojobs
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
  <div >
    <h3>Filter</h3>
    <div  id="group-filter" :>
      <div >
        <input type="radio"  name="fav_language" v-model="selected" value="">
        <label for="">Tất cả</label><br>
      </div>
      <div  v-for="(check, index) in checks" :key="index">
        <input type="radio" :id="index" :value="check"  name="fav_language" v-model="selected" >
        <label for="">{{check}}</label><br>
      </div>
      {{selected}}
    </div>
  </div>
  <div >
    <div >
      <h5>22 tin tuyển dụng</h5>
      <div >
        <input type="search" placeholder="Nhập từ khóa để tìm kiếm">
        <button><img src="../assets/recruit/search.svg" alt=""></button>
      </div>
    </div>
    <div  v-for="(item, index) in filtered" :key="index">
      <h3 >{{item.position}}</h3>
      <div >
          <div >
            <div>
              <img src="../assets/recruit/years.svg" alt="">
              <b>{{item.exprerience}}</b>
            </div>
            <div>
              <img src="../assets/recruit/luong.svg" alt="">
              <b>{{item.salary}}</b>
            </div>
            <div>
              <img src="../assets/recruit/diadiem.svg" alt="">
              <b>{{item.headequarters}}</b>
              </div>
          </div>
          <div>
            <h6>2 ngày trước</h6>
          </div>
        </div>
        <div >
          <div >
            <ul>
                <li>{{item.content1}}</li>
                <li>{{item.content2}}</li>
                <li>{{item.content3}}</li>
            </ul>
          </div>
          <a href="/detail">
            <button  >Xem chi tiết</button>
          </a>
        </div>
    </div>
  </div>
</div>

  • Related