This question already has answers here:
better way to load 2 dropdown in mvc
(5 answers)
Closed 5 years ago.
I know I can make a dropdown with a list of SelectedListItem> and #Html.DropDownList("someID") and os on..
My question is, what if you had 2 dropdowns, and the second dropdown depended on the selected item from the first dropdown?
How do you populate it? With JS? How would you go about it?
Would you change the populate with another list, change the whole dropdown or maybe have a partialview for each dropdown combination, so it's a matter of replacing with the right dropdown.
I have added NetFiddle example. Works here
I would suggest to use jquery $.getJson() to fill second dropdown without refresh to page. You can implement like following example.
//html
<select id="EventId" name="eventId">
<option value="1">option1</option>
<option value="2">option2</option>
<option value="3">option3</option>
</select>
<label>Second</label>
<select id="SecondDropdown">
</select>
// jquery
$("#EventId").on("change", function(){
showValue($(this).val());
})
function showValue(val)
{
console.log(val);
$.getJSON('#Url.Action("GetDropdownList", "Home")' + "?value=" + val, function (result) {
$("#SecondDropdown").html(""); // makes select null before filling process
var data = result.data;
for (var i = 0; i < data.length; i++) {
$("#SecondDropdown").append("<option>"+ data[i] +"</option>")
}
})
}
//controller
[HttpGet]
public JsonResult GetDropdownList(int? value)
{
List<string> yourdata = new List<string>();
if(value == 2)
{
yourdata.Add("option2a");
yourdata.Add("option2b");
yourdata.Add("option2c");
return Json(new { data = yourdata}, JsonRequestBehavior.AllowGet);
}
else
{
return Json(new { data = ""}, JsonRequestBehavior.AllowGet);
}
}
Related
I have a select2 multiselect embedded inside a modal, but when I click on the select2 container to make the dropdown appear for the options, the dropdown doesn't appear where it should - rather it is half way up the page. When I zoom out of the page, it appears in the correct place, but zooming back in returns it to the wrong place.
What is the cause of this and what would be the best way to rectify it?
Normal View
Zoomed Out
Some things that I've already tried as suggested by other questions and answers:
Removed tabindex="-1" from the modal
Added style="overflow:hidden;" to <div class="modal-body">
Added dropdownParent to attach the dropdown to the modal dialog, rather than the HTML body
Attempted to change the z value in CSS
Edit
I have the following script on my page to fire the select2 container. This one its own causes the dropdown to function correctly.
document.addEventListener("DOMContentLoaded", function() {
// Select2
$("#hazards").each(function() {
$(this)
.wrap("<div class=\"position-relative\"></div>")
.select2({
placeholder: "Select hazards or search by typing above",
dropdownParent: $(this).parent()
});
});
});
I also have another script that when my select2 changes, pushes results to a different multiselect. The start of the code is this:
window.addEventListener('DOMContentLoaded', () => {
$('#hazards').select2();
let hprops = document.getElementById('hp_codes')
// document.querySelector('#hazards').addEventListener('change', e => {
// The hazards addEventListener was not firing?
$('#hazards').on('change', e => {
let options = e.target.options;
let hazards = [];
for (var i = 0, l = options.length; i < l; i++) {
if (options[i].selected) {
hazards.push(options[i].value);
}
}
... then if hazards results is XXXX push YYYY
When the second code is commented out and just the first, everything works but the results aren't pushed to my second multiselect. When the first is commented out and the second is left alone, it doesn't work as intended with the dropdown displaying out of position. So it looks like a js conflict.
How would I combine the first into the second whilst maintaining the function of the second script?
In the second script the Select2 plugin is re-initiazed with no other options:
$('#hazards').select2();
The correct code should be:
window.addEventListener('DOMContentLoaded', () => {
// Select2
$("#hazards").each(function() {
$(this)
.wrap("<div class=\"position-relative\"></div>")
.select2({
placeholder: "Select hazards or search by typing above",
dropdownParent: $(this).parent()
});
});
let hprops = document.getElementById('hp_codes')
// document.querySelector('#hazards').addEventListener('change', e => {
// The hazards addEventListener was not firing?
$('#hazards').on('change', e => {
let options = e.target.options;
let hazards = [];
for (var i = 0, l = options.length; i < l; i++) {
if (options[i].selected) {
hazards.push(options[i].value);
}
}
... then if hazards results is XXXX push YYYY
I have a angularjs function which contain an array of values as follows
$scope.arrayVal = function()
{
var array = [{id:1,name:"one"},{id:2,name:"two"}];
}
I am passing this array to my dropdown which is in the html page and show the values in a dropdown as follows
<html>
<select ng-model="data.selected">
<option value="item.id,item.name" ng-repeat="item in array">{{item.id}} {{item.name}}</option>
</html>
What I want to achieve is I want to make user select multiple values from the dropdown and these selected values should be displayed according to the selected order below the dropdown. How can I achieve this with angularjs and html only. (I am not using any libraries other than angularjs)
Try this plunker https://next.plnkr.co/edit/6Nxaxz9hGNXospyT. You might have to tinker with it to get the outcome you want
<div ng-controller="ctrl">
<select ng-model="selectedItem" ng-options="item.name for item in items" ng-change="selectItem()">
</select>
<p ng-repeat="item in selectedItems">{{item.id}} - {{item.name}}</p>
</div>
angular.module('app', [])
.controller('ctrl', function($scope) {
$scope.items = [{id:1,name:"one"},{id:2,name:"two"}];
$scope.selectedItems = [];
$scope.selectedItem;
$scope.selectItem = function () {
var index = $scope.selectedItems.findIndex(function (fItem) { return $scope.selectedItem.id === fItem.id; });
console.log(index)
if (index > -1) $scope.selectedItems.splice(index, 1);
else $scope.selectedItems.push($scope.selectedItem);
};
});
Problem description:
I want to create an add-on on my google sheet (g-1). When the user opens the add-on, I want to immediately read another google spreadsheet (g-2) and populate dropdowns on g-1 based on those columns.
I have enabled the Googlesheet Api
in Code.js:
function readSpreadsheet() {
var questions = Sheets.Spreadsheets.Values.get("_ID_", "SHEET!A2:k").values
if (!questions) {
return 'No data found.';
} else {
return questions
}
}
the function above works, because if I add that to the title on the add-on I see the correct number of columns:
HtmlService.createHtmlOutputFromFile('QuestionBank').setTitle(readSpreadsheet().length)
or I can print the first row
readSpreadsheet()[0]
............
So far so good.
Now in my html file, QuestionBank.html,
Problem #1. I am not able to call readSpreadsheet, it returns undefined. var question_rows returns undefined.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script>
/**
read all rows, upon clicking on sync button. But this is not necessary if I can populate the dropdowns on load
**/
$(function() {
$('#sync').click(readSpreadsheet);
});
function readSpreadsheet() {
this.disabled = true;
$('#error').remove();
var question_rows = google.script.run
.withSuccessHandler(
function(textAndTranslation, element) {
element.disabled = false;
})
.withFailureHandler(
function(msg, element) {
element.disabled = false;
})
.withUserObject(this)
.readSpreadsheet();
for (var row = 0; row < question_rows.length; row++) {
alert(question_rows[row])
}
}
</script>
Problem #2: I have several dropdowns that I want their value to be unique value of the g-2 columns. I want those dropdowns to be populated when the add-on opens.
<select class="filter">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
so instead of Volvo, etc, I want the first column of my data, unique values
Problem #3: if on load is not possible, I can include a button to read the data and populate the dropdowns
<button class="sync-button" id="sync">sync</button>
How about the following answers?
Answer for Problem #1
google.script.run doesn't return values. When it uses values from google.script.run, in your case, textAndTranslation of withSuccessHandler is the returned value. So you can modify readSpreadsheet() as follows.
function readSpreadsheet() {
this.disabled = true;
$('#error').remove();
google.script.run
.withSuccessHandler(withSuccessHandler)
.withFailureHandler(
function(msg, element) {
element.disabled = false;
})
.withUserObject(this)
.readSpreadsheet();
}
function withSuccessHandler(textAndTranslation, element) {
element.disabled = false;
var question_rows = textAndTranslation;
for (var row = 0; row < question_rows.length; row++) {
alert(question_rows[row])
}
}
Answer for Problem #2
You can achieve it by putting google.script.run in $(function() {}). The sample is as follows. This is a sample. So please modify the variables to yours.
<select class="filter"></select>
<script>
$(function() {
google.script.run.withSuccessHandler(sample).readSpreadsheet();
});
function sample(data) {
for (var i=0; i < data.length; i++) {
$('.filter').append($('<option>').html(data[i]).val(data[i]));
}
}
Answer for Problem #3
Of course, you can set values to <select class="filter"></select> using a button.
If I misunderstand your questions, I'm sorry.
I am using auto-complete web service sing JSON, If i am selecting a list item that must not be appear again in auto-complete list;
JSON AJAX code:
select: function (event, ui) {
var terms = split(this.value);
if (terms.length <= 10) {
// remove the current input
terms.pop();
// add the selected item
terms.push(ui.item.value);
// add placeholder to get the comma-and-space at the end
terms.push("");
this.value = terms.join(", ");
return false;
}
else {
var last = terms.pop();
$(this).val(this.value.substr(0, this.value.length - last.length - 0)); // removes text from input
$(this).effect("highlight", {}, 1000);
$(this).addClass("red");
$("#warnings").html("<span style='color:red;'>Max skill reached</span>");
return false;
}
}
I am attaching screenshot also, please see here :
Like #Bindred mentioned in the comments to your question, an easier solution would be to use the Select2 jQuery library. It is not exactly what you are looking for, but as far as UX goes I think it would achieve a similar goal, and it is a breeze to get working.
I have added an example for you to use: https://jsfiddle.net/9cqc5876/9/
HTML
<select id="txtExpertise" multiple="multiple"></select>
JavaSript
$(document).ready(function() {
$("#txtExpertise").prop("disabled", "disabled");
// do your ajax request for data
//$.getJSON("../WebServices/WebServiceSkills.asmx/GetAutoCompleteData", function(data) {
// fake json data
var data = {"languages": ["Java", "C", "C++", "PHP", "Visual Basic",
"Python", "C#", "JavaScript", "Perl", "Ruby"]};
// populate the select
$.each(data.languages, function(key, val) {
$('#txtExpertise')
.append($("<option></option>")
.attr("value", key)
.text(val));
});
// activate the select2
$("#txtExpertise").select2();
$("#txtExpertise").prop("disabled", false);
//});
});
I want to show a hierarchy like in the image. I am able to create this hierarchy and I want to show the second level elements in a dropdown. However, while doing this the hierarchy value gets overrided with dropdown value and hence I am not able to show the third level of hierarchy.
This is my Dropdown html page:
Business Domain
OK
This is my controller:
controller('domainController', ['$scope', '$state', 'DomainNameService', function($scope, $state, DomainNameService) {
$scope.busdomain = DomainNameService.getBusDomainName();
/*For populating the domain values Which I am fetching from service
Attached the json image*/
var domainList=DomainNameService.getBusDomainName();
if(domainList!=null){
domainList[0].childNode.sort();
for (var i = 0; i < domainList[0].childNode.length; i++) {
if (domainList[0].childNode[i].name!=null) {
var name=domainList[0].childNode[i].name;
domainList[0].childNode.splice(i,1,name);//for replacing the object with name as I have to show name in dropdown list
$scope.busdomainname=domainList[0].childNode;//got the list of name but after this my service list also get overrides
$scope.busdomainname.sort();
break;
}
else
{
$scope.busdomainname=$scope.busdomain[0].childNode;//Added for getting business domain list
$scope.busdomainname.sort();
}
}
}
$scope.addSubDomainTree = function(val){
var varType = "busSubDomain";
var domain=[];
var busDomain=$scope.bussubdomain;
var parent = DomainNameService.getDomainName()[0];
//Done some code here to get the hierarchy
DomainNameService.setBusDomain($scope.statements);
$scope.domain.domainName = DomainNameService.getBusDomainName[0];
$state.go('BusDomainTree', null, { reload: true });//fix for refresh issue.
}
}
}
This is my Service Method
setChildBD: function(varType,childBD,value,val){
if(varType == "busSubDomain"){
this.error=undefined;
if(childSubDomainName.indexOf(childBD)==-1){
childSubDomainName.push(childBD);
var i= childDomainName.indexOf(val);
//Done this for replacing the object name with an array which contains the child.attached the image for its value
childDomainName.splice(i,1,value.childNode[0]);
}
},
getBusDomainName: function(){
return this.busDomainValue;//here the busDomainValue gets overrided with the controller list value
},
Can anyone please suggest how to resolve this issue?
Got a solution for this. I used var domainList=angular.copy($scope.busdomain) instead of var domainList=DomainNameService.getBusDomainName(); in controller.js –