How to send value from dropdown list? - angularjs

I have dropdown list :
<select name="timefilter" id="timefilter">
<option value="Last week">#Translate("LAST_WEEK")</option>
<option value="Last 3 days">#Translate("LAST_3_DAYS")</option>
<option value="Yesterday">#Translate("YESTERDAY")</option>
<option value="Today">Translate("TODAY")</option>
</select>
I need to send value from dropdown list to $scope
$scope.GetMenuForSelectedTime = function (numberOfDays) {
}
How can i do it?
EDIT: My function that i need to call:
$scope.SelectTicketByTime = function (range) {
var range = range;
var date = new Date();
var day = date.getUTCDate().padLeft();
var monthIndex = (date.getUTCMonth() + 1).padLeft();
var year = date.getUTCFullYear();
var hours = date.getUTCHours().padLeft();
var minutes = date.getUTCMinutes().padLeft();
$scope.GetMenuForSelectedTime = function (numberOfDays) {
var d = new Date();
d.toUTCString();
day = date.getUTCDate() - numberOfDays;
day = day.padLeft();
var offset = d.getTimezoneOffset();
d.setHours(00);
var hoursToAdd = offset / 60;
d.setHours(hoursToAdd);
if (day > daysInMonth(monthIndex, year)) {
var dayOffset = day - daysInMonth(monthIndex, year);
day = dayOffset.padLeft();
monthIndex = (date.getUTCMonth() + 2).padLeft();
}
var To = year + monthIndex + day + d.getHours() + '00';
$scope.to = To;
$http.get(TicketUrl + 'GetTickets/0/0?DateFrom=' + $scope.from + '&DateTo=' + $scope.to).
then(RecivedTickets, ErrorResponse);
}
if (range == "Today") {
$scope.GetMenuForSelectedTime(0);
}
else if (range == "Yesterday") {
$scope.GetMenuForSelectedTime(1);
}
else if (width == "Last 3 days") {
$scope.GetMenuForSelectedTime(3);
} else if (width == "Last 7 days") {
$scope.GetMenuForSelectedTime(7);
}

This is the example for putvande response (thanks to him):
<select name="timefilter" id="timefilter" ng-model="selectedTime">
<option value="Last week">#Translate("LAST_WEEK")</option>
<option value="Last 3 days">#Translate("LAST_3_DAYS")</option>
<option value="Yesterday">#Translate("YESTERDAY")</option>
<option value="Today">Translate("TODAY")</option>
</select>
And then you will have the value of your option in the controller :
$scope.selectedTime //The selected option
Edit : The function in your controller, will not need a value in param because the controller already know this value. You should have something like this.
$scope.SelectTicketByTime = function () {
var range = $scope.selectedTime;
var date = new Date();
var day = date.getUTCDate().padLeft();
var monthIndex = (date.getUTCMonth() + 1).padLeft();
var year = date.getUTCFullYear();
var hours = date.getUTCHours().padLeft();
var minutes = date.getUTCMinutes().padLeft();
}

I modify the code with ng-options and when you change any option in the dropdown, the ng-change will fire
<select name="ruleDetails" ng-model="selectedTime"
ng-options="time.id as time.value for time in timeDetails"
ng-change="GetMenuForSelectedTime(selectedTime)">
<option value="">-Select Time-</option>
</select>
$scope.GetMenuForSelectedTime = function(id) {
alert(id);
};
UPDATE:
This code also works, value will pass to the function.
<select name="timefilter" id="timefilter"
ng-model="selectedTime"
ng-change="GetMenuForSelectedTime(selectedTime)">
<option value="Last week">#Translate("LAST_WEEK")</option>
<option value="Last 3 days">#Translate("LAST_3_DAYS")</option>
<option value="Yesterday">#Translate("YESTERDAY")</option>
<option value="Today">Translate("TODAY")</option>
</select>
Please check this plunker

Related

how to get Selected Value from Dropdownlist on update record in Angularjs?

I filled my dropdownlist by angular js from json. But selection is not working on update i can not get selected value . How can i select dropdownlist selected item?
//get single record by ID
$scope.getForUpdate = function (Branch) {
debugger
var getData = myService.getBrancheTypes();
getData.then(function (response) {
debugger
$scope.BrancheTypes = response.data;
}, function (error) {
console.log(error);
toastr.error("Error in getting records.");
});
debugger
$scope.Branch_ID = Branch.Branch_ID;
$scope.Branch_Name = Branch.Branch_Name;
$scope.Branch_Address = Branch.Branch_Address;
$scope.Branch_email = Branch.Branch_email;
$scope.Branch_Notes = Branch.Branch_Notes;
$scope.Branch_TimeFrom = new Date(moment(Branch.Branch_TimeFrom));
$scope.Branch_TimeTo = new Date(moment(Branch.Branch_TimeTo));
$scope.Branch_CreatedDate = new Date(moment(Branch.Branch_CreatedDate));
$scope.Branch_Manager = Branch.Branch_Manager;
$scope.Branch_TypeId = Branch.Branch_TypeId; // Branch.Branch_TypeId = 1
$scope.Branch_Type = $scope.BrancheTypes[Branch.Branch_Type]; // Object {BranchTypeId: 1, BranchTypeName: "Head Office", Branches: null}
$scope.Branch_Phone = Branch.Branch_Phone;
$scope.Branch_Fax = Branch.Branch_Fax;
$scope.saturday = Branch.saturday;
$scope.sunday = Branch.sunday;
$scope.monday = Branch.monday;
$scope.tuesday = Branch.tuesday;
$scope.wednesday = Branch.wednesday;
$scope.thursday = Branch.thursday;
$scope.friday = Branch.friday;
};
<select ng-model="Branch_Type">
<option value="{{field.BranchTypeId}}" ng-selected="true"
ng-repeat="field in BrancheTypes | orderBy:'BranchTypeId'">
{{field.BranchTypeName}}
</option>
</select>
this is the values on debug
enter image description here
Try changing your html to this
<select ng-model="selectedDevice"
ng-options="field.BranchTypeId as field.BranchTypeName for field in BrancheTypes | orderBy:'BranchTypeId'">
<option></option>
</select>

Year value is not showing in angularjs

I'm trying to show the value of year in the view that is HTML, but year values are not showing at all. Although, My other value is showing while the condition is not acivated (like credit card), but when I activate it, year values is not showing, while values are coming from the controller and is shown in the console.
Here is the view code :-
<div class="col-xs-2 innerN" style="width: 14%;">
<select ng-disabled="page.creditcard.isDisabled" class="form-control innerN" placeholder="YYYY" name="ccExpYear" ng-model="page.expyear" required style="border-left:none">
<option value="XX" selected ng-if="page.creditcard.isDisabled">XX</option>
<option ng-repeat="year in page.yearList" value="{{year}}" ng-if="!page.creditcard.isDisabled">{{year}}</option>
</select>
</div>
And Here is the controller code :-
$scope.page.monthList = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", ];
$scope.page.yearList = [];
var d = new Date();
var year = d.getFullYear();
//$scope.page.yearList.push(year);
$scope.page.expyear = year;
console.log("Pawan2", $scope.page.expyear);
for (var i = 0; i < 21; i++) {
{
$scope.page.yearList.push(year + i);
};
}
$scope.years = $scope.page.yearList;
console.log("YearListPawan", $scope.page.yearList);
$scope.page.expmonth = "01";
$scope.monthList = "01";
// watcher
$scope.$watchGroup(['page.expmonth', 'page.expyear'], function(newValues, oldValues, scope) {
//MM-YYYY
if (typeof newValues[1] == "undefined" || typeof newValues[0] == "undefined") scope.authorize.creditCard
.expirationDate = null;
else $scope.authorize.creditCard.expirationDate = newValues[1] + "-" + newValues[0];
console.log("Pawan", $scope.authorize.creditCard.expirationDate)
}, true);
I hope, I'm clear anough to get the answer for this.
PS : THnx in advance for help.
You should use ng-value instead of value:-
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.page = {};
$scope.page.monthList = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", ];
$scope.page.yearList = [];
var d = new Date();
var year = d.getFullYear();
//$scope.page.yearList.push(year);
$scope.page.expyear = year;
console.log("Pawan2", $scope.page.expyear);
for (var i = 0; i < 21; i++) {
{
$scope.page.yearList.push(year + i);
};
}
$scope.years = $scope.page.yearList;
$scope.authorize = {creditCard: {expirationDate: 'date'}};
console.log("YearListPawan", $scope.page.yearList);
$scope.page.expmonth = "01";
$scope.monthList = "01";
// watcher
$scope.$watchGroup(['page.expmonth', 'page.expyear'], function(newValues, oldValues, scope) {
//MM-YYYY
if (typeof newValues[1] == "undefined" || typeof newValues[0] == "undefined")
$scope.authorize.creditCard.expirationDate = null;
else
$scope.authorize.creditCard.expirationDate = newValues[1] + "-" + newValues[0];
console.log("Pawan", $scope.authorize.creditCard.expirationDate)
}, true);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<div class="col-xs-2 innerN" style="width: 14%;">
<select ng-disabled="page.creditcard.isDisabled" class="form-control innerN" placeholder="YYYY" name="ccExpYear" ng-model="page.expyear" required style="border-left:none">
<option value="XX" selected ng-if="page.creditcard.isDisabled">XX</option>
<option ng-repeat="year in page.yearList" ng-value="year" ng-if="!page.creditcard.isDisabled">{{year}}</option>
</select>
</div>
</div>
Here is the solution that I have applied to the scenario -
And here is the controller code with few changes : -
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.page = {};
$scope.page.monthList = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", ];
$scope.page.yearList = [];
var d = new Date();
var year = d.getFullYear();
var addYears = function () {
var d = new Date();
var year = d.getFullYear();
$scope.page.yearList = [];
var i = 0;
for (i = 0; i < 40; i++) {
$scope.page.yearList.push(year.toString());
year++;
}
}
addYears();
$scope.years = $scope.page.yearList;
$scope.page.expyear = d.getFullYear();
$scope.page.expmonth = "01";
$scope.monthList = "01";
// watcher
$scope.$watchGroup(['page.expmonth', 'page.expyear'], function(newValues, oldValues, scope) {
//MM-YYYY
if(typeof newValues[1] == "undefined" || typeof newValues[0] == "undefined")
scope.authorize.creditCard.expirationDate = null;
else
$scope.authorize.creditCard.expirationDate = newValues[1] + "-" + newValues[0];
console.log("Pawan", $scope.authorize.creditCard.expirationDate)
}, true);
Here is the view code :-
<!DOCTYPE html>
<html><scriptsrc="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>
<div class="col-xs-2 innerN" style="width: 14%;">
<select ng-disabled="page.creditcard.isDisabled" class="form-control innerN" placeholder="YYYY" name="ccExpYear" ng-model="page.expyear" required style="border-left:none">
<option value="XX" selected ng-if="page.creditcard.isDisabled">XX</option>
<option ng-repeat="year in page.yearList" value="{{year}}" ng-if="!page.creditcard.isDisabled">{{year}}</option>
</select>
</div>
Hope, this will help to others.

Displaying dropdown in React Antd Table

const bands = this.props.ApplicationContainer.bands;
if(bands) {
var bandsHtml = '<Select defaultValue="Select"><Option value="Select">Select</Option>';
const listItems = bands.map(function(obj) {
const rObj = {};
rObj[obj.id] = obj.band;
bandsHtml += '<Option value="'+obj.band+'">'+obj.band+'</Option>';
return rObj;
});
bandsHtml += '</Select>';
} else {
var bandsHtml='';
}
console.log(bandsHtml);
return (
bandsHtml
);
The function returns what goes into the Table, but please check out this image:
https://ibb.co/jNBYnk
It displays as HTML in the cell. Any ideas on how it could be rendered as a Select in the Antd Table?
let bandsHtml = null
if(bands) {
bandsHtml =
<Select defaultValue="Select">
<Option value="Select">Select</Option>
{bands.map( (obj,index) => (<Option value={obj.band} key={index}>{obj.band}</Option>)}
</Select>
}
/de SM0XLK ;-)

How to modify This angularjs function to retrieve range between two dates

How can I modify this angular function to retrieve range of dates. For now its looping 20 times. How can I only retrieve date from Date().getFullYear(); --> Which is current year till 2020. So in range it should display 2017, 2018, 2019, 2020.
$scope.myfunction = function () {
var year = new Date().getFullYear();
var range = [];
range.push(year);
for (var i = 1; i < 20; i++) {
range.push(year + i);
}
return range;
}
<select ng-options="year for year in myfunction()" name="year" ng-model="year" required>
<option value selected disabled>select year</option>
</select>
You can do something like this. You can set the end year or pass to the function as an argument.
$scope.myfunction = function (endYear) {
var range = [];
var startYear = new Date().getFullYear();
endYear = endYear || 2020;
while ( startYear <= endYear ) {
range.push(startYear++);
}
return range;
}

select(name).option(value) selects wrong option?

I've a got a strange issue. When I try to select a date the select() function selects the wrong option.
Ng-Model:
days = ['01', '02', '03', '04', ..., '31'];
Markup:
<select ng-model="day" id="day" name="day" ng-options="day for day in days">
<option value="" disabled="disabled"></option>
</select>
e2e test:
it('should select correct date', function () {
select('day').option('30');
expect(element('#day option:selected').text()).toEqual('30');
});
So my question is: Why does select('day').option('30') select the 31th day when select('day').option('02') works as expected?
So I was wondering what value it's targeting as it's working fine in other places.
The documentation is very sparse so either it's a feature or a bug :)
What I think is going on is the select() tries to select the value in some sort of order. I.E. it first tries to select the option by value then it tries to select it by model value, or something along those lines.
So I looked up the implementation of the select function.
From github:
/**
* Usage:
* select(name).option('value') select one option
* select(name).options('value1', 'value2', ...) select options from a multi select
*/
angular.scenario.dsl('select', function() {
var chain = {};
chain.option = function(value) {
return this.addFutureAction("select '" + this.name + "' option '" + value + "'",
function($window, $document, done) {
var select = $document.elements('select[ng\\:model="$1"]', this.name);
var option = select.find('option[value="' + value + '"]');
if (option.length) {
select.val(value);
} else {
option = select.find('option').filter(function(){
return _jQuery(this).text() === value;
});
if (!option.length) {
option = select.find('option:contains("' + value + '")');
}
if (option.length) {
select.val(option.val());
} else {
return done("option '" + value + "' not found");
}
}
select.trigger('change');
done();
});
};
return function(name) {
this.name = name;
return chain;
};
});
So the "problem" is that select tries to select from the values in the DOM element, that is the <option value="THIS VALUE"> then it tries to find the value by what's being displayed <option>THIS VALUE</option> and then it tries to do a contains on the value. It doesn't actually use the model value at any point.
So select('day').option('02') worked because it was matching the displayed text, where as select('day').option('30') was matching the option value which has an offset.
Keep in mind the generated HTML is:
<select ng-model="day" id="day" name="day" ng-options="day for day in days">
<option value="" disabled="disabled"></option>
<option value="0">01</option> <-- note it starts at 0 not 1
<option value="1">02</option> <-- select('day').option('02') matches display text '02' as no value 02 exists.
<option value="2">03</option>
<option value="29">30</option>
<option value="30">31</option> <-- select('day').option('30') matches value 30 before display text 30 with value 29.
</select>
To "solve" this issue a new function needs to be created (or alter the existing).
angular.scenario.dsl('selectModel', function() {
var chain = {};
chain.option = function(value) {
return this.addFutureAction("select '" + this.name + "' option '" + value + "'",
function($window, $document, done) {
var $ = $window.$; // jQuery inside the iframe
var select = $document.elements('select[ng\\:model="$1"]', this.name);
var option = select.find('option').filter(function(){
return $(this).text() === value;
});
if (!option.length) {
option = select.find('option:contains("' + value + '")');
}
if (option.length) {
select.val(option.val());
} else {
return done("option '" + value + "' not found");
}
select.trigger('change');
done();
});
};
return function(name) {
this.name = name;
return chain;
};
});
This does the trick.
The Angular created option tags have indices as value, when the source model is a list:
$scope.listItems = [
"day 1",
"day 2",
"day 3",
"day 4",
"day 5"
];
and
<select ng-model="listItem" ng-options="item for item in listItems"></select>
create the following HTML:
<select ng-options="item for item in listItems" ng-model="listItem"
class="ng-pristine ng-valid">
<option value="?" selected="selected"></option>
<option value="0">day 1</option>
<option value="1">day 2</option>
<option value="2">day 3</option>
<option value="3">day 4</option>
<option value="4">day 5</option>
</select>
While the option tags have keys as value, when the source model is a map:
$scope.objItems = {
"day 1":"1",
"day 2":"2",
"day 3":"3",
"day 4":"4",
"day 5":"5"
};
and
<select ng-model="objItem2" ng-options="value as key for (key, value)
in objItems"></select>
create:
<select ng-options="value as key for (key, value) in objItems"
ng-model="objItem2" class="ng-valid ng-dirty">
<option value="day 1" selected="selected">day 1</option>
<option value="day 2">day 2</option>
<option value="day 3">day 3</option>
<option value="day 4">day 4</option>
<option value="day 5">day 5</option>
</select>
However this never poses a problem because when the user selects an option, Angular looks up at the indexed position inside the source and assigns that value to the model.
I am not quite familiar with e2e testing but I think the reason you are getting a different value everytime you select an option is that you are trying to access the value stored in the option tag and not what Angular has stored in the model for you.
Fiddle

Resources