반응형
Vue.js를 사용하여 대체 버튼을 클릭하여 대체 약품을 사용하고 싶다.
여기 제 코드가 있습니다.제가 묻고 있는 것의 실제 시나리오를 얻을 수 있도록 몇 가지 이미지를 올렸습니다.
그리고 나는 내가 대체하고 있는 각 약의 지수를 캡처하고 싶다.
substituteExistMedicine() {
const subMed = payload
const itemInCart = state.orderedMedicines.filter(item => item.id === subMed.activeId)[0];
const selectedSubstituteItem = state.medicineSubstitution[subMed.index];
const updatedSubstituteList = state.medicineSubstitution.map((substitute, i) => {
if (i === subMed.index) {
return {
...itemInCart
};
}
return {
...substitute
};
});
const updatedOrderedMedicines = state.orderedMedicines.map(item => {
if (item.id === subMed.activeId) {
return {
...selectedSubstituteItem
};
}
return {
...item
};
});
console.log('updatedOrderedMedicines', updatedOrderedMedicines)
this.medicineSubstitution = updatedSubstituteList;
},
더 좋은 솔루션이 있나요?
참고: 첫 번째 화면에는 약물이 표시되며, VIEW SUPTUSTUTE를 클릭하면 해당 약물의 대체 결과가 표시된 다음 화면이 나타납니다.
SUBTITE를 클릭하면 약이 대체되거나 swap이라고 할 수 있습니다.내 경우는 그렇지 않거든
이 업데이트 코드를 대체할 수 있지만 DOM에 반영되지 않습니다.어떻게 해야 합니까?
네가 뭘 하고 싶은지 알 것 같아.JS Fielle의 예를 들어 보겠습니다.
스크립트:
new Vue({
el: "#app",
data: {
medicines: [
{
name: 'greatMedicine',
id: 1,
price: '100',
substitutes: [
{
name: 'notSoGreatButCheaper',
id: 3,
price: '20',
}
]
},
{
name: 'anotherGreatMedicine',
id: 2,
price: '150',
substitutes: [
{
name: 'alsoGreatButCheaper',
id: 4,
price: '30',
}
]
}
],
showSubsMed: '-1'
},
methods: {
substitute: function(med_index,sub_index){
let medicine = this.medicines[med_index];
let substitute = medicine.substitutes[sub_index];
this.medicines.splice(med_index, 1, substitute);
}
}
})
HTML:
<div id="app">
<h2>Medicines:</h2>
<ol>
<li v-for="(med,med_index) in medicines">
{{med.name}} - {{med.price}}€
<span v-if="med.substitutes">
<button @click="showSubsMed === med.id ? showSubsMed = -1 : showSubsMed = med.id" class="btn-primary">
<span v-if="showSubsMed === med.id">hide subs</span>
<span v-else>show subs</span>
</button>
<ol>
<li v-show="showSubsMed === med.id" v-for="(sub,sub_index) in med.substitutes" class="sub">
{{sub.name}} - {{sub.price}}€ <button @click="substitute(med_index,sub_index)" class="btn-primary">Substitute!</button>
</li>
</ol>
</span>
</li>
</ol>
</div>
https://jsfiddle.net/5mt0Loka/1/
언급URL : https://stackoverflow.com/questions/53758270/i-want-to-substitiute-medicines-on-click-of-substitute-button-using-vue-js
반응형
'programing' 카테고리의 다른 글
C에서 값 최적화 및 복사 항목 반환 (0) | 2022.08.19 |
---|---|
Laravel 리소스 경로를 Axios에서 삭제 (0) | 2022.08.19 |
v-for 항목 내부 전환은 전체 목록에 영향을 미치는데, 각 전환이 포함된 목록 항목에만 영향을 미치도록 하려면 어떻게 해야 합니까? (0) | 2022.08.19 |
x86, win32의 빈 프로그램에 대한 GCC 어셈블리 출력 (0) | 2022.08.19 |
휴지 상태를 사용하여 기본 엔티티 속성 값을 설정하는 방법 (0) | 2022.08.19 |