I'm aware that value of a 'select' element can't be set from the 'value' attribute but via putting a 'selected' attribute in 'option'.
I made a simple if-else block comparing the flash value to put a 'selected' attribute on the right 'option'.
But it didn't work and always went to the else block, even though the 'value' returned by the flash is correct (The 'value' of 'select' in the else block).
#if(flash.get("""value""") == "value3"){
<select>
<option value="value1">value1</option>
<option value="value2">value2</option>
<option value="value3" selected>value3</option>
</select>
}
#if(flash.get("""value""").equals("value2")){
<select>
<option value="value1">value1</option>
<option value="value2" selected>value2</option>
<option value="value3">value3</option>
</select>
} else{
<select value="#flash.get("""value""")">
<option value="value1" selected>value1</option>
<option value="value2">value2</option>
<option value="value3">value3</option>
</select>
}
Is there actually a way to set value with Play Framework flashing?
EDIT
So, I tried using the custom helper blesior written.
#myOwnOptionHelper(optionValue: String, optionLabel: String) = {
<option value = "#optionValue"
#if(flash.get("""flashValue""") != null && flash.get("""flashValue""") == optionValue) { selected = "selected" } >#optionLabel</option>
}
And the select element
<select>
#myOwnOptionHelper("value1", "value1")
#myOwnOptionHelper("value2", "value2")
#myOwnOptionHelper("value3", "value3")
</select>
The result is, after flashing none of the option gets selected.
I tried rewritting the if block and displayed the flashvalue for the sake of testing.
#myOwnOptionHelper(optionValue: String, optionLabel: String) = {
<option value = "#optionValue"
#if(flash.get("""flashValue""").isDefined && "value2" == optionValue) { selected = "selected" } >#flash.get("""flashValue""") + #optionLabel</option>
}
And it worked. After flashing "value2" is selected and displayed the correct flashValue (ex: "value1 + value2"). I changed the "!= null" to ".isDefined" for testing because the value2 gets selected even though there is no flashing happening.
EDIT 2
Apparently flash.get("flashValue") is an [Option]. And that explains why it didn't trigger the right if-else block on the first place, because it returns a Some(value). By adding a match-case block, the custom helper works perfectly. Thanks to #blesior
#myOwnOptionHelper(optionValue: String, optionLabel: String) = {
<option value = "#optionValue"
#if(flash.get("flashValue").isDefined && (flash.get("flashValue") match {
case Some(x:String) => x
case _ => ""
}) == optionValue) { selected = "selected" } >#optionLabel</option>
}
Hm, two mistakes:
you are performing two blocks here - first is checking value3, second is checking value2 or else. That means, that if your value will be 3 you'll get TWO select fields
Why do you repeat all options in all blocks?
Instead you can write custom helper for the option tag, which will generate it and check flashed value against the value of option, you can do this even in the same view, although it would be better if it was done in tag template (docs)...
#myOwnOptionHelper(optionValue: String, optionLabel: String)={
<option value="#optionValue"
#if(flash.get("myFlashedValue") != null && flash.get("myFlashedValue")==optionValue) { selected="selected"} >#optionLabel</option>
}
<select name="someSelect">
#myOwnOptionHelper("value1", "One")
#myOwnOptionHelper("value2", "Two")
#myOwnOptionHelper("value3", "Three")
#myOwnOptionHelper("value4", "Four")
<!-- ... -->
#myOwnOptionHelper("value1000000", "Million")
</select>
Of course the helper can get other params i.e. if you don't want to pass the set value by flash, but reading it from some other model, etc.
Related
I'm trying to retrieve my select option from 3 databases located in a connection that's not my defaut connection.
but I'm getting an error : Undefined variable: marqs (View: C:\wamp64\www\projetSovac\resources\views\home.blade.php)
Here's my controller code
public function index()
{
$marques= DB::connection('sqlsrv2')->table('marque')->get();
$modeles = DB::connection('sqlsrv2')->table('Modele')->select( DB::raw('CodeModele'))->get();
$finitions = DB::connection('sqlsrv2')->table('finition')->select( DB::raw('CodeFinition'))->get();
$marqs = $marques->all(['marque']);
$models = $modeles->all(['CodeModele']);
$Finitions = $finitions->all(['CodeModele']);
return View::make('home')
->with(compact($marqs))
->with(compact($models))
->with(compact($Finitions));
return View('home');
}
and my home.blade.php code
<tr class="filters">
<th><input type="text" class="form-control daterangepicker-field" placeholder="Période d'analyse" disabled ></th>
<th><select class="form-control " disabled>
{!! Form::Label('marque', 'marque:') !!}
#foreach($marqs as $marque)
<option value="{{$marque->codeMarque}}">{{$marque->codeMarque}}</option>
#endforeach
</select>
</th>
Can you help identify the problem?
Thanks
compact($marqs) wants to have a string divining the variable you want to pass to the view. Use: compact('marqs') you can also combine your variables like compact('marqs', 'models', ....etc )
Also you are returning something 2 times now in the function this is not possible.
I would rewrite your function to be like this:
$marques= DB::connection('sqlsrv2')->table('marque')->get();
$modeles = DB::connection('sqlsrv2')->table('Modele')->select( DB::raw('CodeModele'))->get();
$finitions = DB::connection('sqlsrv2')->table('finition')->select( DB::raw('CodeFinition'))->get();
$marqs = $marques->all(['marque']);
$models = $modeles->all(['CodeModele']);
$Finitions = $finitions->all(['CodeModele']);
return View::make('home')->with(compact('marqs', 'models', 'Finitions'));
Assuming the first 6 lines get you the actual data all i changed was the return.
You might want to read up on how to use laravel models
https://laravel.com/docs/5.7/eloquent
I am not sure if u have defined any but it could make your code allot simpler.
I want show the name in a select input form when I select one of their options.
I create the select like that:
<select ng-model="electionEventId" ng-options="option.value as option.name for option in electionEvents">
</select>
And I catch the selected item value with {{ electionEventId }} but I want the name too of this election without realize another request. ¿Anyone can help me?
Thanks
You can bind the entire object in your model:
<select ng-model="electionEvent" ng-options="option as option.name for option in electionEvents">
</select>
My answer seems to be a little too verbose compared to other answers here, but I hope it helps!
I believe that you will have to traverse the electionEvents looking for the option that has value property equal to electionEventId.
For example, add this function to your controller:
$scope.getElectionEventName = function() {
var name;
angular.forEach( $scope.electionEvents, function( option ) {
// Don't process if the name has already been found
name != null && return;
if ( option.value === $scope.electionEventId ) {
name = option.name;
}
});
return name;
};
I haven't tested it, but I'm pretty sure it works!
Unfortunately there's no how to break the loop, so I put that line in the loop function :)
So I have a form on a page, and I am trying to pre load all the old value when the page loads up. But I am having some trouble achieving this for the select object.
I have the following scala code for generating the select:
<select id="networkInterfaces">
<option value="">First Available</option>
#for(interface <- networkInterfaces) {
#if(interface.Name == configs.last.networkInterfaceName) {
<option selected="selected" value="#interface.Name">#interface.DisplayName</option>
} else {
<option value="#interface.Name">#interface.DisplayName</option>
}
}
</select>
And when the page loads it does show that the selected network interface is selected. But the problem is if I change some of my other settings and submit it is returning the options html not the value. Is there a way to select the form value for the select in scala while the page is loading?
Is there something fundamental I am missing? Otherwise I will change the way it is handled to process display name rather than value...
EDIT
As I could not get this to work i changed the value to #interface.DisplayName and then converted it in code on the server. I would like to be able to do it properly but it doesnt seem to work.
Is that correct that <select id="networkInterfaces"> has no name specified?
and than you should use play helper #select and Form object. I think its the better way to handle form values.
#(form : Form[ABean])
#import helper._
#select(
form("configs.last.networkInterfaceName"),
options(networkInterfaces),
'_label -> "Interface Name",
'_default -> "First Available"
)
Further info about helper #select visit :
https://github.com/playframework/Play20/blob/master/framework/src/play/src/main/scala/views/helper/select.scala.html
Try this way.
<input type="hidden" id="groupIdRef" name="groupIdRef" value="#userRecord.groupId">
#input(field=userRecordform("groupId"),'_label -> "") { (id, name, value, htmlArgs) =>
<select id="groupId" name="groupId" #toHtmlArgs(htmlArgs)>
options.map { v =>
#for(g <- groups){
<option value=#g.id >#g.displayName</option>
}
}
</select>
}
// User JavaScript this onload function
window.onload = function() {
document.getElementById('groupId').value = document.getElementById('groupIdRef').value;
}
It is work for me.
I'm tring to implement the classic functionality of this select:elements depends on this other select:selection using Lift, E.g. select the country and get the possible states for the selected country.
The problem that I'm facing is that the "this.myForm.submit()" that I have inside the onchange of the select element is not firing the ajax request. If I use an input type"submit" it works perfectly.
Is this behaivior related with Lift framework? Is there a better way of implementing this kind of functionality using Lift libraries?
The relevant snipped code:
"name=distributionPoints" #> SHtml.select(distributionPoints, Empty, selectedDistributionPoint = _, "id" -> "the_distributionPoints") &
"name=devices" #> (SHtml.select(devices, Empty, selectedDevice = _, "id" -> "the_devices") ++ SHtml.hidden(process))
The view html:
<form class="lift:form.ajax">
<select name="distributionPoints" id="the_distributionPoints" onchange="submit()">
<option></option>
</select>
<br/>
Device:
<select name="devices" id="the_devices">
<option></option>
</select>
</form>
The rendered HTML:
<form id="F391649920812YBACEZ" action="javascript://" onsubmit="liftAjax.lift_ajaxHandler(jQuery('#'+"F391649920812YBACEZ").serialize(), null, null, "javascript");return false;">
<div>
Punto de distribución:
<select onchange="submit()" id="the_distributionPoints" name="F630704816482OLP514"></select>
<br />
Equipo:
<select name="F391649920817BLRJW5" id="the_devices"></select><input type="hidden" name="F391649920818HPS35E" value="true" />
<br />
</div>
</form>
[edit]
I finally got the solution. Like Chris mentioned I used ajaxSelect instead of just selects and instead of using setHtml there's a method called JsCmds.ReplaceOptions that does exactly what I was looking for.
You should understand that when using Ajax submit the page is not reloaded. So I would suggest you to use JsCmds.setHtml on server side to "reset" the second select element.
So, in fact the first select is an ajaxSelect which is meant to modify the second one (so it is not concerned by the hidden submit in my opinion). The second select is updated when the first one is changed, using 'selectPoint(s)'
Piece of Scala code
def selectPoint(s):JsCmd = {
selectedDistributionPoint = s;
newDevices:List[String] = findCorrespondingDevices(s);
JsCmds.setHtml("name=devices", SHtml.select(newDevices, Empty, selectedDevice = _, "id" -> "the_devices")) ++ SHtml.hidden(process))
}
"name=distributionPoints" #> SHtml.AjaxSelect(distributionPoints, Empty, s=> selectPoint(s), "id" -> "the_distributionPoints") &
"name=devices" #> (SHtml.select(Nil, Empty, selectedDevice = _, "id" -> "the_devices") ++ SHtml.hidden(process))
Piece of template code
<input name="distributionPoints" id="the_distributionPoints" onchange="submit()"/>
<input name="devices" id="the_devices"/>
I'm trying to find a way to get all values and label from drop-down in web page.
With label, I could use:
my #labels = $sel->get_select_options('s');
Return value is array of label in drop-down.
However, there's no equivalent method for getting all values.
Do you guys know how to do this?
As far as in Selenium 1 there is no direct API for this. However you could try this.
Consider a <select> like below.
<select name="mydropdown" id="optionset">
<option value="Milk">Fresh Milk</option>
<option value="Cheese">Old Cheese</option>
<option value="Bread">Hot Bread</option>
</select>
Below is the snippet in Java to retrieve values. You can get the logic from this snippet and implement it in Perl.
int no_of_options = selenium.getSelectOptions("//select[#id='optionset']").length
String option_values[] = new String[no_of_options];
for (int i=0;i<no_of_options;i++){
String value = selenium.getAttribute("//select[#id='optionset']/option["+i+"]/#value");
option_values[i] = value;
}
Hope this helps.