I'm not sure if it's possible to achieve what I want with Vue for a specific component by changing its data and automatically loading it.
Below is my expectation (tried in jQuery)
var data = {country:{type:'dropdown',values:['india','usa']},money:{type:'input',placeholder:'enter amount'},india:['Bengaluru'],usa:['Silicon Valley']}
function getDropTemplate(dropDownList){
var dropDownStr = '';
for(var i = 0; i < dropDownList.length; i++){
dropDownStr += `<option value="${dropDownList[i]}">${dropDownList[i]}</option>`
}
return `<select class="mainCountry">${dropDownStr}</select>`;
}
function getInputTemplate(inputObj){
return `<input type="text" placeholder="${inputObj.placeholder}"/>`
}
$(function(){
$('#dropdown').on('change',function(){
var value = $(this).val(), template = '';
if(data[value].type == 'dropdown'){
template += getDropTemplate(data[value].values)
}else{
template += getInputTemplate(data[value])
}
$('#selectedResults').html(template);
});
$(document).on('change','.mainCountry',function(){
var result = data[$(this).val()]
$('#subResults').html(getDropTemplate(result));
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="dropdown">
<option value="">--select--</option>
<option value="money">Money</option>
<option value="country">Country</option>
</select>
<div id="selectedResults">
</div>
<div id="subResults">
</div>
From the snippet above, you can see that by selecting
country -> india -> Bengaluru
or country -> usa -> Silicon Valley
.
I want to replicate the same thing with vue-multiselect
Below is what I have tried in Vue
var app = new Vue({
el: '#app',
components: { Multiselect: window.VueMultiselect.default },
data () {
return {
value: [],
//data:{country:{type:'dropdown',values:['india','usa']},money:{type:'input',placeholder:'enter amount'},india:['Bengaluru'],usa:['Silicon Valley']}
options:[{name:'money'},{name:'country'}]
}
}
})
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="aadcdfcf87c7dfc6dec3d9cfc6cfc9deea98849b849a">[email protected]</a>"></script>
<link rel="stylesheet" href="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="285e5d4d05455d445c415b4d444d4b5c681a06190618">[email protected]</a>/dist/vue-multiselect.min.css">
<script defer src="https://use.fontawesome.com/releases/v5.3.1/js/all.js"></script>
<div id="app">
<multiselect
v-model="value"
track-by="name"
:options="options"
label="name"
:multiple="false"
:taggable="false"
></multiselect>
</div>