How to make a tag in html only shows some data depending on another tag using Vue.jsVue.js dependent selectHTML form readonly SELECT tag/inputHow to store arbitrary data for some HTML tagsenable/disable textbox on combo selectionHow to populate data into a dropbox based on selections of another dropbox?dynamic select tag in html using javascript, php and jsonShow AJAX success JSON values in html selectionenable a disabled select when choose a value in another select tagHow can i disable/enable form based on dropdown valueVue.js - How to properly watch for nested dataHow to clear v-model used with a select when options change

Any way to add more GPIOs to the AIY Voice Kit?

Vehemently against code formatting

Why does an injection from a set to a countable set imply that set is countable?

Good examples of "two is easy, three is hard" in computational sciences

How to use Screen Sharing if I don't know the remote Mac's IP address

No Active Recurring Contributions on civi record but stripe has the data. Is there a way to resend IPN?

How to convince boss to spend notice period on documentation instead of new projects

How can sister protect herself from impulse purchases with a credit card?

How would a physicist explain this starship engine?

Story about encounter with hostile aliens

HoD says group project may be rejected. How to mitigate?

How to choose the correct exposure for flower photography?

How to say "they didn't leave him a penny"?

Bash - Execute two commands and get exit status 1 if first fails

How to become an Editorial board member?

What does it mean to "take the Cross"

How should I mix small caps with digits or symbols?

Is there a realtime, uncut video of Saturn V ignition through tower clear?

Is there a word for pant sleeves?

How to safely discharge oneself

Working hours and productivity expectations for game artists and programmers

Salesforce bug enabled "Modify All"

How to prove the emptiness of intersection of two context free languages is undecidable?

Why is this python script running in background consuming 100 % CPU?



How to make a tag in html only shows some data depending on another tag using Vue.js


Vue.js dependent selectHTML form readonly SELECT tag/inputHow to store arbitrary data for some HTML tagsenable/disable textbox on combo selectionHow to populate data into a dropbox based on selections of another dropbox?dynamic select tag in html using javascript, php and jsonShow AJAX success JSON values in html selectionenable a disabled select when choose a value in another select tagHow can i disable/enable form based on dropdown valueVue.js - How to properly watch for nested dataHow to clear v-model used with a select when options change






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I'm displaying two <select> elements. The second <select> is disabled depending on the <option> selected on the first <select>. The problem is when I select an <option> on the first <select>, I want the data shown on the second <select> to be changed. For example, if I select District 1 on the first <select>, I want to see john and mary as options in the second <select>, but if I select District 2, I want josef and charles. Consider that I'm doing this on Laravel and using Vue.



I have done the first part using Vue, disabling the second <select> depending on what has been chosen on the first <select> (only third option on the first <select> will enable the second <select>):



https://jsfiddle.net/vowexafm/122/



Template:



<script src="https://cdn.jsdelivr.net/npm/vue"></script>

<div id="app">
<select @change="treat">
<option value="District1">District 1</option><!--disable 2nd select-->
<option value="District2">District 2</option><!--disable 2nd select-->
<option value="District3">District 3</option><!--enable 2nd select-->
</select>
<br><br>
<select :disabled="isDisabled">
<option>cds 1</option>
<option>cds 2</option>
<option>cds 3</option>
</select>
</div>


Script:



new Vue(
el:'#app',
data:
isDisabled: true,
,
methods:
treat: function(e)
e.target.options.selectedIndex == 1)
return this.disable();


if (e.target.options.selectedIndex != 0 &&
e.target.options.selectedIndex != 1)
return this.enable();

,
enable: function()
return this.isDisabled = false; // enables second select
,
disable: function()
return this.isDisabled = true; // disables second select

,
);


Now the solution I want,for example: if i'I select District 1 on the first , I want to see john and mary as options in the second , but if I select District 2, I want to see josef and charles on the second .










share|improve this question



















  • 1





    stackoverflow.com/questions/42470501/vue-js-dependent-select

    – Roy J
    Mar 23 at 19:35











  • this answer does not help

    – kisaw88
    Mar 23 at 20:02

















1















I'm displaying two <select> elements. The second <select> is disabled depending on the <option> selected on the first <select>. The problem is when I select an <option> on the first <select>, I want the data shown on the second <select> to be changed. For example, if I select District 1 on the first <select>, I want to see john and mary as options in the second <select>, but if I select District 2, I want josef and charles. Consider that I'm doing this on Laravel and using Vue.



I have done the first part using Vue, disabling the second <select> depending on what has been chosen on the first <select> (only third option on the first <select> will enable the second <select>):



https://jsfiddle.net/vowexafm/122/



Template:



<script src="https://cdn.jsdelivr.net/npm/vue"></script>

<div id="app">
<select @change="treat">
<option value="District1">District 1</option><!--disable 2nd select-->
<option value="District2">District 2</option><!--disable 2nd select-->
<option value="District3">District 3</option><!--enable 2nd select-->
</select>
<br><br>
<select :disabled="isDisabled">
<option>cds 1</option>
<option>cds 2</option>
<option>cds 3</option>
</select>
</div>


Script:



new Vue(
el:'#app',
data:
isDisabled: true,
,
methods:
treat: function(e)
e.target.options.selectedIndex == 1)
return this.disable();


if (e.target.options.selectedIndex != 0 &&
e.target.options.selectedIndex != 1)
return this.enable();

,
enable: function()
return this.isDisabled = false; // enables second select
,
disable: function()
return this.isDisabled = true; // disables second select

,
);


Now the solution I want,for example: if i'I select District 1 on the first , I want to see john and mary as options in the second , but if I select District 2, I want to see josef and charles on the second .










share|improve this question



















  • 1





    stackoverflow.com/questions/42470501/vue-js-dependent-select

    – Roy J
    Mar 23 at 19:35











  • this answer does not help

    – kisaw88
    Mar 23 at 20:02













1












1








1








I'm displaying two <select> elements. The second <select> is disabled depending on the <option> selected on the first <select>. The problem is when I select an <option> on the first <select>, I want the data shown on the second <select> to be changed. For example, if I select District 1 on the first <select>, I want to see john and mary as options in the second <select>, but if I select District 2, I want josef and charles. Consider that I'm doing this on Laravel and using Vue.



I have done the first part using Vue, disabling the second <select> depending on what has been chosen on the first <select> (only third option on the first <select> will enable the second <select>):



https://jsfiddle.net/vowexafm/122/



Template:



<script src="https://cdn.jsdelivr.net/npm/vue"></script>

<div id="app">
<select @change="treat">
<option value="District1">District 1</option><!--disable 2nd select-->
<option value="District2">District 2</option><!--disable 2nd select-->
<option value="District3">District 3</option><!--enable 2nd select-->
</select>
<br><br>
<select :disabled="isDisabled">
<option>cds 1</option>
<option>cds 2</option>
<option>cds 3</option>
</select>
</div>


Script:



new Vue(
el:'#app',
data:
isDisabled: true,
,
methods:
treat: function(e)
e.target.options.selectedIndex == 1)
return this.disable();


if (e.target.options.selectedIndex != 0 &&
e.target.options.selectedIndex != 1)
return this.enable();

,
enable: function()
return this.isDisabled = false; // enables second select
,
disable: function()
return this.isDisabled = true; // disables second select

,
);


Now the solution I want,for example: if i'I select District 1 on the first , I want to see john and mary as options in the second , but if I select District 2, I want to see josef and charles on the second .










share|improve this question
















I'm displaying two <select> elements. The second <select> is disabled depending on the <option> selected on the first <select>. The problem is when I select an <option> on the first <select>, I want the data shown on the second <select> to be changed. For example, if I select District 1 on the first <select>, I want to see john and mary as options in the second <select>, but if I select District 2, I want josef and charles. Consider that I'm doing this on Laravel and using Vue.



I have done the first part using Vue, disabling the second <select> depending on what has been chosen on the first <select> (only third option on the first <select> will enable the second <select>):



https://jsfiddle.net/vowexafm/122/



Template:



<script src="https://cdn.jsdelivr.net/npm/vue"></script>

<div id="app">
<select @change="treat">
<option value="District1">District 1</option><!--disable 2nd select-->
<option value="District2">District 2</option><!--disable 2nd select-->
<option value="District3">District 3</option><!--enable 2nd select-->
</select>
<br><br>
<select :disabled="isDisabled">
<option>cds 1</option>
<option>cds 2</option>
<option>cds 3</option>
</select>
</div>


Script:



new Vue(
el:'#app',
data:
isDisabled: true,
,
methods:
treat: function(e)
e.target.options.selectedIndex == 1)
return this.disable();


if (e.target.options.selectedIndex != 0 &&
e.target.options.selectedIndex != 1)
return this.enable();

,
enable: function()
return this.isDisabled = false; // enables second select
,
disable: function()
return this.isDisabled = true; // disables second select

,
);


Now the solution I want,for example: if i'I select District 1 on the first , I want to see john and mary as options in the second , but if I select District 2, I want to see josef and charles on the second .







javascript laravel vue.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 24 at 12:13







kisaw88

















asked Mar 23 at 19:30









kisaw88kisaw88

3818




3818







  • 1





    stackoverflow.com/questions/42470501/vue-js-dependent-select

    – Roy J
    Mar 23 at 19:35











  • this answer does not help

    – kisaw88
    Mar 23 at 20:02












  • 1





    stackoverflow.com/questions/42470501/vue-js-dependent-select

    – Roy J
    Mar 23 at 19:35











  • this answer does not help

    – kisaw88
    Mar 23 at 20:02







1




1





stackoverflow.com/questions/42470501/vue-js-dependent-select

– Roy J
Mar 23 at 19:35





stackoverflow.com/questions/42470501/vue-js-dependent-select

– Roy J
Mar 23 at 19:35













this answer does not help

– kisaw88
Mar 23 at 20:02





this answer does not help

– kisaw88
Mar 23 at 20:02












2 Answers
2






active

oldest

votes


















1














Populate data object from laravel to have the options for the second select in it and a value for the current selected index from the first select



data: 
secondSelect: [
['john', 'mary'],
['josef', 'charles']
],
currentIndex: 0



Define a computed property that returns the values for the second select depending on currentIndex



computed: 
persons ()
return this.secondSelect[parseInt(this.currentIndex)]




Generate the second select using the computed property persons and use v-model to capture currentIndex.



<div id="app">
<select @change="treat" v-model="selectedIndex">
<option value="0">District 1</option><!--diable-->
<option value="1">District 2</option><!--diable-->
<option value="2">District 3</option><!--unable-->
</select>
<br><br>
<select :disabled="isDisabled">
<option v-for="option in persons" :value="option">option</option>
</select>
</div>





share|improve this answer























  • okay , but what if the data i want to show in the second select i need to get them from the database ? –

    – kisaw88
    Mar 24 at 9:20











  • You have 2 options: get the data before hand and populate the data object when generating the page or create an API that returns the data and use, fetch, axios or vue resources to fetch the data

    – Radu Diță
    Mar 24 at 9:32


















1















now the solution I want,for example if i select 'District 1 'on the
first i want to see 'john' and 'mary' as options in the second , but
if I select 'District 2' i want to see 'josef' and 'charles' on the
second .




is that whats in your mind?






new Vue(
el:'#app',
data:
district:'-1',
options:
'District1':false,
'District2':false,
'District3':false
,
,
methods:
getOptions()
axios.get('https://jsonplaceholder.typicode.com/users',district:this.district).then(res=>
this.options[this.district]=res.data)


);

<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.18.0/axios.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>

<div id="app">
<select v-model="district" @change="getOptions()">
<option value="-1" selected disabled hidden>choose a district</option>
<option value="District1">District 1</option>
<option value="District2">District 2</option>
<option value="District3">District 3</option>
</select>
<br><br>
<select v-if="options[district]">
<option v-for="option in options[district]">option.name</option>
</select>
</div>





edit:



after your comment, i edited this answer so now its fetching the data from some api.



to fetch the options from a db, you first have to create an api for your app, and when the request comes out of your vue client side - the server will retrieve rows from the db, do some caculations based on the parameters you sent in the request, and bring back a json data array.



(i wont cover the server side now - thats completely off-topic. but you can easily google for 'laravel json response')



in this snippet, i used some example json api, just to show you how its done on the client side:



i use v-if to cause late- rendering of the second select. it will be rendered only after i get the options, via axios (a very common npm package used to make ajax requests in modern js frameworks).



im also registering an event listener to the change event of the first select - to make my ajax request and populate my options every time the disrict changes (i used a default option to avoid unneeded requests)






share|improve this answer

























  • okay , but what if the data i want to show in the second select i need to get them from the database ?

    – kisaw88
    Mar 24 at 8:43











  • @kisaw88 ive made an edit

    – Efrat Levitan
    Mar 24 at 10:21











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%2f55317574%2fhow-to-make-a-select-tag-in-html-only-shows-some-data-depending-on-another-se%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Populate data object from laravel to have the options for the second select in it and a value for the current selected index from the first select



data: 
secondSelect: [
['john', 'mary'],
['josef', 'charles']
],
currentIndex: 0



Define a computed property that returns the values for the second select depending on currentIndex



computed: 
persons ()
return this.secondSelect[parseInt(this.currentIndex)]




Generate the second select using the computed property persons and use v-model to capture currentIndex.



<div id="app">
<select @change="treat" v-model="selectedIndex">
<option value="0">District 1</option><!--diable-->
<option value="1">District 2</option><!--diable-->
<option value="2">District 3</option><!--unable-->
</select>
<br><br>
<select :disabled="isDisabled">
<option v-for="option in persons" :value="option">option</option>
</select>
</div>





share|improve this answer























  • okay , but what if the data i want to show in the second select i need to get them from the database ? –

    – kisaw88
    Mar 24 at 9:20











  • You have 2 options: get the data before hand and populate the data object when generating the page or create an API that returns the data and use, fetch, axios or vue resources to fetch the data

    – Radu Diță
    Mar 24 at 9:32















1














Populate data object from laravel to have the options for the second select in it and a value for the current selected index from the first select



data: 
secondSelect: [
['john', 'mary'],
['josef', 'charles']
],
currentIndex: 0



Define a computed property that returns the values for the second select depending on currentIndex



computed: 
persons ()
return this.secondSelect[parseInt(this.currentIndex)]




Generate the second select using the computed property persons and use v-model to capture currentIndex.



<div id="app">
<select @change="treat" v-model="selectedIndex">
<option value="0">District 1</option><!--diable-->
<option value="1">District 2</option><!--diable-->
<option value="2">District 3</option><!--unable-->
</select>
<br><br>
<select :disabled="isDisabled">
<option v-for="option in persons" :value="option">option</option>
</select>
</div>





share|improve this answer























  • okay , but what if the data i want to show in the second select i need to get them from the database ? –

    – kisaw88
    Mar 24 at 9:20











  • You have 2 options: get the data before hand and populate the data object when generating the page or create an API that returns the data and use, fetch, axios or vue resources to fetch the data

    – Radu Diță
    Mar 24 at 9:32













1












1








1







Populate data object from laravel to have the options for the second select in it and a value for the current selected index from the first select



data: 
secondSelect: [
['john', 'mary'],
['josef', 'charles']
],
currentIndex: 0



Define a computed property that returns the values for the second select depending on currentIndex



computed: 
persons ()
return this.secondSelect[parseInt(this.currentIndex)]




Generate the second select using the computed property persons and use v-model to capture currentIndex.



<div id="app">
<select @change="treat" v-model="selectedIndex">
<option value="0">District 1</option><!--diable-->
<option value="1">District 2</option><!--diable-->
<option value="2">District 3</option><!--unable-->
</select>
<br><br>
<select :disabled="isDisabled">
<option v-for="option in persons" :value="option">option</option>
</select>
</div>





share|improve this answer













Populate data object from laravel to have the options for the second select in it and a value for the current selected index from the first select



data: 
secondSelect: [
['john', 'mary'],
['josef', 'charles']
],
currentIndex: 0



Define a computed property that returns the values for the second select depending on currentIndex



computed: 
persons ()
return this.secondSelect[parseInt(this.currentIndex)]




Generate the second select using the computed property persons and use v-model to capture currentIndex.



<div id="app">
<select @change="treat" v-model="selectedIndex">
<option value="0">District 1</option><!--diable-->
<option value="1">District 2</option><!--diable-->
<option value="2">District 3</option><!--unable-->
</select>
<br><br>
<select :disabled="isDisabled">
<option v-for="option in persons" :value="option">option</option>
</select>
</div>






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 23 at 20:28









Radu DițăRadu Diță

4,38311321




4,38311321












  • okay , but what if the data i want to show in the second select i need to get them from the database ? –

    – kisaw88
    Mar 24 at 9:20











  • You have 2 options: get the data before hand and populate the data object when generating the page or create an API that returns the data and use, fetch, axios or vue resources to fetch the data

    – Radu Diță
    Mar 24 at 9:32

















  • okay , but what if the data i want to show in the second select i need to get them from the database ? –

    – kisaw88
    Mar 24 at 9:20











  • You have 2 options: get the data before hand and populate the data object when generating the page or create an API that returns the data and use, fetch, axios or vue resources to fetch the data

    – Radu Diță
    Mar 24 at 9:32
















okay , but what if the data i want to show in the second select i need to get them from the database ? –

– kisaw88
Mar 24 at 9:20





okay , but what if the data i want to show in the second select i need to get them from the database ? –

– kisaw88
Mar 24 at 9:20













You have 2 options: get the data before hand and populate the data object when generating the page or create an API that returns the data and use, fetch, axios or vue resources to fetch the data

– Radu Diță
Mar 24 at 9:32





You have 2 options: get the data before hand and populate the data object when generating the page or create an API that returns the data and use, fetch, axios or vue resources to fetch the data

– Radu Diță
Mar 24 at 9:32













1















now the solution I want,for example if i select 'District 1 'on the
first i want to see 'john' and 'mary' as options in the second , but
if I select 'District 2' i want to see 'josef' and 'charles' on the
second .




is that whats in your mind?






new Vue(
el:'#app',
data:
district:'-1',
options:
'District1':false,
'District2':false,
'District3':false
,
,
methods:
getOptions()
axios.get('https://jsonplaceholder.typicode.com/users',district:this.district).then(res=>
this.options[this.district]=res.data)


);

<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.18.0/axios.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>

<div id="app">
<select v-model="district" @change="getOptions()">
<option value="-1" selected disabled hidden>choose a district</option>
<option value="District1">District 1</option>
<option value="District2">District 2</option>
<option value="District3">District 3</option>
</select>
<br><br>
<select v-if="options[district]">
<option v-for="option in options[district]">option.name</option>
</select>
</div>





edit:



after your comment, i edited this answer so now its fetching the data from some api.



to fetch the options from a db, you first have to create an api for your app, and when the request comes out of your vue client side - the server will retrieve rows from the db, do some caculations based on the parameters you sent in the request, and bring back a json data array.



(i wont cover the server side now - thats completely off-topic. but you can easily google for 'laravel json response')



in this snippet, i used some example json api, just to show you how its done on the client side:



i use v-if to cause late- rendering of the second select. it will be rendered only after i get the options, via axios (a very common npm package used to make ajax requests in modern js frameworks).



im also registering an event listener to the change event of the first select - to make my ajax request and populate my options every time the disrict changes (i used a default option to avoid unneeded requests)






share|improve this answer

























  • okay , but what if the data i want to show in the second select i need to get them from the database ?

    – kisaw88
    Mar 24 at 8:43











  • @kisaw88 ive made an edit

    – Efrat Levitan
    Mar 24 at 10:21















1















now the solution I want,for example if i select 'District 1 'on the
first i want to see 'john' and 'mary' as options in the second , but
if I select 'District 2' i want to see 'josef' and 'charles' on the
second .




is that whats in your mind?






new Vue(
el:'#app',
data:
district:'-1',
options:
'District1':false,
'District2':false,
'District3':false
,
,
methods:
getOptions()
axios.get('https://jsonplaceholder.typicode.com/users',district:this.district).then(res=>
this.options[this.district]=res.data)


);

<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.18.0/axios.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>

<div id="app">
<select v-model="district" @change="getOptions()">
<option value="-1" selected disabled hidden>choose a district</option>
<option value="District1">District 1</option>
<option value="District2">District 2</option>
<option value="District3">District 3</option>
</select>
<br><br>
<select v-if="options[district]">
<option v-for="option in options[district]">option.name</option>
</select>
</div>





edit:



after your comment, i edited this answer so now its fetching the data from some api.



to fetch the options from a db, you first have to create an api for your app, and when the request comes out of your vue client side - the server will retrieve rows from the db, do some caculations based on the parameters you sent in the request, and bring back a json data array.



(i wont cover the server side now - thats completely off-topic. but you can easily google for 'laravel json response')



in this snippet, i used some example json api, just to show you how its done on the client side:



i use v-if to cause late- rendering of the second select. it will be rendered only after i get the options, via axios (a very common npm package used to make ajax requests in modern js frameworks).



im also registering an event listener to the change event of the first select - to make my ajax request and populate my options every time the disrict changes (i used a default option to avoid unneeded requests)






share|improve this answer

























  • okay , but what if the data i want to show in the second select i need to get them from the database ?

    – kisaw88
    Mar 24 at 8:43











  • @kisaw88 ive made an edit

    – Efrat Levitan
    Mar 24 at 10:21













1












1








1








now the solution I want,for example if i select 'District 1 'on the
first i want to see 'john' and 'mary' as options in the second , but
if I select 'District 2' i want to see 'josef' and 'charles' on the
second .




is that whats in your mind?






new Vue(
el:'#app',
data:
district:'-1',
options:
'District1':false,
'District2':false,
'District3':false
,
,
methods:
getOptions()
axios.get('https://jsonplaceholder.typicode.com/users',district:this.district).then(res=>
this.options[this.district]=res.data)


);

<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.18.0/axios.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>

<div id="app">
<select v-model="district" @change="getOptions()">
<option value="-1" selected disabled hidden>choose a district</option>
<option value="District1">District 1</option>
<option value="District2">District 2</option>
<option value="District3">District 3</option>
</select>
<br><br>
<select v-if="options[district]">
<option v-for="option in options[district]">option.name</option>
</select>
</div>





edit:



after your comment, i edited this answer so now its fetching the data from some api.



to fetch the options from a db, you first have to create an api for your app, and when the request comes out of your vue client side - the server will retrieve rows from the db, do some caculations based on the parameters you sent in the request, and bring back a json data array.



(i wont cover the server side now - thats completely off-topic. but you can easily google for 'laravel json response')



in this snippet, i used some example json api, just to show you how its done on the client side:



i use v-if to cause late- rendering of the second select. it will be rendered only after i get the options, via axios (a very common npm package used to make ajax requests in modern js frameworks).



im also registering an event listener to the change event of the first select - to make my ajax request and populate my options every time the disrict changes (i used a default option to avoid unneeded requests)






share|improve this answer
















now the solution I want,for example if i select 'District 1 'on the
first i want to see 'john' and 'mary' as options in the second , but
if I select 'District 2' i want to see 'josef' and 'charles' on the
second .




is that whats in your mind?






new Vue(
el:'#app',
data:
district:'-1',
options:
'District1':false,
'District2':false,
'District3':false
,
,
methods:
getOptions()
axios.get('https://jsonplaceholder.typicode.com/users',district:this.district).then(res=>
this.options[this.district]=res.data)


);

<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.18.0/axios.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>

<div id="app">
<select v-model="district" @change="getOptions()">
<option value="-1" selected disabled hidden>choose a district</option>
<option value="District1">District 1</option>
<option value="District2">District 2</option>
<option value="District3">District 3</option>
</select>
<br><br>
<select v-if="options[district]">
<option v-for="option in options[district]">option.name</option>
</select>
</div>





edit:



after your comment, i edited this answer so now its fetching the data from some api.



to fetch the options from a db, you first have to create an api for your app, and when the request comes out of your vue client side - the server will retrieve rows from the db, do some caculations based on the parameters you sent in the request, and bring back a json data array.



(i wont cover the server side now - thats completely off-topic. but you can easily google for 'laravel json response')



in this snippet, i used some example json api, just to show you how its done on the client side:



i use v-if to cause late- rendering of the second select. it will be rendered only after i get the options, via axios (a very common npm package used to make ajax requests in modern js frameworks).



im also registering an event listener to the change event of the first select - to make my ajax request and populate my options every time the disrict changes (i used a default option to avoid unneeded requests)






new Vue(
el:'#app',
data:
district:'-1',
options:
'District1':false,
'District2':false,
'District3':false
,
,
methods:
getOptions()
axios.get('https://jsonplaceholder.typicode.com/users',district:this.district).then(res=>
this.options[this.district]=res.data)


);

<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.18.0/axios.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>

<div id="app">
<select v-model="district" @change="getOptions()">
<option value="-1" selected disabled hidden>choose a district</option>
<option value="District1">District 1</option>
<option value="District2">District 2</option>
<option value="District3">District 3</option>
</select>
<br><br>
<select v-if="options[district]">
<option v-for="option in options[district]">option.name</option>
</select>
</div>





new Vue(
el:'#app',
data:
district:'-1',
options:
'District1':false,
'District2':false,
'District3':false
,
,
methods:
getOptions()
axios.get('https://jsonplaceholder.typicode.com/users',district:this.district).then(res=>
this.options[this.district]=res.data)


);

<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.18.0/axios.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>

<div id="app">
<select v-model="district" @change="getOptions()">
<option value="-1" selected disabled hidden>choose a district</option>
<option value="District1">District 1</option>
<option value="District2">District 2</option>
<option value="District3">District 3</option>
</select>
<br><br>
<select v-if="options[district]">
<option v-for="option in options[district]">option.name</option>
</select>
</div>






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 24 at 10:20

























answered Mar 23 at 20:37









Efrat LevitanEfrat Levitan

1,2031314




1,2031314












  • okay , but what if the data i want to show in the second select i need to get them from the database ?

    – kisaw88
    Mar 24 at 8:43











  • @kisaw88 ive made an edit

    – Efrat Levitan
    Mar 24 at 10:21

















  • okay , but what if the data i want to show in the second select i need to get them from the database ?

    – kisaw88
    Mar 24 at 8:43











  • @kisaw88 ive made an edit

    – Efrat Levitan
    Mar 24 at 10:21
















okay , but what if the data i want to show in the second select i need to get them from the database ?

– kisaw88
Mar 24 at 8:43





okay , but what if the data i want to show in the second select i need to get them from the database ?

– kisaw88
Mar 24 at 8:43













@kisaw88 ive made an edit

– Efrat Levitan
Mar 24 at 10:21





@kisaw88 ive made an edit

– Efrat Levitan
Mar 24 at 10:21

















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%2f55317574%2fhow-to-make-a-select-tag-in-html-only-shows-some-data-depending-on-another-se%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

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해