I have the following html dropdown in an Angular view:
HTML
<select>
<option value="Func 1">
<button class="btn ui-button ui-widget ui-state-default ui-corner-all" ng-click="callFunc1()">
<span class="btn-icon add"></span>
Func 1
</button>
</option>
<option value="Func 2">
<button class="btn ui-button ui-widget ui-state-default ui-corner-all" ng-click="callFunc2()">
<span class="btn-icon add"></span>
Func 2
</button>
</option>
</select>
JS
$scope.callFunc1= function() {
alert('func 1');
};
$scope.callFunc2 = function() {
alert('func 2');
};
My problem is the functions attached to the ng-clicks aren't firing.
Can anyone tell me why?
I would suggest following something similar to this format. How you decide to pass around the data is completely up to you, but this should provide some guidance.
<div ng-controller="testCtrl">
<!--js-->
<script>
app.controller('testCtrl',function($scope){
$scope.selectValue = undefined;
$scope.event = function(){
console.log($scope.selectValue)
}
})
</script>
<div>
<select ng-model="selectValue" ng-change="event()">
<option value="test">something</option>
<option value="test2">something2</option>
</select>
</div>
</div>
Make sure all html is within your controller and that you've initiated the body of your html with ng-app (not shown above). ng-change will fire the expression that you pass it when you make a change to the select input. Also, ng-change requires that you assign an ng-model value to your element.
Related
I have a form which i need to select one value and i am setting default value to the select dropdown as "select".
If dropdown selected value = "select" then save button should be disabled.
Other wise enabled.
My Html :
<select class="form-control" name="studymode" ng-model="data.studymode" ng-options="studymode.id as studymode.name for studymode in studymodes" ng-disabled="!eEditMode[$index]">
<option value="">Select Mode of Study</option>
</select>
<button ng-show="saveButton && eEditMode[$index]" class="btn btn-primary" type="submit" ng-click="update('education',$index);" ng-disabled="eForm.$invalid || dataLoading">Save Changes </button>
You can do it this way as well:
function validate() {
var submitbutton = document.getElementById("submitbutton");
var ddl = document.getElementById("selector");
var selectedValue = ddl.options[ddl.selectedIndex].value;
if (selectedValue == "select") {
submitbutton.disabled = true;
} else {
submitbutton.disabled = false;
}
}
$( ".form-control" ).change(function() {
validate();
});
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
</head>
<body>
<select id="selector" class="form-control" name="studymode" ng-model="data.studymode" ng-options="studymode.id as studymode.name for studymode in studymodes" ng-disabled="!eEditMode[$index]">
<option value="select"></option>
<option value="">Select Mode of Study</option>
</select>
<button id="submitbutton" disabled ng-show="saveButton && eEditMode[$index]" class="btn btn-primary" type="submit" ng-click="update('education',$index);" ng-disabled="eForm.$invalid || dataLoading">Save Changes </button>
html angularjs shareeditflag asked 1 min ago Kalaiyarasi M 408 add a comment | show 1 more comment
</body>
</html>
This works fine. BTW I needed to add some id for the selector and button to access them through javascript.
I hope this helps you.
Try this
<form name="eForm">
<select class="form-control" name="eForm.studymode" ng-model="data.studymode" ng-options="studymode.id as studymode.name for studymode in studymodes" ng-disabled="!eEditMode[$index]" required> <option value="">Select Mode of Study</option> </select> <button ng-show="saveButton && eEditMode[$index]" class="btn btn-primary" type="submit" ng-click="update('education',$index);" ng-disabled="eForm.$invalid || dataLoading">Save Changes </button>
Instead of disabling your save button on a certain value, as you are using a form, mark your select as required. And use a placeholder for your select.
Then use ng-disabledin conjunction with $invalid to disable you button until it has a selected value.
Here's an example, apply this to your button :
ng-disabled="form.userForm.$invalid"
Where form.userForm is the nameof your form.
With this your button will be enabled only if a value is selected.
See this arcticle for information on `$invalid
You can use data.studymode.length < 1 in your ng-disabled as shown below:
<select class="form-control" name="studymode" ng-model="data.studymode" ng-options="studymode.id as studymode.name for studymode in studymodes" ng-disabled="!eEditMode[$index]">
<option value="">Select Mode of Study</option>
</select>
<button ng-show="saveButton && eEditMode[$index]" class="btn btn-primary" type="submit" ng-click="update('education',$index);" ng-disabled="eForm.$invalid || dataLoading || data.studymode.length < 1">Save Changes </button>
I am using Flat-UI (http://designmodo.github.io/Flat-UI/) to aid with the front-end elements of a small MeteorJS App I am building.
The problem I am having is that the Select Dropdown is not working. And I would like to know how to get the option value.
Please see the below Markup:
<div class="select2-container form-control select select-primary" id="s2id_autogen1">
<a href="javascript:void(0)" class="select2-choice" tabindex="-1">
<span class="select2-chosen" id="select2-chosen-2">My Profile</span>
<abbr class="select2-search-choice-close"></abbr>
<span class="select2-arrow" role="presentation">
<b role="presentation"></b>
</span>
</a>
<label for="s2id_autogen2" class="select2-offscreen"></label>
<input class="select2-focusser select2-offscreen" type="text" aria-haspopup="true" role="button" aria-labelledby="select2-chosen-2" id="s2id_autogen2">
</div>
<select class="form-control select select-primary" data-toggle="select" tabindex="-1" title="" style="display: none;">
<optgroup label="Profile">
<option value="0">My Profile</option>
<option value="1">My Friends</option>
</optgroup>
<optgroup label="System">
<option value="2">Messages</option>
<option value="3">My Settings</option>
<option value="4">Logout</option>
</optgroup>
</select>
This is what we have in flatui document. But the select dropdown is not functioning, can someone help me to make it work and pull value from the options?
We have made a custom select due to the inability of styling the system one. It is based on the Select2 plug-in with the source slightly modified to meet Bootstrap naming convention.
Select2 3.5.2
Check this:
http://select2.github.io/select2/
$("#selectList")
.on("change", function(e) { log("change "+JSON.stringify({val:e.val, added:e.added, removed:e.removed})); })
If you read the documentation well you will notice you have manually add a select2 call to transform all selects.
E.g. like this:
$( document ).ready(function() {
$("select").select2({dropdownCssClass: 'dropdown-inverse'});
});
I am creating an Add-On for Google Spreadsheet. When click the search button nothing seems to happen. How does one go about debugging this ?
I am using the Google Apps Script to create the addon. I am using the example of QuickStart, with some changes to reflect my requirements.
Here is my Code
<div class='sidebar branding-below'>
<form>
<div class="block col-contain">
<div class="block form-group">
<div>
<label for="Query-txt"><b>Query Term</b></label><br/>
<input type="text" name="querytxt" id="Query-txt" value="" class="width-100" />
</div>
<br/>
<div>
<label for="Channel-id-dd"><b>Channel</b></label><br/>
<select id="Channel-id-dd" class="width-100">
<option value="UCpZG4Vl2tqg5cIfGMocI2Ag">CFC India</option>
<option value="UCPPkrC9R5ED2R2JRTaQgETw">NCCF Church</option>
<option value="UCL8wQnv6qB7rZtEYfY3vPtw">River of Life Christian Fellowship</option>
</select>
</div>
<br/>
<div>
<label for="Region-Code-dd"><b>Country Code</b></label><br/>
<select id="Region-Code-dd" class="width-100">
<option value="AU">AU - Australia</option>
<option value="GB">GB - United Kingdom</option>
<option value="US">US - USA</option>
<option value="UAE">UAE - United Arab Emerates</option>
</select>
</div>
<br/>
<div>
<label for="Safety-Type"><b>Safety</b></label><br/>
<select id="Safety-Type" class="width-100">
<option value="moderate">moderate</option>
<option value="strict">strict</option>
<option value="none">none</option>
</select>
</div>
<br/>
<div class="block" id="button-bar">
<button class="blue" id="run-Query">Search</button>
</div>
</div>
</div>
</form>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
$(document).ready(function (){
$('#run-Query').click(function () {
Logger.log("Starting");
var query = $('#Query-txt').val();
var channelid = $('#Channel-id-dd').val();
var countryCode = $('#Region-Code-dd').val();
var safety = $('#Safety-Type').val();
google.script.run
.withSuccessHandler(
alert("success");
)
.withFailureHandler(
alert("failure");
)
.withUserObject(this)
.search_all(query, safety, channelid, countryCode);
Logger.log("Complete");
});
});
</script>
I see two problems with your code. Firstly, Logger.log() is an Apps Script server-side method. It is designed to be called from server-side code, for example in the Code.gs file. What you are writing above is the client-side javascript that is executed on the end user's browser. To log here, you do the same thing you would in regular javascript programming and use the console.log() method instead. This will write logging statements to the javascript console (accessible with Ctrl+Shift+J in Chrome).
Secondly, withSuccessHandler and withFailureHandler take as a parameter the method to call during success and failure scenarios and no parameters. The method you define will get the parameter automatically. Notice how they are used in the Quickstart:
...
.withSuccessHandler(
function(returnSuccess, element) {
element.disabled = false;
})
...
To alert on success, like you have above, you need to use something like this:
...
.withSuccessHandler(
function(returnVal, element) {
alert("Call was successful with return value: " + returnVal);
}
)
...
Using these two methods, you should have the tools necessary to debug your code.
In Chrome these do not behave as expected, the style options doesn't work if a size attribute is used. How can I make this work?
<select size=2>
<option>1</option>
<option selected>2</option>
<option style="display: none;">3</option>
<option>4</option>
</select>
<br/>
<select>
<option>1</option>
<option selected>2</option>
<option style="display: none;">3</option>
<option>4</option>
</select>
http://jsfiddle.net/D9X9U/1/
My end game here is to have a search box that filters a select list of some kind.
I ended up using a hidden select list and moving elements between the two. If someone finds this and can improve this please do so, I will mark yours as the answer. I'm more or less fumbling my way through javascript and jquery so I'm sure a lot can be done to make this better.
http://jsfiddle.net/x6cfF/1/
Edit: anyone who finds this looking for a solution, there is a better one here https://codereview.stackexchange.com/questions/23706/better-way-to-filter-select-list/23710?noredirect=1#23710
<input type="search" id="SearchBox" />
<br />
<div class="scrollable" id="CustomerSelectDiv">
<select size=2 class="scrollableinside" id="CustomerSelect">
<option value=100>test</option>
<option value=101>test1</option>
<option value=102>test2</option>
<option value=103>test3</option>
</select>
</div>
<div style="display: none;">
<select id="CustomerSelectHidden"></select>
</div>
<script type="text/javascript">
window.onload=function() {
var $options = $('#CustomerSelect option');
document.getElementById("SearchBox").onkeyup = function () {
var $HiddenOptions = $('#CustomerSelectHidden option');
$HiddenOptions.each(function (index, value) {
document.getElementById('CustomerSelect').appendChild(this);
});
var search = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();
var element = $options.filter(function () {
var text = $(this).text().replace(/\s+/g, ' ').toLowerCase();
return !~text.indexOf(search);
}).appendTo(document.getElementById('CustomerSelectHidden'));
}
}
</script>
Suppose I have a list box with different items, each with unique values, and an 'Enter' button below it. I want to be able to get the value of the selected item at the time of click, and then when have the button property:
ONCLICK="window.location.href='http://somewebsite.com/somefile.php?id="thisvalue"'"
So for example, something like----
<SELECT NAME = ParticipantList STYLE = "WIDTH: 187" SIZE = 18>
<OPTION VALUE='hi'> hello </OPTION>
<SELECT>
<INPUT TYPE="submit" VALUE="Info" ONCLICK="window.location.href='http://helloworld.com/this.php?="hi"'"/>
Can anyone help me figure this out? Much appreciated.
HTML forms are designed to do exactly this when their method is GET:
<form action="http://helloworld.com/this.php" method="get">
<select name="ParticipantList">
<option value="hi">Hello</option>
</select>
<input type="submit">
</form>
This will send the user to http://helloworld.com/this.php?ParticipantList=hi. No JavaScript required.
The Javascript
<script type="text/javascript">
function doAction(){
var selected = document.getElementById('ParticipantList').value;
if (selected == 'hello'){
window.location.href = 'http://somewebsite.com/somefile.php?id=1';
} else if (selected == 'bye'){
window.location.href = 'http://somewebsite.com/somefile.php?id=2';
} else {
alert('unknown option selected');
}
}
</script>
The HTML
<select name="ParticipantList" id="ParticipantList">
<option value="hello">hello</option>
<option value="bye">bye</option>
</select>
<input type="button" name="action" id="action" value="Submit" onclick="doAction()" />