We need to create html dropdown list dynamically. Following is our current code. Option1 and Option2 are strings that include all the items. The reason we have two strings is we split all the items in two groups.
<td>
<div id="dvGroup" style="display:none;" runat="server"></div>
</td>
string Option1 = "<optgroup label='Group1'><option>1</option><option1>2</option>";
string Option2 = "<optgroup label='Group2'><option>a</option><option>b</option><option>c</option>;
…
dvGroup.InnerHtml = "<Select name='ddGroup' id='ddGroup'>" + Option1 + Option2 + "</Select>";
But by setting InnerHtml will potentially bring some security problem. So I changed it to:
HtmlSelect select = new HtmlSelect();
select.ID = "ddGroup";
select.Name = "ddGroup";
select.ClientIDMode = ClientIDMode.Static;
dvGroup.Controls.Add(select);
select.Controls.Add(new LiteralControl(Option1 + Option2));
However, it threw following error at the line select.Controls.Add…
'System.Web.UI.HtmlControls.HtmlSelect' does not allow child controls.
How do I fix this? Another problem is, setting ClientIDMode to Static only make sure the id is not changed, but name still being added extra stuff. How do I make sure name is not changed either?
You can do this way:
HtmlSelect mHtmlSelect = new HtmlSelect();
mHtmlSelect.ID = "ddGroup";
mHtmlSelect.Name = "ddGroup";
mHtmlSelect.ClientIDMode = ClientIDMode.Static;
String Option1 = "Hello";
String Option2 = "World";
mHtmlSelect.Items.Add(new ListItem(Option1 + Option2));
this.Controls.Add(mHtmlSelect);
Output:
<select name="ddGroup" id="ddGroup">
<option value="HelloWorld">HelloWorld</option>
</select>
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'm trying to set up a simple select dropdown form with Flask. Based on the option chosen, I grab different data from my database, and display it back into a div on my html template. But I can't seem to get the Flask request to register any of the select options. When I print request.form or request.args, I always get empty Dicts. It's probably something simple I'm missing but I can't seem to find the problem. I've gotten this to work with several input and button forms, but I can't get it to work right for selects.
Here is a bit of my html template code, with the form and select. I've tried both GET and POST method in the form.
<div class="options" id="options">
<form class="form-horizontal container-fluid" role="form" method="GET" action="exploresn2.html">
<div class="form-group">
<label for="xaxis" class="col-sm-2 control-label">X-axis:</label>
<div class="col-sm-2">
<select name="xaxis" class="form-control" id="xaxis">
<option selected value="mjd" id="mjd">MJD</option>
<option value="seeing" id="seeing">Seeing</option>
<option value="airmass" id="airmass">Airmass</option>
<option value="hourangle" id="hourangle">Hour Angle</option>
</select>
</div>
</div>
</form>
</div>
In Flask, at first, I tried inside my app
import flask
from flask import request, render_template, send_from_directory, current_app
explore_page = flask.Blueprint("explore_page", __name__)
#explore_page.route('/exploresn2.html', methods=['GET','POST'])
def explore():
xaxis = str(request.args.get("xaxis", "any"))
.... [populate new xaxis variable based on request option selected]
exploreDict['xaxis'] = xaxis
return render_template("exploresn2.html", **exploreDict)
or
mjd = valueFromRequest(key='mjd', request=request, default=None)
if mjd:
mjds = [int(exp.platedbExposure.start_time/(24*3600)) for exp in exposures]
xaxis = mjds
exploreDict['xaxis'] = xaxis
to look for and grab a specific values, or in the first case, any value select. The valueFromRequest is function that grabs data from either GET or POST requests.
but this returns nothing, and then I tried just printing the entire request.args (or request.form) and it returns and empty Dict. Everything I try it still returns empty Dicts. So I'm missing some set up somewhere I think but the form looks right to me?
I'm not sure if this is the actual answer to this problem that I was looking for, but here is what I came up with. I couldn't actually get the Flask to accept a GET request into the original explore method defined, so I implemented a new method in Flask to return a JSON object
#explore_page.route('/getdata', methods=['GET','POST'])
def getData(name=None):
name = str(request.args.get("xaxis", "mjd"))
xaxis = 'populate new xaxis data based on value of name'
data = '(x,y) data array filled with values for plotting'
axisrange = range of x,y data for axes for plot
return jsonify(result=data, range=axisrange)
and then I just made a GET request via javascript to that method whenever the select button changes. So in my exploresn2.html template I have (using Flot for plotting)
$("#xaxis").change(function(){
var newname = $("#xaxis :selected").text();
var axes = plot.getAxes();
options = plot.getOptions();
var plotdata = plot.getData();
// make a GET request and return new data
$.getJSON($SCRIPT_ROOT + '/getdata', {'xaxis':$("#xaxis :selected").val()},
function(newdata){
// set new data
for (var i = 0; i < plotdata.length; ++i) {
plotdata[i].data = newdata.result[plotdata[i].label];
}
// set new axes
axes.xaxis.options.panRange = [newdata.range[0]-50,newdata.range[1]+50];
axes.xaxis.options.axisLabel = newname;
axes.xaxis.options.min = newdata.range[0]-1;
axes.xaxis.options.max = newdata.range[1]+1;
axes.yaxis.options.min = newdata.range[2];
axes.yaxis.options.max = newdata.range[3];
// redraw plot
plot.setData(plotdata);
plot.setupGrid();
plot.draw();
});
});
In java class i need to add list (E.g. list.getFirstName()) to the Label in wicket and that first name shoulb be a hyper link in the html. Below i have code of java and html
the html code
<tr >
<a wicket:id="gotoClass">
<span wicket:id="firstname"></span>
</a>
</tr>
the java class
Iterator<String> brds = list.iterator();
RepeatingView repeating = new RepeatingView("repeating");
add(repeating);
while (brds.hasNext())
{
AbstractItem item = new AbstractItem(repeating.newChildId());
repeating.add(item);
String contact = brds.next();
item.add(new Label("firstname", contact));
}
The above code works for me and i am able to add the label i.e if i have 10 first names in the list i am able to add 10 labels in html.But i try to add the anchor tag in html and
form.add(new BookmarkablePageLink<String>("firstname", gotoClass.class)); in java
then i get the below exception
Last cause: Unable to find component with id 'firstname' in [ [Component id = formname]]
Expected: 'formname.firstname'.
Found with similar names: ''
can anybody help me on this
Regards
Sharath
The link element must be son of the repeating element (as you did firstly). For example:
//...
BookmarkablePageLink<String> link = new BookmarkablePageLink<String>("firstname", gotoClass.class);
item.add(link);
link.add(new Label("firstname", contact));
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.
Yeah this works in FF and Chrome, but for some reason wont work in IE 8. I'm using a radio button to clear a section of a form.. that section is a select box, but I don't want to leave the area empty - instead I want to reset it to what it was when the page loaded. At the moment IE8 is just leaving me with an empty small select box.
Html:
<select id="city_select" disabled="true" name="location_id" onchange="show_search_button();"><option selected>Select your city</option> </select>
Javascript:
document.getElementById('city_select').innerHTML = "<option selected>Select your city</option>";
I've also tried using location_id instead of city_select in the javascript but to no avail.. innerText and innerContent dont work either.. though the inner.HTML works in IE8 for an earlier function, but that isnt trying to innerHTML into a form. Does anybody know why this works in Chrome and FF but not IE8? and is there a solution to this? Any help appreciated thanks!
Try this:
document.getElementById('city_select').options.length = 0;
Then create a new option and push it onto the options array of the select. The options are a tricky bit that don't behave like other markup.
Edited to show how to create an option:
var sel = document.getElementById('city_select').options.length = 0;
var opt = document.createElement('option');
opt.value = "Select Your City";
sel.options.push(opt);
sel.selectedIndex = 0;
There are supposed to be 4 way of assigning new options to a select element. Some work in some scenarios and others work in others. Look here - How to Add options to <SELECT>, in IE Windows Mobile 5
For me, Robusto's solution didn't work due to three reasons:
1) sel variable in the first line is assigned document.getElementById('city_select').options.length = 0; instead of simply holding the select element (for later use in 4th and 5th line) and then deleting options in a next line, like this:
var sel = document.getElementById('city_select');
sel.options.length = 0;
2) the 4th line sel.options.push(opt) (or later suggested sel.options[0] = opt) throws an Object doesn't support this property or method error. Instead use this:
sel.appendChild(opt);
3) apart from assigning values to options you must also assign text to display. You do it like this:
opt.innerText = "Select Your City - displayed";
Therefore, to sum up the whole piece:
var sel = document.getElementById('city_select');
sel.options.length = 0;
var opt = document.createElement('option');
opt.value = "Select Your City";
opt.innerText = "Select Your City - displayed";
sel.appendChild(opt);
sel.selectedIndex = 0;