F# Canopy Select or Dropdown with Option Group - f#

How do you select an option or set the value in a select list that has option groups?
This method does not work: How to change a dropdown in an F# Canopy UI Testing Script
sample code:
<!DOCTYPE html>
<html>
<body>
<select id="test-select">
<optgroup label="Swedish Cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
</optgroup>
<optgroup label="German Cars">
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</optgroup>
</select>
</body>
</html>
Canopy Test:
open canopy
open runner
open Helpers
let all() =
context "OptGroup Tests"
let page = ""
ntest ("Testing page " + page) (fun _ ->
let mUrl = testpage.html
url mUrl
"#test-select" << "Audi"
)

Related

Cascade drop-down in MVC View without ajax?

I created one View Model with two Entities. I am passing this view model to my MVC Razor view which have two html drop-downs for each entity respectively.
<select class="form-control" id="Employees" name="Employees">
#foreach (var employee in Model.Employees)
{
<option value="#employee.Id"> #employee.name </option>
}
</select>
<select class="form-control" id="Tasks" name="Tasks">
#foreach (var task in Model.Tasks)
{
<option value="#task.Id"> #task.name </option>
}
</select>
Employee table is the parent of Task table. What I want is getting all the tasks which are related to particular employee only. e.g. In Employee drop-down I select John, then in Tasks drop-down I should get all the tasks which are relative to John. I know how to do this with ajax. I am looking for some other solution.
Is it possible to do something like this:
#foreach (var task in Model.Tasks.Where(x=>x.employeeId == 'Selected in previous dropdown'))
{
<option value="#task.Id"> #task.name </option>
}
Html block
<select id="Employees">
<option value="">Select Employee</option>
<option value="1">Employee1</option>
<option value="2">Employee2</option>
</select>
<select id="Tasks">
<option value="">Select Task</option>
<option value="1" data-employee="1">Employee1Task1</option>
<option value="2" data-employee="1">Employee1Task2</option>
<option value="3" data-employee="1">Employee1Task3</option>
<option value="1" data-employee="2">Employee2Task1</option>
<option value="2" data-employee="2">Employee2Task2</option>
<option value="3" data-employee="2">Employee2Task3</option>
</select>
Script scetion
<script>
$(document).ready(function () {
//on page ready hide all task option
$("#Tasks").find('option').hide();
// set task as empty
$("#Tasks").val('');
// onchange of employee Drop down
$("#Employees").on('change', function () {
var selectedEmployee = $("#Employees").val();
if (selectedEmployee != '') {
$("#Tasks").find('option').hide();
$("#Tasks option[value='']").show();
$('*[data-employee="' + selectedEmployee + '"]').show();
}
else {
// if employee not selected then hide all tasks
$("#Tasks").find('option').hide();
$("#Tasks").val('');
}
});
});
</script>
Please populate country and state drop down list using MVC way by for each loop and use above script. The mandatory case is you have to render all cascade options
<select class="form-control" id="Employees" name="Employees">
#foreach (var employee in Model.Employees)
{
<option value="#employee.Id"> #employee.name </option>
}
</select>
<select class="form-control" id="Tasks" name="Tasks">
#foreach (var task in Model.Tasks)
{
<option value="#task.Id" data-employee="#task.EmployeeId"> #task.name </option>
}
</select>

How to dynamically filter a select2 listing based on option class

I have a dropdown select of optional 'opportunities' (id="opportunities" in the example below) which I enhance using the select2 jquery plugin, and wish to dynamically filter this list in order to reduce the possible options presented to a user using a 2nd select dropdown (id="group-select" in the example below),
<label>
Select an opportunity<br>
<select id="opportunities" style="width:300px;" multiple="multiple" name="selected-opps" value="">
<optgroup label="Class A">
<option class="group-1" value="1a">Opportunity 1a</option>
<option class="group-2" value="2a">Opportunity 2a</option>
</optgroup>
<optgroup label="Class B">
<option class="group-1" value="1b">Opportunity 1b</option>
<option class="group-2" value="2b">Opportunity 2b</option>
</optgroup>
<optgroup label="Class C">
<option class="group-1" value="1c">Opportunity 1c</option>
<option class="group-2" value="2c">Opportunity 2c</option>
</optgroup>
</select>
</label>
<select id="group-select">
<option value="">Select a group</option>
<option value="group-1">group 1</option>
<option value="group-2">group 2</option>
</select>
</div>
<div id="select-opportunity"></div>
<script type="text/javascript">
(function( $ ) {
'use strict';
var opportunities = $('select#opportunities').select2({
dropdownParent: $('#select-opportunity')
});
})( jQuery );
</script>
I wish to be able to make a selection in the 2nd select, say 'group 1' and would like the select2 list to contain only 'group-1' items as per the option in the first select dropdown that have the grouo-1 class attribute.
I managed to solve this using 2 optional functionality provided by the select2 plugin, namely the ability to control the way items are built and displayed using the templating functionality, and using the programmatic control exposed in the plugin. I replaced the javascript appended below the example in the question with,
<script type="text/javascript">
(function( $ ) {
'use strict';
var opportunities = $('select#opportunities').select2({
dropdownParent: $('#select-opportunity'),
templateResult: formatItem
});
function formatItem (state) {
if (!state.id) { return state.text; }
//change the id of our select2 items
state._resultId = state._resultId+'-'+state.element.className;
var $state = $(
'<span class="opportunity-item ' + state.element.className + '">' + state.text + '</span>'
);
return $state;
}
$('select#group-select').change( function() {
//hide the unwanted options
var group = $('select#group-select option:selected').val();
//clear the styling element
$('style#select2-style').empty();
if(group){
//if a group is selected we need to hide all the others
$('select#group-select option').not(':selected').each(function(){
group = $(this).val();
if(group){
$('style#select2-style').append(
'li[id$="'+group+'"]{display:none;}'
);
}
});
}
//force the select2 to referesh by opening and closing it again
opportunities.select2('open');
opportunities.select2('close');
});
})( jQuery );
</script>
<style id="select2-style"></style>
I have also added an empty <style> element at the bottom in which I dynamically create the rules required to hide the unwanted items.
The logic
The code above creates templating function formatItem that the select2 plugin will use to format the items. The item state object is passed to the function which includes the unique id for each item.
This id is modified by appending the class of the corresponding option element.
When a group option is selected in the 2nd dropdown select (#group-select) a set of styling is created and appended to the bottom <style> element to hide all the elements whose id attributes end with the class names to be hidden, for example if one seleced group-1, the code will create a style to hide group-2 items,
li[id$="group-2"] {
display:none;
}
However for this to work we need to force the select2 dropdown to refresh to pick up the new styling and the only way I found for this work was to use the programmatic control of the plugin to 'open' and immediately 'close' the select2 dropdown.
ّshort Example .
window.templateResult = function templateResult(state) {
if (state.text.indexOf('ss') == -1)
return null;///hide
if (!state.id) { return state.text; }
var $state = $(
'<span>' + state.text + '</span>'
);
return $state;
};
//Init
$("#select1").select2({
templateResult: templateResult
});
ّFull Example below.
$(document).ready(function () {
//general custom filter
window.Select2FilterFunc = function (state) { return "Default" };
//general custom templateResult
window.templateResult = function templateResult(state) {
//call custom filter
var result = Select2FilterFunc(state);
if (result != "Default")
return result;
if (!state.id) { return state.text; }
var $state = $(
'<span>' + state.text + '</span>'
);
return $state;
};
//Init
$("#select1").select2({
templateResult: templateResult
});
//Add Custom Filter when opening
$('#select1').on('select2:opening', function (evt) {
//set your filter
window.Select2FilterFunc = function (state) {
if (state.text.indexOf('ss') == -1)
return null;//hide item
else
return "Default";
};
}).on('select2:close', function (evt) {
window.Select2FilterFunc = function (state) { return "Default" };
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
<select id="select1" class="js-states form-control" style="width:300px" multiple="multiple ">
<optgroup label="Alaskan/Hawaiian Time Zone">
<option value="AK" title="11111111">Alaska</option>
<option value="HI" title="2222">Hawaii </option>
</optgroup>
<optgroup label="Pacific Time Zone">
<option value="CA">California</option>
<option value="NV">Nevada</option>
<option value="OR">Oregon</option>
<option value="WA">Washington</option>
</optgroup>
<optgroup label="Mountain Time Zone">
<option value="AZ">Arizona</option>
<option value="CO">Colorado</option>
<option value="ID">Idaho</option>
<option value="MT">Montana</option>
<option value="NE">Nebraska</option>
<option value="NM">New Mexico</option>
<option value="ND">North Dakota</option>
<option value="UT">Utah</option>
<option value="WY">Wyoming</option>
</optgroup>
<optgroup label="Central Time Zone">
<option value="AL">Alabama</option>
<option value="AR">Arkansas</option>
<option value="IL">Illinois</option>
<option value="IA">Iowa</option>
<option value="KS">Kansas</option>
<option value="KY">Kentucky</option>
<option value="LA">Louisiana</option>
<option value="MN">Minnesota</option>
<option value="MS">Mississippi</option>
<option value="MO">Missouri</option>
<option value="OK">Oklahoma</option>
<option value="SD">South Dakota</option>
<option value="TX">Texas</option>
<option value="TN">Tennessee</option>
<option value="WI">Wisconsin</option>
</optgroup>
<optgroup label="Eastern Time Zone">
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
<option value="FL">Florida</option>
<option value="GA">Georgia</option>
<option value="IN">Indiana</option>
<option value="ME">Maine</option>
<option value="MD">Maryland</option>
<option value="MA">Massachusetts</option>
<option value="MI">Michigan</option>
<option value="NH">New Hampshire</option>
<option value="NJ">New Jersey</option>
<option value="NY">New York</option>
<option value="NC">North Carolina</option>
<option value="OH">Ohio</option>
<option value="PA">Pennsylvania</option>
<option value="RI">Rhode Island</option>
<option value="SC">South Carolina</option>
<option value="VT">Vermont</option>
<option value="VA">Virginia</option>
<option value="WV">West Virginia</option>
</optgroup>
</select>

MVC 4 .NET use jquery to modify the set of options in a dropdown list

I am using #Html.DropDownListFor to build a select object that looks like this:
<select id="GroupCode" name="GroupCode" tabindex="4"><option value="">Select One</option>
<option value="1">One thing</option>
<option value="17">Another thing</option>
<option value="7">A Third thing</option>
</select>
There comes a time when something else changes on the page, and I want to swap out the options, ending up with something like
<select id="GroupCode" name="GroupCode" tabindex="4"><option value="">Select One</option>
<option value="21">A completely different list</option>
<option value="17">A second item only</option>
</select>
However, when I set a breakpoint in the view and look at $('#GroupCode').html, I see something like this:
$('#GroupCode').html
function(a){return p.access(this,function(a){var c=this[0]||{},d=0,e=this.length;if(a===b)return c.nodeType===1?c.innerHTML.replace(bm,""):b;if(typeof a=="string"&&!bs.test(a)&&(p.support.htmlSerialize||!bu.test(a))&&(p.support.leadingWhitespace||!bn.test(
__proto__:
function() {
[native code]
}
arguments: null
caller: null
length: 1
prototype: {...}
where I was expecting to see something like the html above.
What I'm concerned about is whether, by replacing the html for #GroupCode, I will lose what was provided by the #Html.DropDownFor code.
Bottom line: what is a good way to replace the contents of a dropdown list like this?
See this Working fiddle example
HTML
<select id="GroupCode" name="GroupCode" tabindex="4">
<option value="">Select One</option>
<option value="1">One thing</option>
<option value="17">Another thing</option>
<option value="7">A Third thing</option>
</select>
<button id="testBtn" type="button">replace list with items with ids (21, 7)</button>
Script:
$(function () {
$(document).on("click", "#testBtn", function () {
$('#GroupCode').empty().append(
$('<option/>', {
value: "",
text: 'Select One'
}),
$('<option/>', {
value: "21",
text: "A completely different list"
}),
$('<option/>', {
value: "17",
text: "A second item only"
}));
});
});

Active Select option in Razor

I am working on a mobile version of my website. I have the option Action working however when I select, for example "About" it will take me to the correct page but the navigation bar goes back to the "Home" option. How do I go about doing this? thanks in advance
<select class="navbar-nav" style="width:250px" onchange='location.href = this.value'>
<option value="#Url.Action("Index", "Home")">Home</option>
<option value="#Url.Action("About", "Home")">About</option>
<option value="#Url.Action("Products", "Home")">Products</option>
<option value="#Url.Action("Services", "Home")">Services</option>
<option value="#Url.Action("Contact", "Home")">Contact</option>
</select>
<option selected="#ViewBag.Home" value="#Url.Action("Index", "Home")">Home</option>
<option selected="#ViewBag.About" value="#Url.Action("About", "Home")">About</option>
<option selected="#ViewBag.Products" value="#Url.Action("Products", "Home")">Products</option>
<option selected="#ViewBag.Services" value="#Url.Action("Services", "Home")">Services</option>
<option selected="#ViewBag.Contact" value="#Url.Action("Contact", "Home")">Contact</option>
and in your Home.cshtml
#{
ViewBag.Home = true;
}
and the other pages.
You can do it like this :
#{
Dictionary<string, string> menu = new Dictionary<string, string>();
menu.Add("Home", #Url.Action("Index", "Home"));
menu.Add("About", #Url.Action("About", "Home"));
menu.Add("Products", #Url.Action("Products", "Home"));
menu.Add("Services", #Url.Action("Services", "Home"));
menu.Add("Contact", #Url.Action("Contact", "Home"));
}
<select class="navbar-nav" style="width:250px" onchange='location.href = this.value'>
#foreach (var m in menu)
{
<option value="#m.Value" #(m.Value == Request.Url.AbsoluteUri ? "selected='selected' " : "")>#m.Key</option>
}
</select>

Dropdowns - selects dynamics-jquery

I'm try to do a dynamic selects with jQuery, example:
<select >
<option value=1> 1</option>
<option value=2> 2</option>
<option value=3> 3</option>
<option value=4> 4</option>
</select>
when I get value="x"
I would like to add
count=x;
for(int i=1 ; i<=count; i++){
<select > </select>
}
I have a problem with me code , this code add and add.. I dont want this
I just want add the 'x' select
http://jsfiddle.net/hqLPp/48/
If you want result on same page, then this might be useful,
this will be on your page, where you want result
<script>
function showSelectBox(str)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myresult").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","test.php?q="+str,true); // Pass value to another page Here->test
xmlhttp.send();
}
</script>
<select name='check' onchange="showSelectBox(this.value)">
<option>Select</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<div id="myresult">
</div>
Now On test.php Simply Call Value & put select box,
<?php
$q = $_GET['q'];
for($i=1 ; $i<=$q; $i++)
{
echo '<select > </select>';
}
?>

Resources