PdrFrncsc
Signal Intelligence Expert
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 2
1000 XP
In this tutorial we will create a Sorting Array Table By Lastname using JavaScript. This code will automatically sort the array table when the user click a button. The code use onclick() function to initiate a method that sort an array by base on the click button then it will be undergoing in a conditional statement that organize the index position of an array by the use of sortArray(). You can apply this script to your code to make your transaction faster, it is a user-friendly program feel free to modify it.
We will use JavaScript to add some new feature to the website interface by actually written into an HTML page. It is what gives your page a different interactive elements and animation that engage a user.
Getting Started:
This is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.
The Main Interface
This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
Creating the Script
This code contains the script of the application. This code will sort the array when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory
There you have it we successfully created a Sorting Array Table By Lastname using JavaScript. I hope that this 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 use JavaScript to add some new feature to the website interface by actually written into an HTML page. It is what gives your page a different interactive elements and animation that engage a user.
Getting Started:
This is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.
The Main Interface
This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
- <!DOCTYPE html>
- <html
lang
=
"en"
>
- <head
>
- <meta
charset
=
"UTF-8"
name
=
"viewport"
content
=
"width=device-width, initial-scale=1"
/
>
- <link
rel
=
"stylesheet"
type
=
"text/css"
href
=
"css/bootstrap.css"
/
>
- </
head
>
- <body
>
- <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 - Sorting Array Table By Lastname</
h3
>
- <hr
style
=
"border-top:1px dotted #ccc;"
/
>
- <div
class
=
"col-md-2"
></
div
>
- <div
class
=
"col-md-8"
>
- <button
type
=
"button"
class
=
"btn btn-primary"
onclick
=
"sortAscending()"
>
Ascending</
button
>
<button
type
=
"button"
class
=
"btn btn-primary"
onclick
=
"sortDescending()"
>
Descending</
button
>
- <br
/
><br
/
>
- <table
class
=
"table table-bordered"
>
- <thead
class
=
"alert-info"
>
- <tr
>
- <td
>
Firstname</
td
>
- <td
>
Lastname </
td
>
- </
tr
>
- </
thead
>
- <tbody
id
=
"result"
style
=
"background-color:#fff;"
></
tbody
>
- </
table
>
- </
div
>
- </
div
>
- </
body
>
- <script
src
=
"js/script.js"
></
script
>
- </
html
>
Creating the Script
This code contains the script of the application. This code will sort the array when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory
- var
members =
[
- {
"firstname"
:
"John"
,
"lastname"
:
"Smith"
}
,
- {
"firstname"
:
"Claire"
,
"lastname"
:
"Temple"
}
,
- {
"firstname"
:
"Steve"
,
"lastname"
:
"Roger"
}
,
- {
"firstname"
:
"Paul"
,
"lastname"
:
"Miles"
}
,
- ]
;
- displayData(
)
;
- function
displayData(
)
{
- var
data =
""
;
- if
(
members.length
>
0
)
{
- for
(
var
i=
0
;
i <
members.length
;
i++
)
{
- data +=
"<tr>"
;
- data +=
"<td>"
+
members[
i]
.firstname
+
"</td>"
;
- data +=
"<td>"
+
members[
i]
.lastname
+
"</td>"
;
- data +=
"</tr>"
;
- }
- }
- document.getElementById
(
'result'
)
.innerHTML
=
data;
- }
- function
sortAscending(
)
{
- if
(
members.length
>
0
)
{
- var
sortArray =
members;
- sortArray.sort
(
function
(
a,
b)
{
- if
(
a.lastname
<
b.lastname
)
{
- return
-
1
;
- }
- if
(
a.lastname
>
b.lastname
)
{
- return
1
;
- }
- return
0
;
- }
)
;
- var
data =
""
;
- if
(
members.length
>
0
)
{
- for
(
var
i=
0
;
i <
members.length
;
i++
)
{
- data +=
"<tr>"
;
- data +=
"<td>"
+
members[
i]
.firstname
+
"</td>"
;
- data +=
"<td>"
+
members[
i]
.lastname
+
"</td>"
;
- data +=
"</tr>"
;
- }
- }
- document.getElementById
(
'result'
)
.innerHTML
=
data;
- }
- }
- function
sortDescending(
)
{
- if
(
members.length
>
0
)
{
- var
sortArray =
members;
- sortArray.sort
(
function
(
a,
b)
{
- if
(
a.lastname
>
b.lastname
)
{
- return
-
1
;
- }
- else
if
(
a.lastname
<
b.lastname
)
{
- return
1
;
- }
else
{
- return
0
;
- }
- }
)
;
- var
data =
""
;
- if
(
members.length
>
0
)
{
- for
(
var
i=
0
;
i <
members.length
;
i++
)
{
- data +=
"<tr>"
;
- data +=
"<td>"
+
members[
i]
.firstname
+
"</td>"
;
- data +=
"<td>"
+
members[
i]
.lastname
+
"</td>"
;
- data +=
"</tr>"
;
- }
- }
- document.getElementById
(
'result'
)
.innerHTML
=
data;
- }
- }
There you have it we successfully created a Sorting Array Table By Lastname using JavaScript. I hope that this 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.