malseladso
Privacy Advocate
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
200 XP
In this tutorial we will create a Dropdown Sort using AngularJS. This code can sort an table row data in the HTML table when select a value in the drop down. The code use angular directives to sort array data by adding orderBy in the ng-repeat and assigning the drop down ng-model as the orderBy value. This a user-friendly program feel free to modify and use it to your system.
We will be using AngularJS as a framework which has additional custom HTML attributes embedded into it. It can interprets those attributes as directives to bind inputted parts of the page to a model that represent as a standard JavaScript variables.
Getting started:
First you will need to download & install the AngularJS here's the link https://angularjs.org/.
And this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.
Creating the Main Interface
This code contains the interface of the application. This code will render application and display the form. To do that just kindly write these block of code inside the text editor and save it as index.html.
Creating the Script
This code contains the main function of the application. This code will sort table data when the drop down value is selected. To that just kindly copy and write these block of codes inside the text editor, then save it inside the js directory as script.js.
There you have it we successfully created a Dropdown Sort using AngularJS. I hope that this very simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!
Download
We will be using AngularJS as a framework which has additional custom HTML attributes embedded into it. It can interprets those attributes as directives to bind inputted parts of the page to a model that represent as a standard JavaScript variables.
Getting started:
First you will need to download & install the AngularJS here's the link https://angularjs.org/.
And this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.
Creating the Main Interface
This code contains the interface of the application. This code will render application and display the form. To do that just kindly write these block of code inside the text editor and save it as index.html.
- <!DOCTYPE html>
- <html
lang
=
"en"
ng-app=
"myModule"
>
- <head
>
- <meta
charsert=
"UTF-8"
name
=
"viewport"
content
=
"width=device-width, initial-scale=1"
/
>
- <link
rel
=
"stylesheet"
type
=
"text/css"
href
=
"css/bootstrap.css"
/
>
- </
head
>
- <body
ng-controller=
"myController"
>
- <nav class
=
"navbar navbar-default"
>
- <div
class
=
"container-fluid"
>
- <a
class
=
"navbar-brand"
href
=
"https://sourcecodester.com"
>
Sourcecodester</
a
>
- </
div
>
- </
nav>
- <div
class
=
"col-md-3"
></
div
>
- <div
class
=
"col-md-6 well"
>
- <h3
class
=
"text-primary"
>
JavaScript - Dropdown Sort Using AngularJS</
h3
>
- <hr
style
=
"border-top:1px dotted #ccc;"
/
>
- <div
class
=
"col-md-4"
>
- <form
>
- <div
class
=
"form-group"
>
- <label
>
Firstname</
label
>
- <input
type
=
"text"
class
=
"form-control"
ng-model=
"newMember.firstname"
/
>
- </
div
>
- <div
class
=
"form-group"
>
- <label
>
Lastname</
label
>
- <input
type
=
"text"
class
=
"form-control"
ng-model=
"newMember.lastname"
/
>
- </
div
>
- <div
class
=
"form-group"
>
- <label
>
Address</
label
>
- <input
type
=
"text"
class
=
"form-control"
ng-model=
"newMember.address"
/
>
- </
div
>
- <center
><button
type
=
"button"
class
=
"btn btn-primary"
ng-click =
"saveMember()"
><span
class
=
"glyphicon glyphicon-save"
></
span
>
Save</
button
></
center
>
- </
form
>
- </
div
>
- <div
class
=
"col-md-8"
>
- <div
class
=
"form-inline"
>
- <label
>
Order By:</
label
>
- <select
class
=
"form-control"
ng-model=
"order"
>
- <option
value
=
"firstname"
>
Firstname ASC</
option
>
- <option
value
=
"-firstname"
>
Firstname DESC</
option
>
- <option
value
=
"lastname"
>
Lastname ASC</
option
>
- <option
value
=
"-lastname"
>
Lastname DESC</
option
>
- <option
value
=
"address"
>
Address ASC</
option
>
- <option
value
=
"-address"
>
Address DESC</
option
>
- </
select
>
- </
div
>
- <br
/
>
- <table
class
=
"table table-bordered"
>
- <thead
class
=
"alert-info"
>
- <tr
>
- <th
>
Firstname</
th
>
- <th
>
Lastname</
th
>
- <th
>
Address</
th
>
- </
tr
>
- </
thead
>
- <tbody
>
- <tr
ng-repeat=
"member in members | orderBy:order"
>
- <td
>
{{member.firstname}}</
td
>
- <td
>
{{member.lastname}}</
td
>
- <td
>
{{member.address}}</
td
>
- </
tr
>
- </
tbody
>
- </
table
>
- </
div
>
- </
div
>
- <script
src
=
"js/angular.js"
></
script
>
- <script
src
=
"js/script.js"
></
script
>
- </
body
>
- </
html
>
- </
html
>
Creating the Script
This code contains the main function of the application. This code will sort table data when the drop down value is selected. To that just kindly copy and write these block of codes inside the text editor, then save it inside the js directory as script.js.
- var
app =
angular.module
(
"myModule"
,
[
]
)
- .controller
(
"myController"
,
function
(
$scope)
{
- $scope.members
=
[
- {
firstname:
"John"
,
lastname:
"Smith"
,
address:
"New York"
}
,
- {
firstname:
"Claire"
,
lastname:
"Temple"
,
address:
"Racoon City"
}
,
- ]
;
- $scope.newMember
=
{
}
;
- $scope.saveMember
=
function
(
)
{
- $scope.members
.push
(
$scope.newMember
)
;
- $scope.newMember
=
{
}
;
- }
;
- $scope.order
=
"firstname"
;
- }
)
;
There you have it we successfully created a Dropdown Sort using AngularJS. I hope that this very simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!
Download
You must upgrade your account or reply in the thread to view the hidden content.