How to set default selected option in very big select tag with many options in Laravel? - laravel

I have 3 select tags with a lot of options inside them. One for year, one for month and one for day.
The question is ... How to set selected on the option I want.
I was thinking of this solutions:
Pass the options as array to the view. After that forech them in the select tag and with "#if" check every single one if its the right one and set "selected:selected".
But in my case this isnt good solution because I have many options ... that means the array will be very very big.
Put if statement in every option in the html and check if its te right one and set "selected:selected" to that option. This is also a bad solution ... there will be like 200+ if statements.
Maybe create 3 db tables with this options inside. Getting them in the controller method, foreaching them in the select tag and checking them with if statement and setting "selected" to the right one.
I really dont know if Im thinking in the right way. I will apriciate some help.

what you r doing is right here is a solution that it did worked for me, i have a user city, and when i want the user to update the city i show it as selected
just pass the arrays to the view then :
#foreach ($citys as $city)
#if (Auth::user()->city_id == $city->id)
<option value="{{$city->id}}" selected="selected">{{$city->name}}</option>
#else
<option value="{{$city->id}}">{{$city->name}}</option>
#endif
#endforeach
here is a short version u can use
#foreach ($citys as $city)
<option value="{{$city->id}}" #if (Auth::user()->city_id == $city->id) selected="selected" #endif > {{$city->name}}</option>
#endforeach

Related

Coldfusion, Dropdown menu, Can a single option value contain multiple variables?

I am writing a CFM file to display a dropdown menu, in the dropdown options I want Var1 [Var2, Var3] to display for each line. I tried both concatenation and commas, tried a mix of brackets and quotes and cannot find the right combination. Using Oracle SQL developer. I am a student taking a database management class. I read through Adobe's site and cannot find an example like this. I also combed stack, but everything is PHP and javascript related. The result should have a dropdown, where each selection shows: cus_code with corresponding [cus_fname, cus_lname] for each line. Below is the query that pulls the data needed and the dropdown menu. The place I am having trouble is in the output, where each line from the dropdown should have 3 variables. Every attempt I make causes an error.
<!--Here is my query: grab customer code, last name, first name from customer table, join invoice table on customer code-->
<CFQUERY NAME="INVOICESEARCH" DATASOURCE="ORCL">
SELECT DISTINCT levine04.CUSTOMER6.CUS_CODE, CUS_LNAME, CUS_FNAME
FROM levine04.CUSTOMER6, levine04.INVOICE6
WHERE levine04.CUSTOMER6.CUS_CODE = levine04.INVOICE6.CUS_CODE;
</CFQUERY>
<!--Here is my dropdown:-->
<TR>
<TD ALIGN="right">INV_NUMBER</TD>
<TD>
<INPUT TYPE ="text" NAME="INV_NUMBER" SIZE="10" MAXLENGTH="10">
</TD>
</TR>
<TR>
<TD ALIGN="right">CUS_CODE, [CUS_FNAME, CUS_LNAME]</TD>
<TD>
<SELECT NAME="CUS_CODE" SIZE=1>
<OPTION SELECTED VALUE="ANY">ANY
<CFOUTPUT QUERY="INVOICESEARCH">
<OPTION VALUE="#INVOICESEARCH.CUS_CODE#" + "#INVOICESEARCH.CUS_LNAME#" +"#INVOICESEARCH.CUS_FNAME#"> #CUS_CODE# + #CUSLNAME# + #CUSFNAME#
</CFOUTPUT>
</SELECT>
</TD>
</TR>
Got it! But maybe this will help someone else..
<OPTION VALUE="#INVOICESEARCH.CUS_CODE#"> #CUS_CODE#[#CUS_LNAME#, #CUS_FNAME#]

Doesn't select the Right option in dropdown

Protractor select the wrong option in dropdown.
I have to:
select the first dropdown to open another dropdown (in my situation: select Payment Method (1st dropdown) to open the 2nd dropdown - available credit cards):
The IMPORTANT moment: if the 1st dropdown has a default value - the 2nd dropdown CAN NOT be visible.
My piece of code had different variation:
let paymentDropdown = element(by.name("payment-method"));
let dynamicBlock = $$(".dynamic").first();
let useThisCardButton = element(by.name("do-add-payment-method»));
let paymentConfigBlock = element(by.id("payment-config-container"));
browser.wait(EC.visibilityOf(paymentDropdown), 5000);
$("#payment-method-0 > option:nth-child(2)").click();
browser.wait(EC.visibilityOf(dynamicBlock), 5000);
useThisCardButton.click();
browser.wait(EC.visibilityOf(paymentConfigBlock), 5000);
So, I’ve tried for the first dropdown:
1. $("#payment-method-0 > option:nth-child(2)").click();
2. element(by.name("payment-method"))
.all(by.tagName('option'))
.get(1)
.click();
3. element(by.cssContainingText('option', 'Credit Card')).click();
4. element(by.name("payment-method")).$('[value="cc"]').click();
Html is:
<select name="payment-method" id="payment-method-0" style="display: inline-block;">
<option value="0">Select Payment Method</option>
<option value="cc">Credit Card</option>
</select>
And Nothing! I don’t know how does it work, but the 1st dropdown has Default Value, and the 2nd dropdown is VISIBLE! Then I click on «Use this method» button and got Fail!
1 test from 10 tests choose the right 1st value, so test passed. But other 9 trials are failed. Why? How to do it correct and how to be sure that it will select the right option?
Try the below one. Here you can select the values without opening the drop down.
var firstDrop = element(by.css('#payment-method-0'));
browser.wait(EC.elementToBeClickable(firstDrop), 5000);
firstDrop.sendKeys('Credit Card');
browser.wait(EC.visibilityOf(dynamicBlock), 5000);
Hope it helps you

In Angular JS Kendo Drop down not setting the model value

HI I have following Kendo Dropdown, even though I have a value in ng-model, the dropdown is not selecting the default values.
<select name="myDropDown"
id="myDropDown"
class="width-90"
kendo-drop-down-list k-data-text-field="'key'"
k-data-value-field="'value'"
k-data-source="myDataSource"
ng-model="model.selectedProperty"></select>
model.selectedProperty does have the value . However the UI does't select the options.
DataSource Values:
[{"key":"Critical","value":0},{"key":"High","value":1},{"key":"Medium","value":2},{"key":"Low","value":3}]
model.selectedProperty = 1
The above one should have selected the second option, but the dropdown selects none
Well I have to make the selected value same object as in the list:
$scope.model.selectedProperty = {"key":"Critical","value":0}
And I updated the select tag to,
<select name="myDropDown"
id="myDropDown"
class="width-90"
ng-model="model.selectedProperty"
ng-options="option.key for option in myDataSource track by option.value">
</select>

Xpath to Scrape Size Information with Scrapy

I aim to scrape the following bit of code:
Select Size:</b>
<select name="option[225]">
<option value=""> --- Please Select --- </option>
<option value="480">UK 11 </option>
<option value="478">UK 8 </option>
<option value="477">UK 7 </option>
</select>
I'm facing two problems:
1) the value in "option[225] isn't constant for all the pages on that site and changes from each product.
2) Is there anyway, I can store the data with semi-colon separators between each value.
I wish for the data to be displayed in the following manner:
UK 11;UK 8;UK 7
>>> ';'.join(map(str.strip, sel.xpath('//option[string-length(#value)!=0]/text()')))
'UK 11;UK 8;UK 7'
// for selecting all option tags starting from the root of the document, string-length to filter out first empty name option and ';'.join(...) to join generator elements with ; between them
If "Select Size:" is something that is constant before the select/options you want to select, you can try an XPath expression like this:
xpath_expression = """//b[contains(., "Select Size:")]
/following-sibling::select[starts-with(#name, "option[")][1]
/option[#value != ""]/#value"""
Then, as #Guy suggests, you can use:
u";".join([val.strip() for val in sel.xpath(xpath_expression).extract()])

Select a value from a select List by value element

recently I came upon an issue while using page object that I could not find an answer to.
When using a select list to choose an option. I need to select by partial text or by attribute. here is an example:
<select id="custcol95" name="custcol95">
<option selected="" value="">- Select -</option>
<option value="5">Monthly Basic Plan - $27.99</option>
<option value="1">Monthly Business Plan - $54.99</option>
</select>
I tried:
select_list(:planPurchase, :id => 'custcol95')
I can do this:
selectCloudPlan_element.option(:value, CLOUD_PLANS['PersonalMonthly']).select
but that is deprecated.
I have yet to see a select by value option or by index anywhere on the web. Does one exist?
Also if there was a search by partial text that would fix my issue.
Thanks in advance.
The page object gem already supports selecting by partial text:
page.planPurchase = /Basic/
puts page.planPurchase
#=> "Monthly Basic Plan - $27.99"
Selecting by value is also supported by using select_value:
page.planPurchase_element.select_value('5')
puts page.planPurchase
#=> "Monthly Basic Plan - $27.99"
The page object gem supports selecting options by index using []. Note that you have to use .click as there is no .select. The index is 0-based.
page.planPurchase_element[2].click
puts page.planPurchase
#=> "Monthly Business Plan - $54.99"

Resources