How I take the name in the selected option - forms

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 :)

Related

how to retrieve select options from my database

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.

AEM multifield data-sly-repeat ${item} not working

This has been driving me nuts - hoping someone can help me.
I have a multifield component called 'books' with a single textfield: 'title'.
Everything seems to be working; the dialog box contains the multifield then I add two title fields then enter 'title1' and 'title2'.
then in the HTML itself I go:
<div data-sly-repeat="${properties.books}">
<p>${item}</p>
<p>${itemList.index</p>
<p>${item.title}</p>
</div>
What I don't get is, ${item} correctly gives me:
{"title": "title1"} {"title": "title2"}
and ${itemList.index} correctly gives me: 0 1
but ${item.title} keeps coming up blank. I also tried ${item["title"]} and that comes up blank too.
What am I doing wrong here? In my desperation I contemplated using
<div data-title="${item}"></div>
and then using JS to process the JSON object but I don't really want to do that.
Someone help, please!
It looks like your books property is either a JSON array string or a multivalued property with each value being a JSON object string;
The easiest way to parse the property is via a JS model like the following:
You could simplify this script to match your specific case, I made it general to multi-value and non-multi-value string properties.
/path/to/your-component/model.js:
"use strict";
use(function () {
// parse a JSON string property, including multivalued, returned as array
function parseJson(prop){
if(!prop) return [];
var result =[];
if(prop.constructor === Array){
prop.forEach(function(item){
result.push(JSON.parse(item));
});
}
else {
var parsed = JSON.parse(prop);
if(parsed.constructor === Array){
result = parsed;
}
else result = [parsed];
}
return result;
}
var $books = properties.get("books", java.lang.reflect.Array.newInstance(java.lang.String, 1));
var books = parseJson($books);
return {
books: books
}
});
/path/to/your-component/your-component.html:
<sly data-sly-use.model="model.js"/>
<div data-sly-repeat="${model.books}">
<p>${item}</p>
<p>${itemList.index</p>
<p>${item.title}</p>
</div>

How to insert an option value into a select tag with CasperJS?

Well, the question is very self-explanatory.
Right now, I'm front of a form which has a select tag with a couple of options already. But I must insert a new one, with a different value that I will receive from a .json file.
The thing is: I haven't been able to find a suitable solution from the CasperJS documentation.
I've tried something like this:
this.fill('form.coworkerdiscountcode', {
'CoworkerDiscountCode.DiscountCode': ['Value1']
});
But no results. Any ideas?
Thanks in advance.
You can execute any javascript code by passing it to casper.evaluate like this:
casper.evaluate(function() {
var x = document.getElementById("coworkerdiscountcode");
var option = document.createElement("option");
option.text = "Kiwi";
x.add(option);
});

Lift - how to get default value of select using WiringUI

I have an issue with getting default value of select dropdown.
i have fruits val:
val fruits = List("apple", "banana", "other")
and i render a tr with:
<tr id={ theLine.guid }>
<td>
{
SHtml.ajaxSelect(fruits, Full(fruits(0)),
s => {
mutateLine(theLine.guid) {
l => Line(l.guid, l.name, s, l.note)
}
Noop
})
}
</td>
(...)
on page html is rendered correctly with option selected="selected", but when i try to save it to DB i get empty value of fruit. if i change option to 'other' and then i select it back to 'apple', it saves right value.
i add also a println function to addLine to see what values are in this vector, and there is empty value if i dont change fruit option, so i suppose that it is not problem when i process lines to save it to DB.
can you help me with this?
thanks
Gerard
Before you change your select option, you are not triggering the event that calls your function. The function is bound to onChange and that only gets fired when the value changes.
To fix, you could either: Start with an option like "Select a value". This would require the user to change the item, but is the only way to trigger the onchange.
If you don't want to do that, you could add a button and add your logic to a button click handler that would get called when submitted. Something like this should help - you'll need to bind it to your output, either inline as you provided, or via CSS Selectors:
var selected = Full(fruits(0))
SHtml.ajaxSelect(fruits, selected,
s => {
selected = Full(s)
Noop
})
SHtml.ajaxSubmit("Submit", () => {
mutateLine(theLine.guid) {
l => Line(l.guid, l.name, selected, l.note)
}
})

how to validate dropdown optionlist required functionality asp.net mvc

I have dropdown like this ,
<%= Html.OptionList("Worktype", new SelectList(new List<SelectListItem>{
new SelectListItem{Text = "--Select One--", Value = "0", Selected=true},
new SelectListItem{Text = "Fulltime", Value = "Full Time"},
new SelectListItem{Text = "Partime", Value = "Part Time"}}, "Value", "Text" )) %>
After selecting either fulltime or parttime it should submit, but because the default select is there, required validation is passing. I want the required validation for below two options. can anyone help me out.
thank you,
michael
SetValue empty instead of 0 for "--Select One--"
new SelectListItem{Text = "--Select One--", Value = string.Empty , Selected=true}
I suggest that you should not be adding optional label in SelectList or as SelectListItem. you have overloads for Html.DropDown and Html.DropDownListFor that would allow you to insert an optional label at the top of your dropdown list. Pleas check my answer to this question.
DO you want to fire any event only in case of full time and part time and in case of select you dont want anything to happen.
If this is what you want
$('#dropdownname').change(function () {
var dropdownValue = $(this).val();
if (dropdownValuetoString().length > 0)
{
Your Code here.........
}
});
dropdownname is the name of dropdown dropdownValue is what I m getting from dropdown list when index is changed.
I was filling the dropdown from a list and I was not using any value field
when u check the dropdownValue for select It will show blank and I m sure ur dropdown select list will always have a name.
Tell me if it helps you else I will try something different