Vue Component-radio button一組

by cactus77kiki

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.0/axios.min.js"></script>

<div id="app">
  【Test1】
  <radio-ele v-bind:selected="selectedRadio" v-on:changedata="getselection"></radio-ele>
  【Test2】
  <radio2-ele v-bind:selected="selectedRadio" v-on:changedata="getselection2"></radio2-ele>
  <br/>
  <h2>Todos:</h2>
  <ol>
    <li v-for="todo in todos">
      <label>
        <input type="checkbox"
          v-on:change="toggle(todo)"
          v-bind:checked="todo.done">

        <del v-if="todo.done">
          {{ todo.text }}
        </del>
        <span v-else>
          {{ todo.text }}
        </span>
      </label>
    </li>
  </ol>
</div>

CSS

body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

#app {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  transition: all 0.2s;
}

li {
  margin: 8px 0;
}

h2 {
  font-weight: bold;
  margin-bottom: 15px;
}

del {
  color: rgba(0, 0, 0, 0.3);
}

JavaScript

Vue.component('radio2-ele', {
	props:['selected'],
  template: `<div>
  					  <div v-for="(l,index) in list">
  						<input type="radio" 
                v-bind:value="l.id"
                v-model="selection"
                v-on:change="$emit('changedata',l)"><label>{{l.text}}</label>
							</div>
             </div>`,  
  data: function () {
    return {
      message: 'Hello World!',
      selection: this.selected,
      list: [],      
    }
  },
  methods: {
    sayHi: function() {
      alert('Hi');
    },
    getData: function(){    	
      var data = 
      [{id: 1, text:'台北', kind:'A'},{id: 2, text:'新北', kind:'A'},{id: 3, text:'基隆', kind:'B'},{id: 4, text:'UI',kind:'C'}];
      this.list = data;
    }    
  },
  created: function(){
  	this.getData();
  }
});
Vue.component('radio-ele', {
	props:['selected'],
  template: `<div>
  					  <div v-for="l in list">
  						<label><input type="radio" 
                v-bind:value="l.id"
                v-model="selection"
                v-on:change="$emit('changedata',$event)">{{l.text}}</label>
							</div>
             </div>`,  
  data: function () {
    return {
      message: 'Hello World!',
      selection: this.selected,
      list: [],      
    }
  },
  methods: {
    sayHi: function() {
      alert('Hi');
    },
    getData: function(){    	
      var data = 
      [{id: 1, text:'前端', kind:'A'},{id: 2, text:'後端', kind:'A'},{id: 3, text:'資料庫', kind:'B'},{id: 4, text:'UI',kind:'C'}];
      this.list = data;
    }    
  },
  created: function(){
  	this.getData();
  }
});
var vapp = new Vue({
  el: "#app",
  data: {
    todos: [
      { text: "Learn JavaScript", done: false },
      { text: "Learn Vue", done: false },
      { text: "Play around in JSFiddle", done: true },
      { text: "Build something awesome", done: true }
    ],
    selectedRadio:'2'
  },
  methods: {
  	toggle: function(todo){
    	todo.done = !todo.done
    },
    getselection: function(event){
    	//僅能回傳value,...