I have 2 instances of the <date-picker>, in which the first captures the start date (starts_at) and the second captures the end date (ends_at).
As per the design I have in mind, I would like to disable the ends_at instance and then re-enable it once the starts_at has been applied.
This is my starts_at instance:
<date-picker v-model="starts_at" #input="enableEndsAt()" valueType="format"></date-picker>
This is my ends_at instance:
<date-picker id="endsAt" v-model="ends_at" disabled #input="checkDate()" valueType="format"></date-picker>
This is what I have tried so far. When you change the input, it calls the enableEndsAt() function, which looks like this:
enableEndsAt(){
if(this.starts_at === "") {
return
}
var element = document.querySelector('#endsAt')
element.removeAttribute('disabled')
console.log(element)
}
You can use disabled as a reactive property:
<date-picker id="endsAt"
v-model="ends_at"
:disabled="disableDatePicker"
#input="checkDate()"
valueType="format"></date-picker>
So, to use this, you could create a property disableDatePicker in your data declaration, and then set it inside your method:
data(){ return { disableDatePicker: false }},
...
methods: { ....
enableEndsAt(){
if(this.starts_at === "") {
return
}
this.disableDatePicker = true;
}
Assuming your <date-picker> can accept disabled property, you can simply bind it to a condition:
<date-picker id="endsAt" v-model="ends_at" :disabled="this.starts_at === ''" #input="checkDate()" valueType="format"></date-picker>
so you don't need to put #input="enableEndsAt()" on your starts_at date picker, and you don't need enableEndsAt() function
Related
I need to disable a button if a boolean variable (returned from back-end) is true.
This is my incomplete function on my angular controller, how do I change my controller and HTML button to make this work?
$scope.flagInLavorazione = function (flagInLavorazione) {
if (flagInLavorazione === true){
// What to add here?
}
}
You're looking for ng-disabled. Simply assign it a property from your controller. When it's true, the element will be disabled. Populate that property with the value from the back end.
For AngularJS, you can use:
<button ng-disabled="isNotReady">I'm a button</button>
Edit: According to your requirements, you would use:
$scope.disabilitaConvalida = function (flagInLavorazione) {
$scope.saldoNegativo = false;
if (flagInLavorazione === false) {
$scope.saldoNegativo = true;
}
}
And in your template:
<button ng-disabled="saldoNegativo"></button>
Original Answer:
You set in your template:
<button ng-disabled="condition"></button>
And
$scope.condition = true or false in your controller based on the backend variable.
Basically, I have an EmberJS Application. In it, I want to show a different validation state and to do so, I want to update the html class attribute. Each of these form elements can have three different names: "" (empty), "has-success" or "has-error".
Basically, I want to bind the class name off a computed property which will return any of those three (much like the AngularJS ng-class) depending on the state of the form.
I want something like this:
validationState: function() {
if(element.state === "pristine") {
return "";
}
else if(element.state === "valid") {
return "has-success";
}
else{
return "has-error";
}
}
In the template, I would like something like that:
<input class="{{ validationState }} form-control">
Is this something feasible ? If yes, what is the best way to do it ? Creating a custom helper ? Or is there already a way to do so?
Use the bind-attr helper and make the validationState a computed property. This would look like this
{{input value=inputValue class=validationState}}
App.IndexController = Em.ArrayController.extend({
inputValue: 'test',
validationState: function() {
if(this.get('inputValue')) {
return 'valid';
}
return 'empty';
}.property('inputValue')
});
Here is a working example.
I'm using AngularJs on my project and i've a property on my viewModel that is connected to a dropdown (< select >)
that dropdown have a empty value witch is selected by default, what i want is to prevent user to select that empty value after he select some other value.
ive started to look to $watch, but i dont know if there is some way to cancel the "changing oof that property", some thing like this:
$scope.$watch('myProp', function (newVal, oldVal, scope) {
if (newVal) { scope.preventDefault(); }
}
any idea, this is the base idea, on a more advanced development i need to ask users for a confirmation.
any ideas?
what i want is to prevent user to select that empty value after he select some other value
This should happen automatically for you, as long as you don't assign the ng-model property a value initially. So using the <select> shown below, don't initialize $scope.selected_year in your controller:
<select ng-model="selected_year" ng-options="year for year in years"></select>
When the list displays initially, Angular will have added an option like this to the HTML, since $scope.selected_year is not currently set to a valid option/value:
<option value="?" selected="selected"></option>
After selecting a valid choice, that option will magically disappear, so the user will not be able to select it again. Try it in this fiddle.
If the ng-model property already has a valid value assigned when the select list is first displayed, then you can assign a controller function to the undocumented ng-change parameter:
<select ... ng-change="preventUserFromDoingXzy()">
Inside function preventUserFromDoingXzy() you can do what you need to do to control what the user can select, or modify the model.
You can just add ng-required to the select.
If there is no initial value to the model then an empty option will be added and on change to a valid value it will remove the empty option
EDITED jsFiddle to revert to previous value and to include the ng-change directive.
From the docs:
The expression is not evaluated when the value change is coming from the model.
This is useful in not interfering with change listeners and creating an infinite loop when reverting the old value in the $apply function
Controller
$scope.options = [{value: 'abc'},{value: 'def'}];
var confirmDialog = function(newVal, yes, no) {
// obviously not a good way to ask for the user to confirm
// replace this with a non blocking dialog
//the timeout is only for the confirm since it's blocking the angular $digest
setTimeout(function() {
c = confirm('Is it ok? [' + newVal.value + ']');
if(c) {
yes();
}
else {
no();
}
}, 0);
};
//Asking for confirmation example
function Ctrl($scope) {
$scope.options = [{value: 'abc'},{value: 'def'}];
$scope.select = undefined;
var oldSelect = undefined;
$scope.confirmChange = function(select) {
if(oldSelect) {
confirmDialog(select,
function() {
oldSelect = select;
},
function() {
$scope.$apply(function() {$scope.select = oldSelect;});
});
}
else {
oldSelect = select;
}
}
}
Template
<div ng-controller="Ctrl">
<select ng-model="select" ng-options="o.value for o in options"
ng-required ng-change="confirmChange(select)">
</select>
</div>
Probably the easiest, cleanest thing to do would be adding an initial option and setting disabled on it:
<option value="?" selected="selected" disabled></option>
Actually, it is easier to remove the empty value. Suppose you have a list of options:
$scope.options = [{value: ''}, {value: 'abc'},{value: 'def'}];
and a select:
<select ng-model="select" ng-options="o.value for o in options"></select>
Then $watch the model:
$scope.$watch('select', function(value) {
if (value && value !== '') {
if ($scope.options[0].value === '') {
$scope.options = $scope.options.slice(1);
}
}
}, true);
See it in action here.
PS Don't forget the objectEquality parameter in the $watch or it won't work!
I'm trying to assign a function to a couple of checkboxes, but I only want them added based on a condition, in this case the step number of the form. This is a roundabout way of making the checkboxes readOnly AFTER they have been selected (or not). So, at step 1 I want the user to choose cb1 or cb2, but at step 2 I want to assign the function that will not let the checkboxes values be changed.
What am I doing wrong?
function functionOne() {
this.checked = !this.checked
};
if (document.getElementById("stepNumber").value == 2) {
document.getElementById("cb1").setAttribute("onkeydown", "functionOne(this)");
document.getElementById("cb2").setAttribute("onkeydown", "functionOne(this)");
}
You are passing the element in an argument, so use that:
function functionOne(elem) {
elem.checked = !elem.checked
};
You could also use properties:
document.getElementById("cb1").onkeydown = functionOne;
document.getElementById("cb2").onkeydown = functionOne;
function functionOne() {
this.checked = !this.checked
};
This is a solution that requires jquery but you can use the .click function to disable checkboxes once one is clicked.
Here is a working example:
http://jsfiddle.net/uPsm7/
Why not on the selection disable the checkbox?
Function onCheck(elm)
{
document.getElementById("cbValue").value = elm.value;
elm.disabled = true;
}
<input id="cbValue" type="hidden" />
Use the hidden input field to allow form to send data back to server.
I have a form that has default values describing what should go into the field (replacing a label). When the user focuses a field this function is called:
function clear_input(element)
{
element.value = "";
element.onfocus = null;
}
The onfocus is set to null so that if the user puts something in the field and decides to change it, their input is not erased (so it is only erased once). Now, if the user moves on to the next field without entering any data, then the default value is restored with this function (called onblur):
function restore_default(element)
{
if(element.value == '')
{
element.value = element.name.substring(0, 1).toUpperCase()
+ element.name.substring(1, element.name.length);
}
}
It just so happened that the default values were the names of the elements so instead of adding an ID, I just manipulated the name property. The problem is that if they do skip over the element then the onfocus event is nullified with clear_input but then never restored.
I added
element.onfocus = "javascript:clear_input(this);";
In restore_default function but that doesn't work. How do I do this?
Use
element.onfocus = clear_input;
or (with parameters)
element.onfocus = function () {
clear_input( param, param2 );
};
with
function clear_input () {
this.value = "";
this.onfocus = null;
}
The "javascript:" bit is unnecessary.
It looks like you don't allow the fields to be empty, but what if the user puts a single or more spaces in the field? If you want to prevent this, you need to trim it. (See Steven Levithans blog for different ways to trim).
function trim(str) {
return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}
If you really want to capitalize the strings you could use:
function capitalize(str) {
return str.substr(0, 1).toUpperCase() + str.substr(1).toLowerCase();
}
By clearing the onfocus event you have created a problem that should not have been there. An easier solution is to just add an if-statement to the onfocus event, so it only clears if it is your default value (but I prefer to select it like tvanfosson suggested).
I assume that you on your input-elements have set the value-property so that a value is shown in the input-elements when the page is displayed even if javascript is disabled. That value is available as element.defaultValue. Bonuses by using this approach:
You only define the default value in one place.
You no longer need to capitalize any value in your handlers.
The default value can have any case (like "John Y McMain")
The default value no longer needs to be the same as the name of the element.
.
function clear_default(element) {
if (trim(element.value) == element.defaultValue ) { element.value = ""; }
}
function restore_default(element) {
if (!trim(element.value).length) { element.value = element.defaultValue;}
}
I would suggest that you handle it a little differently. Instead of clearing the value, why not just highlight it all so that the user can just start typing to overwrite it. Then you don't need to restore the default value (although you could still do so and in the same way if the value is empty). You also can leave the handler in place since the text is not cleared, just highlighted. Use validation to make sure the value is not the original value of the input.
function hightlight_input(element) {
element.select();
}
function restore_default(element) // optional, do we restore if the user deletes?
{
if(element.value == '')
{
element.value = element.name.substring(0, 1).toUpperCase()
+ element.name.substring(1, element.name.length);
}
}
<!-- JavaScript
function checkClear(A,B){if(arguments[2]){A=arguments[1];B=arguments[2]} if(A.value==B){A.value=""} else if(A.value==""){A.value="Search"}}
//-->
<form method="post" action="search.php">
<input type="submit" name="1">
<input type="text" name="srh" Value="Search" onfocus="checkClear(this,'Search')" onblur="checkClear(this,' ')">
</form>