Vue 선택, v-for 및 v-model이 포함된 값 사전 선택
사용하고 있다.select
와 함께v-model
그리고 선택권을 가지고 있다v-for
그리고 가치로써의 목적.옵션은 ID로 식별되는 일부 요소들이다.사용자 정의 동일성에 따라 옵션을 미리 선택하는 방법(이 경우 동일)id
들판)?앵글제비 비슷한 것을 찾고 있다.track by
로부터ng-options
.
https://jsfiddle.net/79wsf1n4/5/
id가 같은 값으로 미리 선택한 입력을 만드는 방법?
템플릿:
<div id="vue-instance">
<select v-model="selected">
<option v-for="item in inventory" :value="item" :key="item.id">
{{ item.name }}
</option>
</select>
<p>
{{ selected.id }}
</p>
</div>
js:
var vm = new Vue({
el: '#vue-instance',
data: {
inventory: [
{name: 'MacBook Air', id: 1},
{name: 'MacBook Pro', id: 2},
{name: 'Lenovo W530', id: 3},
{name: 'Acer Aspire One', id: 4}
],
selected: {
id: 2
}
}
});
Dharmendra의 답변에 따라 선택한 속성을 추가할 수 있다.
그러나 문제는 선택한 속성에 유효한 개체를 할당하지 않고 있다는 점이다.Vue는 옵션 목록에서 동일한 개체를 찾으려고 할 것이며, 개체 동일성 비교를 통해 이 작업을 수행할 것이다.
현재 나는 Vue에게 속성에 기초하여 초기 선택을 하도록 지시하는 것이 가능한지 알 수 없지만, 매우 간단한 해결책은 생성된 라이프사이클 콜백에서 직접 ID를 기반으로 선택한 속성을 할당하는 것이다.
var vm = new Vue({
el: '#vue-instance',
data: {
inventory: [
{name: 'MacBook Air', id: 1},
{name: 'MacBook Pro', id: 2},
{name: 'Lenovo W530', id: 3},
{name: 'Acer Aspire One', id: 4}
],
selected: 2
},
created: function() {
this.selected = this.inventory.find(i => i.id === this.selected);
}
});
네 피들 업데이트도 했어.
옵션에 값을 할당하는 방법을 변경해야 한다.이것을 바꾸다:value="item"
로v-bind:value="item"
.
HTML 부품
<select class="form-control" name="template" v-model="selected">
<option v-for="item in inventory" v-bind:value="item">
{{ item.name }}
</option>
</select>
JS 부분
new Vue({
el: '#selector',
data: {
...
selected: {name: 'MacBook Pro', id: 2}
}
})
여기 일하는 사람이 있다.
동작을 테스트하는 데 필요 없이 유용한 코드가 많이 있다(경우 설정 필요).initSelect
와 함께id
또는name
) :
var vm = new Vue({
el: '#vue-instance',
data: {
inventory: [
{name: 'MacBook Air', id: 1},
{name: 'MacBook Pro', id: 2},
{name: 'Lenovo W530', id: 3},
{name: 'Acer Aspire One', id: 4}
],
initSelect : {
id: 2
},
selected: {},
},
created: function() {
this.setSelectedWithInit();
},
methods: {
setSelectedWithInit: function() {
var firstKey = Object.keys(this.initSelect)[0],
value = this.initSelect[firstKey];
for( var i = 0, length = this.inventory.length; i < length; i++ ) {
if( (this.inventory[i].hasOwnProperty(firstKey))
&& (this.inventory[i][firstKey] === value) )
{
this.selected = this.inventory[i];
return;
}
}
},
toggle: function() {//you don't need this method
if( this.initSelect.hasOwnProperty('id') ) {
this.initSelect = { name: 'Lenovo W530' };
} else {
this.initSelect = { id: 2 };
}
}
},
watch: {
'initSelect': {
handler: function (val, oldVal) {
this.setSelectedWithInit();
},
deep: true
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.2.5/vue.js"></script>
<div id="vue-instance">
<select v-model="selected">
<option v-for="item in inventory" :value="item" :key="item.id">
{{ item.name }}
</option>
</select>
<p>
{{ initSelect.id }}
{{ initSelect.name }}
</p>
<!-- You don't need the rest of the html below -->
<button @click="toggle">Toggle id/name</button>
<div v-if="initSelect.hasOwnProperty('id')">
Id: <input v-model.number="initSelect.id" type="number">
</div>
<div v-else>
Name: <input v-model="initSelect.name" type="text">
</div>
<br><br>
{{ initSelect }}
</div>
참조URL: https://stackoverflow.com/questions/43366723/vue-preselect-value-with-select-v-for-and-v-model
'Programing' 카테고리의 다른 글
선택 입력 /w Vuex 저장소 바인딩에 의한 동적 구성 요소 (0) | 2022.04.14 |
---|---|
표준 라이브러리를 사용하여 정렬된 메모리만 할당하는 방법 (0) | 2022.04.14 |
문자 배열 선언에서 문자열 리터럴 주위에 있는 브레이스가 유효한가?(예: char s[] = {"Hello World"}) (0) | 2022.04.14 |
Vue+webpack+vue-loader 프로젝트의 다른 js 파일에서 기능을 가져오는 방법 (0) | 2022.04.14 |
Vuex store getter는 항상 false를 반환함 (0) | 2022.04.14 |