How can I get optgroup label when select an option in vuejs? - vuejs2

I want to get country group name when select option. I've countries with several group like
<template>
<select id="countries" v-model="country" #change="getCountryGroup($event)">
<optgroup label="Asia">
<option value="AF">Afghanistan</option>
<option value="AM">Armenia</option>
<option value="AZ">Azerbaijan</option>
...
<option value="BD">Bangaldesh</option>
...
</optgroup>
<optgroup label="Australia / Oceania">
<option value="AS">American Samoa</option>
<option value="AU">Australia</option>
<option value="CK">Cook Islands</option>
</optgroup>
<optgroup label="Africa">
<option value="DZ">Algeria</option>
<option value="AM">Angola</option>
<option value="AZ">Azerbaijan</option>
</optgroup>
<optgroup label="South America">
<option value="AR">Argentina</option>
<option value="BO">Bolivia</option>
<option value="BR">Brazil</option>
</optgroup>
<optgroup label="North America">
<option value="US">United States</option>
<option value="UM">United States Minor Outlying Islands</option>
<option value="CA">Canada</option>
</optgroup>
<optgroup label="Europe">
<option value="UK">United Kingdom</option>
<option value="AL">Albania</option>
<option value="AD">Andorra</option>
</optgroup>
</select>
</template>
Suppose when I select Bangladesh then I want to get optgroup name Asia. Here is my vueJs method
getCountryGroup: function(event){
console.log( event.target.getAttribute('label') );
}

Here are two steps
Get the selected index
Find the selected option
Select the parent element (optgroup) for the selected option
Finally, get the label (Country group)
getCountryGroup: function(event){
// 1. Get the selected index
const index = event.target.selectedIndex;
// 2. Find the selected option
const option = event.target.options[index];
// 3. Select the parent element (optgroup) for the selected option
const optgroup = option.parentElement;
// 4. Finally, get the label (Country group)
const countryGroup = optgroup.getAttribute('label');
console.log(countryGroup);
}
Here is the demo https://codepen.io/maab16/pen/KKKpJba

Related

v-model doesn't work in the select2 . I didin't know how i can solve. when i remove select2's class v-model works

when user choose some value , i have to hide some input .
I can't get value of 'advertisement_type' but i have to get this value.
May you help me in this case ?
select2 and jquert js later i added vuejs file.
when i remove select2 class , v-model works.
i have to use select2 and vue .
Thank you in advance.
<div class="kt-section kt-section--first">
<label>İlan Türü :</label>
<select v-model="advertisement_type" class="form-control kt-select2" id="kt_select2_2" name="advertisement_type">
<optgroup label="Arsa">
<option value="land">Arsa</option>
</optgroup>
<optgroup label="Konut">
<option value="apartment">Daire</option>
<option value="residence">Residence</option>
<option value="detached-house">Müstakil Ev</option>
<option value="villa">Villa</option>
<option value="farm-house">Çiftlik Evi</option>
<option value="pavilion">Köşk</option>
<option value="waterside">Yalı</option>
<option value="waterside-apartment">Yalı Dairesi</option>
<option value="summery">Yazlık</option>
<option value="prefabricated-house">Prefabrik Ev</option>
<option value="cooperative">Kooperatif</option>
</optgroup>
<optgroup label="İş Yeri">
<option value="">İş Yeri</option>
<option value="akaryakit-istasyonu"> Akaryakıt İstasyonu</option>
<option value="apartman-dairesi"> Apartman Dairesi</option>
<option value="atolye"> Atölye</option>
<option value="avm"> AVM</option>
<option value="bufe"> Büfe</option>
<option value="buro-ofis"> Büro & Ofis</option>
<option value="cafe-bar"> Cafe & Bar</option>
<option value="ciftlik"> Çiftlik</option>
<option value="depo-antrepo"> Depo & Antrepo</option>
<option value="dugun-salonu"> Düğün Salonu</option>
<option value="dukkan-magaza"> Dükkan & Mağaza</option>
<option value="enerji-santralleri"> Enerji Santralleri</option>
<option value="fabrika"> Fabrika</option>
<option value="garaj-park-yeri"> Garaj & Park Yeri</option>
<option value="imalathane"> İmalathane</option>
<option value="is-hani-kati-ofisi"> İş Hanı Katı & Ofisi</option>
<option value="kantin"> Kantin</option>
<option value="kir-kahvalti-bahcesi"> Kır & Kahvaltı Bahçesi</option>
<option value="kiraathane"> Kıraathane</option>
<option value="komple-bina"> Komple Bina</option>
<option value="maden-ocagi"> Maden Ocağı</option>
<option value="otopark"> Otopark</option>
<option value="pazar-yeri"> Pazar Yeri</option>
<option value="plaza"> Plaza</option>
<option value="plaza-kati"> Plaza Katı</option>
<option value="prefabrik-yapi"> Prefabrik Yapı</option>
<option value="radyo-istasyonu"> Radyo İstasyonu</option>
<option value="residence-kati-ofisi"> Residence Katı & Ofisi</option>
<option value="restoran-lokanta"> Restoran & Lokanta</option>
<option value="saglik-merkezi"> Sağlık Merkezi</option>
<option value="sinema-konferans-salonu"> Sinema & Konferans Salonu</option>
<option value="spa"> SPA</option>
<option value="spor-tesisi"> Spor Tesisi</option>
<option value="akaryakit-istasyonu"> Akaryakıt İstasyonu</option>
<option value="apartman-dairesi"> Apartman Dairesi</option>
<option value="atolye"> Atölye</option>
<option value="avm"> AVM</option>
<option value="bufe"> Büfe</option>
<option value="buro-ofis"> Büro & Ofis</option>
<option value="cafe-bar"> Cafe & Bar</option>
<option value="ciftlik"> Çiftlik</option>
<option value="depo-antrepo"> Depo & Antrepo</option>
<option value="dugun-salonu"> Düğün Salonu</option>
<option value="dukkan-magaza"> Dükkan & Mağaza</option>
<option value="enerji-santralleri"> Enerji Santralleri</option>
<option value="fabrika"> Fabrika</option>
<option value="garaj-park-yeri"> Garaj & Park Yeri</option>
<option value="imalathane"> İmalathane</option>
<option value="is-hani-kati-ofisi"> İş Hanı Katı & Ofisi</option>
<option value="kantin"> Kantin</option>
<option value="kir-kahvalti-bahcesi"> Kır & Kahvaltı Bahçesi</option>
<option value="kiraathane"> Kıraathane</option>
<option value="komple-bina"> Komple Bina</option>
</optgroup>
</select>
</div>
select2 is a jquery component. If you want to use select2 on vue.js you can use vue-select2 (see
https://vue-select.org/guide/values.html#getting-and-setting).
You can still use jQuery select2, but to get the value you need to use v-bind:name, and then when you submit, you can get the value inside Vue method and store it in data()
<select
v-bind:name="value"
id="value"
class="form-control"
>
data() {
return {
value: ''
}
}
this.value = $('#value').val();

How to display multi dimensional array into the multiple dropdown menu?

I am working in multiple dropdown menu. I have created an array from php and it looks like this:
https://api.myjson.com/bins/1emzic
Now from this json I want to display 4 dropdown menu.
In the first drop down I need to display
"FS A", "FS MT" and "FS OTHER"
Based on the first selection I need to display its related data in second third and fourth and so on as I add in my data.
This is what I need to bind
<select [(ngModel)]="first" id="first">
<option value="" disabled selected>select a category</option>
<option *ngFor="let item of first_list" [value]="item.category">{{item.category}}</option>
</select>
<br>
<select [(ngModel)]="second" id="second">
<option value="" disabled selected>select a category</option>
<option *ngFor="let item of first_list" [value]="item.category">{{item.category}}</option>
</select>
<br>
<select [(ngModel)]="third" id="third">
<option value="" disabled selected>select a category</option>
<option *ngFor="let item of first_list" [value]="item.category">{{item.category}}</option>
</select>
<br>
<select [(ngModel)]="four" id="four">
<option value="" disabled selected>select a category</option>
<option *ngFor="let item of first_list" [value]="item.category">{{item.category}}</option>
</select>
Here is my json data
{"FS A":{"BKK":{"BKK PULL":{"BKK SR1":[]},"BKK PUSH":{"BKK BCDE1":[],"BKK BAKE SE1":[]}},"RSM2":{"CHIANGMAI":{"CMI WS SE1":[],"CMI WS SE2":[]},"NORTH":{"NO1 SE1":[],"NO2 SEPLUS1":[],"NO3 SE1":[]},"ASM HOTEL BKK":{"BKK HO 5STARS1":[],"BKK HO SR1":[],"BKK HO SR2":[],"BKK HO SR3":[]}}},"FS MT":{"FSR1":{"FSA1":{"FS MAKRO":[]}},"FSR2":{"FSA2":{"FS FOODLAND":[],"FS GAS STATION":[],"FS VILLA MARKET JP":[],"SIAM FOODSERVICE":[]}},"FS LOCAL EXP":{"FS LOCAL EXP BKK":{"FS LOCAL EXP BKK":[]},"FS LOCAL EXP CD":{"FS LOCAL EXP CD":[]},"FS LOCAL EXP MM":{"FS LOCAL EXP MM":[]}}},"FS OTHER":{"FS OTHER":{"FS OTHER":{"FS OTHER":[]}}}}
Can anybody help me in this?
Here I am working:
https://stackblitz.com/edit/angular-dsylxi
You need to define the dependency on each other and based on that you can fill the models and its options. I will go for generic solution which can be applied with one function and works for all the drop-downs.
This are the model variables you need
wholeList:any = [];
first:any = [];
second:any = [];
third:any = [];
four:any = [];
firstModel = ''
secondModel = ''
thirdModel = ''
fourModel = ''
Then first you fill the first dropdown
this.testService.get_data().subscribe(
res => {
this.wholeList = res;
this.first = Object.keys(this.wholeList).map(a=> a);
console.log("res", this.first);
},
err => {
console.log(err);
}
)
Then in the view you need to fire a dependency which should look like this, note that I am defining dependency ChangeDropdown(this.wholeList[firstModel],'second')"
<select [(ngModel)]="firstModel" id="first" (ngModelChange)="ChangeDropdown(this.wholeList[firstModel],'second')">
<option value="" disabled selected>select a category</option>
<option *ngFor="let item of first" [value]="item">{{item}}</option>
</select>
<br>
<select [(ngModel)]="secondModel" id="second" (ngModelChange)="ChangeDropdown(this.wholeList[firstModel][secondModel],'third')">
<option value="" disabled selected>select a category</option>
<option *ngFor="let item of second" [value]="item">{{item}}</option>
</select>
<br>
<select [(ngModel)]="thirdModel" id="third" (ngModelChange)="ChangeDropdown(this.wholeList[firstModel][secondModel][thirdModel],'four')">
<option value="" disabled selected>select a category</option>
<option *ngFor="let item of third" [value]="item">{{item}}</option>
</select>
<br>
<select [(ngModel)]="fourModel" id="four">
<option value="" disabled selected>select a category</option>
<option *ngFor="let item of four" [value]="item">{{item}}</option>
</select>
and finally one common function which will update the models
ChangeDropdown = (value,dropdownName) =>{
this[dropdownName] = Object.keys(value).map(a=>{
return a;
})
}
Demo

Angular 5 dropdown setting default value

I am trying to preselect (set as default) value from Angular 5 drop down.
There are three values and I am trying to set the second one with this code but it doesn't work. I can't make any changes in the component, only in the template.
<select [(ngModel)]="declaration.media" name="media" (change)="onChangeMedia()" class="form-control form-control-sm" required>
<option *ngFor="let media of mediaArray" value="{{media.value}}" selected = "{{media.value == '02'}}">{{media.text}}</option>
</select>
UPDATE: if I remove [(ngModel)] then it works, but no two way binding.
<select>
<option value="" selected disabled hidden>Choose here</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
<option value="5">Five</option>
</select>

Unable to select a dropdown by value, visible text or index using selenium select command

I am unable to select an option dynamically using select commands by value, visible text or index, the workaround was i can use actions to click select and select a particular option or sending keys ArrowDown which is not a best way to implement because i want to use the same select function globally to automate UI all over the website like a keyword function.
sample script below:
WebElement selectElement = driver.findElement(By.xpath(.//*[#id='XYZPopUpForm:j_idt90:country_label']));
Actions selectItem = new Actions(driver);
selectItem.moveToElement(selectElement).click().perform();
selectItem.sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).perform();
selectItem.sendKeys(Keys.ENTER).perform();
HTML Code :
<tr>
<td>
<label for="XYZPopUpForm:j_idt90:country">Country</label>
</td>
<td>
<div id="XYZPopUpForm:j_idt90:country" class="ui-selectonemenu ui-widget ui-state-default ui-corner-all">
<div class="ui-helper-hidden-accessible">
<div class="ui-helper-hidden-accessible">
<select id="XYZPopUpForm:j_idt90:country_input" tabindex="-1" name="XYZPopUpForm:j_idt90:country_input">
<option selected="selected" disabled="disabled">--select--</option>
<option value="AF">Afghanistan</option>
<option value="AX">Åland Islands</option>
<option value="AL">Albania</option>
<option value="DZ">Algeria</option>
<option value="AS">American Samoa</option>
<option value="AD">Andorra</option>
<option value="AO">Angola</option>
<option value="AI">Anguilla</option>
<option value="AQ">Antarctica</option>
<option value="AG">Antigua and Barbuda</option>
<option value="AR">Argentina</option>
<option value="AM">Armenia</option>
<option value="AW">Aruba</option>
<option value="AU">Australia</option>
<option value="AT">Austria</option>
<option value="AZ">Azerbaijan</option>
<option value="BS">Bahamas</option>
<option value="BH">Bahrain</option>
<option value="BD">Bangladesh</option>
<option value="BB">Barbados</option>
<option value="BY">Belarus</option>
<option value="BE">Belgium</option>
<option value="BZ">Belize</option>
<option value="BJ">Benin</option>
<option value="BM">Bermuda</option>
<option value="BT">Bhutan</option>
<option value="BO">Bolivia, Plurinational State of</option>
<option value="BQ">Bonaire, Sint Eustatius and Saba</option>
<option value="BA">Bosnia and Herzegovina</option>
<option value="BW">Botswana</option>
<option value="BV">Bouvet Island</option>
<option value="BR">Brazil</option>
<option value="IO">British Indian Ocean Territory</option>
<option value="BN">Brunei Darussalam</option>
<option value="BG">Bulgaria</option>
<option value="BF">Burkina Faso</option>
<option value="BI">Burundi</option>
<option value="KH">Cambodia</option>
<option value="CM">Cameroon</option>
<option value="CA">Canada</option>
<option value="CV">Cape Verde</option>
</select>
</div>
Updated Script:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement selectElement = driver.findElement(By.id("XYZPopUpForm:j_idt90:country_input"));
((JavascriptExecutor)driver).executeScript("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].text == arguments[1]){ select.options[i].selected = true; } } ", selectElement, "Afghanistan");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("XYZPopUpForm:j_idt90:country_input")));
Screenshot of HTML to be more clear :
Seems you are finding incorrect element By.xpath your drop-down id looks like XYZPopUpForm:j_idt90:country_input but you are finding id XYZPopUpForm:j_idt90:country_label in By.xpath, May be that is the problem. Try using Select with correct drop-down id as below :-
WebElement selectElement = driver.findElement(By.id("XYZPopUpForm:j_idt90:country_input"));
//Now use select
Select select = new Select(selectElement);
select.selectByVisibleText("Afghanistan");
or
sel.selectByIndex(1);
or
sel.selectByValue("AF");
Edited :- If above does not work due to visibility of element you should try using JavascriptExecutor as below :-
WebElement selectElement = driver.findElement(By.id("XYZPopUpForm:j_idt90:country_input"));
((JavascriptExecutor)driver).executeScript("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].text == arguments[1]){ select.options[i].selected = true; } }", selectElement, "Afghanistan");
Hope it works...:)

Cannot select item from drop down in Selenium IDE

I'm writing a Selenium script that containing a drop down list.
<select id="seats" class="form-control" name="seats">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="3">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">22</option>
<option value="23">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
</select>
I've looked several answers in stack overflow to select a value form dropdown box and every time I failed. Even tried to get xpath from Selenium IDE. in that case I got same Relative XPath to both dropdown and the value.
Here is the code I'm currently using.
driver.findElement(By.id("seats")).click();
driver.findElement(By.xpath("//form[#id='side-form']/div/div/div/div/div[3]/div/select")).click();
Here what i need to select is 4. but, it selecting 1.
Can someone tell me how to select 4 from my dropdown. Any help would be appreciated.
Thanks in advance. :)
It is a bit awkward that your dropdown has 2 options with the same value. Anyway, here is one possible XPath which will return the last option of certain value, from dropdown with id equals 'seats' :
driver.findElement(By.xpath("//select[#id='seats']/option[#value='3'][last()]")).click();
Please try below sample
new Select(driver.findElement(By.id("seats"))).selectByValue("4");
Edit
WebElement select = driver.findElement(By.id("seats"));
List<WebElement> allOptions = select.findElements(By.tagName("option"));
for (WebElement option : allOptions) {
if(option.getText().equals("4")){
option.click();
}
}