Vue: An empty option should remain selectabe












1














I have a documentwith many input fields. Some are dependent on another, i.e. when in the first input field an option is selected, the second one should give the corresponding options based on a database query. This works fine.



Now I want to add for the second input field an empty option, wich should remain selectable when the first option is selected.



So far I have this solution:



<template>
<div>
<label for="ed_kbid" class="control-label">Bereich</label>
<select2 v-model="bereichId":options="bereiche" name="ed_kbid">
<option disabled selected value="0">Select one</option>
</select2>
</div>
</template>


But the “Select one” disappears when in the first box is something selected.
How can I make it permanent?



Thanks!



edit:



<script>
import JQuery from 'jquery'
import Select2 from '@/components/Select2.vue'

let $ = JQuery

export default {
name: 'Bereich',

components: {
Select2
},

data: function () {
return {
bereiche: ,
kundeUebertrag: '',
bereichId: 0

}
},


computed: {
kundeInputStore: function()
{
return this.$store.state.kundeInputStore

}
},

watch:{
kundeInputStore: function() {
let vm = this;
vm.kundeUebertrag = vm.kundeInputStore;


$.getJSON("/api/get_bereich.php", {kundeUebertrag:
this.kundeUebertrag } , function(result){
vm.bereiche = result;

});

},

bereichId: function(value){
this.$store.commit('setBereichInput', value)
}

},


}



and the Select2-component:



<template>
<select class="form-control" >
<slot></slot>
</select>
</template>


<script>
import JQuery from 'jquery'

let $ = JQuery

export default {
name: 'select2',
props: ['options', 'value'],


mounted: function () {
var vm = this
$(this.$el)
// init select2
.select2({
data: this.options,
tags:true

})
// emit event on change.
.on('change', function () {
vm.$emit('input', this.value)
})
console.log( $(this.$el))

},
watch: {
value: function (value) {
// update value
//console.log(value)
$(this.$el)
.val(value)
.trigger('change')
},
options: function (options) {
// update options
$(this.$el).empty().select2({data: options,tags: true})
}
},
destroyed: function () {
$(this.$el).off().select2('destroy')
}

}











share|improve this question
























  • Provide the "Select one" related code
    – Tamilvanan N
    Nov 13 '18 at 10:37










  • I have edited the post ...
    – Peter
    Nov 13 '18 at 12:02










  • Ref: jsfiddle.net/mani04/Lgxrcc5p
    – Tamilvanan N
    Nov 13 '18 at 14:06










  • I am creating the options via the Select2 component, so the solution doesn't fit to my case..
    – Peter
    Nov 13 '18 at 14:36










  • I just wanna mention: you shouldn't mix Vue and jQuery. Vue is based on creating a virtual DOM and maintaining it, while jQuery is based on manipulating the DOM directly. There's no functionality the jQuery offers that Vue does not either offer straight out of the box or have a plugin for doing (in a Vue friendly manner I might add).
    – Simon Hyll
    Nov 14 '18 at 6:23
















1














I have a documentwith many input fields. Some are dependent on another, i.e. when in the first input field an option is selected, the second one should give the corresponding options based on a database query. This works fine.



Now I want to add for the second input field an empty option, wich should remain selectable when the first option is selected.



So far I have this solution:



<template>
<div>
<label for="ed_kbid" class="control-label">Bereich</label>
<select2 v-model="bereichId":options="bereiche" name="ed_kbid">
<option disabled selected value="0">Select one</option>
</select2>
</div>
</template>


But the “Select one” disappears when in the first box is something selected.
How can I make it permanent?



Thanks!



edit:



<script>
import JQuery from 'jquery'
import Select2 from '@/components/Select2.vue'

let $ = JQuery

export default {
name: 'Bereich',

components: {
Select2
},

data: function () {
return {
bereiche: ,
kundeUebertrag: '',
bereichId: 0

}
},


computed: {
kundeInputStore: function()
{
return this.$store.state.kundeInputStore

}
},

watch:{
kundeInputStore: function() {
let vm = this;
vm.kundeUebertrag = vm.kundeInputStore;


$.getJSON("/api/get_bereich.php", {kundeUebertrag:
this.kundeUebertrag } , function(result){
vm.bereiche = result;

});

},

bereichId: function(value){
this.$store.commit('setBereichInput', value)
}

},


}



and the Select2-component:



<template>
<select class="form-control" >
<slot></slot>
</select>
</template>


<script>
import JQuery from 'jquery'

let $ = JQuery

export default {
name: 'select2',
props: ['options', 'value'],


mounted: function () {
var vm = this
$(this.$el)
// init select2
.select2({
data: this.options,
tags:true

})
// emit event on change.
.on('change', function () {
vm.$emit('input', this.value)
})
console.log( $(this.$el))

},
watch: {
value: function (value) {
// update value
//console.log(value)
$(this.$el)
.val(value)
.trigger('change')
},
options: function (options) {
// update options
$(this.$el).empty().select2({data: options,tags: true})
}
},
destroyed: function () {
$(this.$el).off().select2('destroy')
}

}











share|improve this question
























  • Provide the "Select one" related code
    – Tamilvanan N
    Nov 13 '18 at 10:37










  • I have edited the post ...
    – Peter
    Nov 13 '18 at 12:02










  • Ref: jsfiddle.net/mani04/Lgxrcc5p
    – Tamilvanan N
    Nov 13 '18 at 14:06










  • I am creating the options via the Select2 component, so the solution doesn't fit to my case..
    – Peter
    Nov 13 '18 at 14:36










  • I just wanna mention: you shouldn't mix Vue and jQuery. Vue is based on creating a virtual DOM and maintaining it, while jQuery is based on manipulating the DOM directly. There's no functionality the jQuery offers that Vue does not either offer straight out of the box or have a plugin for doing (in a Vue friendly manner I might add).
    – Simon Hyll
    Nov 14 '18 at 6:23














1












1








1


0





I have a documentwith many input fields. Some are dependent on another, i.e. when in the first input field an option is selected, the second one should give the corresponding options based on a database query. This works fine.



Now I want to add for the second input field an empty option, wich should remain selectable when the first option is selected.



So far I have this solution:



<template>
<div>
<label for="ed_kbid" class="control-label">Bereich</label>
<select2 v-model="bereichId":options="bereiche" name="ed_kbid">
<option disabled selected value="0">Select one</option>
</select2>
</div>
</template>


But the “Select one” disappears when in the first box is something selected.
How can I make it permanent?



Thanks!



edit:



<script>
import JQuery from 'jquery'
import Select2 from '@/components/Select2.vue'

let $ = JQuery

export default {
name: 'Bereich',

components: {
Select2
},

data: function () {
return {
bereiche: ,
kundeUebertrag: '',
bereichId: 0

}
},


computed: {
kundeInputStore: function()
{
return this.$store.state.kundeInputStore

}
},

watch:{
kundeInputStore: function() {
let vm = this;
vm.kundeUebertrag = vm.kundeInputStore;


$.getJSON("/api/get_bereich.php", {kundeUebertrag:
this.kundeUebertrag } , function(result){
vm.bereiche = result;

});

},

bereichId: function(value){
this.$store.commit('setBereichInput', value)
}

},


}



and the Select2-component:



<template>
<select class="form-control" >
<slot></slot>
</select>
</template>


<script>
import JQuery from 'jquery'

let $ = JQuery

export default {
name: 'select2',
props: ['options', 'value'],


mounted: function () {
var vm = this
$(this.$el)
// init select2
.select2({
data: this.options,
tags:true

})
// emit event on change.
.on('change', function () {
vm.$emit('input', this.value)
})
console.log( $(this.$el))

},
watch: {
value: function (value) {
// update value
//console.log(value)
$(this.$el)
.val(value)
.trigger('change')
},
options: function (options) {
// update options
$(this.$el).empty().select2({data: options,tags: true})
}
},
destroyed: function () {
$(this.$el).off().select2('destroy')
}

}











share|improve this question















I have a documentwith many input fields. Some are dependent on another, i.e. when in the first input field an option is selected, the second one should give the corresponding options based on a database query. This works fine.



Now I want to add for the second input field an empty option, wich should remain selectable when the first option is selected.



So far I have this solution:



<template>
<div>
<label for="ed_kbid" class="control-label">Bereich</label>
<select2 v-model="bereichId":options="bereiche" name="ed_kbid">
<option disabled selected value="0">Select one</option>
</select2>
</div>
</template>


But the “Select one” disappears when in the first box is something selected.
How can I make it permanent?



Thanks!



edit:



<script>
import JQuery from 'jquery'
import Select2 from '@/components/Select2.vue'

let $ = JQuery

export default {
name: 'Bereich',

components: {
Select2
},

data: function () {
return {
bereiche: ,
kundeUebertrag: '',
bereichId: 0

}
},


computed: {
kundeInputStore: function()
{
return this.$store.state.kundeInputStore

}
},

watch:{
kundeInputStore: function() {
let vm = this;
vm.kundeUebertrag = vm.kundeInputStore;


$.getJSON("/api/get_bereich.php", {kundeUebertrag:
this.kundeUebertrag } , function(result){
vm.bereiche = result;

});

},

bereichId: function(value){
this.$store.commit('setBereichInput', value)
}

},


}



and the Select2-component:



<template>
<select class="form-control" >
<slot></slot>
</select>
</template>


<script>
import JQuery from 'jquery'

let $ = JQuery

export default {
name: 'select2',
props: ['options', 'value'],


mounted: function () {
var vm = this
$(this.$el)
// init select2
.select2({
data: this.options,
tags:true

})
// emit event on change.
.on('change', function () {
vm.$emit('input', this.value)
})
console.log( $(this.$el))

},
watch: {
value: function (value) {
// update value
//console.log(value)
$(this.$el)
.val(value)
.trigger('change')
},
options: function (options) {
// update options
$(this.$el).empty().select2({data: options,tags: true})
}
},
destroyed: function () {
$(this.$el).off().select2('destroy')
}

}








vue.js jquery-select2 html-select






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 11:57







Peter

















asked Nov 13 '18 at 10:31









PeterPeter

114




114












  • Provide the "Select one" related code
    – Tamilvanan N
    Nov 13 '18 at 10:37










  • I have edited the post ...
    – Peter
    Nov 13 '18 at 12:02










  • Ref: jsfiddle.net/mani04/Lgxrcc5p
    – Tamilvanan N
    Nov 13 '18 at 14:06










  • I am creating the options via the Select2 component, so the solution doesn't fit to my case..
    – Peter
    Nov 13 '18 at 14:36










  • I just wanna mention: you shouldn't mix Vue and jQuery. Vue is based on creating a virtual DOM and maintaining it, while jQuery is based on manipulating the DOM directly. There's no functionality the jQuery offers that Vue does not either offer straight out of the box or have a plugin for doing (in a Vue friendly manner I might add).
    – Simon Hyll
    Nov 14 '18 at 6:23


















  • Provide the "Select one" related code
    – Tamilvanan N
    Nov 13 '18 at 10:37










  • I have edited the post ...
    – Peter
    Nov 13 '18 at 12:02










  • Ref: jsfiddle.net/mani04/Lgxrcc5p
    – Tamilvanan N
    Nov 13 '18 at 14:06










  • I am creating the options via the Select2 component, so the solution doesn't fit to my case..
    – Peter
    Nov 13 '18 at 14:36










  • I just wanna mention: you shouldn't mix Vue and jQuery. Vue is based on creating a virtual DOM and maintaining it, while jQuery is based on manipulating the DOM directly. There's no functionality the jQuery offers that Vue does not either offer straight out of the box or have a plugin for doing (in a Vue friendly manner I might add).
    – Simon Hyll
    Nov 14 '18 at 6:23
















Provide the "Select one" related code
– Tamilvanan N
Nov 13 '18 at 10:37




Provide the "Select one" related code
– Tamilvanan N
Nov 13 '18 at 10:37












I have edited the post ...
– Peter
Nov 13 '18 at 12:02




I have edited the post ...
– Peter
Nov 13 '18 at 12:02












Ref: jsfiddle.net/mani04/Lgxrcc5p
– Tamilvanan N
Nov 13 '18 at 14:06




Ref: jsfiddle.net/mani04/Lgxrcc5p
– Tamilvanan N
Nov 13 '18 at 14:06












I am creating the options via the Select2 component, so the solution doesn't fit to my case..
– Peter
Nov 13 '18 at 14:36




I am creating the options via the Select2 component, so the solution doesn't fit to my case..
– Peter
Nov 13 '18 at 14:36












I just wanna mention: you shouldn't mix Vue and jQuery. Vue is based on creating a virtual DOM and maintaining it, while jQuery is based on manipulating the DOM directly. There's no functionality the jQuery offers that Vue does not either offer straight out of the box or have a plugin for doing (in a Vue friendly manner I might add).
– Simon Hyll
Nov 14 '18 at 6:23




I just wanna mention: you shouldn't mix Vue and jQuery. Vue is based on creating a virtual DOM and maintaining it, while jQuery is based on manipulating the DOM directly. There's no functionality the jQuery offers that Vue does not either offer straight out of the box or have a plugin for doing (in a Vue friendly manner I might add).
– Simon Hyll
Nov 14 '18 at 6:23












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53279010%2fvue-an-empty-option-should-remain-selectabe%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53279010%2fvue-an-empty-option-should-remain-selectabe%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly