OBJECTIVE: Learn how to create an Employee Directory App and then add Local Storage so that the data will persist once the app is closed.
Resources:
NOTE: You don't have to understand all of the concepts to complete this tutorial. You can complete it by:
In the tutorial, we will create a mock-up of an Employee Directory using the jQuery Mobile Framework. If you look at the finished app WITHOUT jQuery enabled, you will see something like this:

However, once we inject some of the jQuery Mobile Goodness, the SAME app looks like this:
Before we get started creating the Employee Directory app, we need to implement a jQuery framework. There are two ways to do this:
For simplicity sake, we will use a remote version. However, you could implement a local version if you wish.

<!doctype html> <html lang="en"> <head> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
title>Employee Directory</title> </head> <body> </body> </html>
<title>Employee Directory</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
Now that we have the jQuery framework implemented, let’s create a jQuery page to use for our app.


<div data-role="page" id="home_page" data-theme="a">
<div data-role="header" data-position="fixed">
<h1>EMPLOYEE DIRECTORY</h1>
</div>
<div data-role="content">Content</div>
<div data-role="footer" data-position="fixed">
<h4>Copyrighted 2016 by RMCS. All rights reserved.</h4>
</div>
</div>

<div data-role="content">
</div>

<div data-role="content">
<ul data-role="listview" data-inset="true">
<li>
<a href="#"><h3>Page</h3><p>Lorem ipsum</p></a>
<a href="#">Default</a>
</li>
</ul>
</div>

<ul data-role="listview" data-inset="true">
<li>
<a href="#"><img src="images/ann_ricoh.jpg" width="307" height="240" alt=""/><h3>Page</h3><p>Lorem ipsum</p></a> <a href="#">Default</a> </li>
</ul>

<ul id="myList" data-split-icon="edit" data-split-theme="b" data-theme="b" data-role="listview" data-inset="true">
<li>
<a href="#"><img src="images/ann_ricoh.jpg" width="307" height="240" alt=""/><h3>Page</h3><p>Lorem ipsum</p></a>
<a href="#">Default</a>
</li>
</ul>
Now that we have implemented the jQueryMobile framework AND create a jQuery page, it’s time to implement the data we need for our Employee Directory app. We will do this by creating an array of [employee] objects.
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<script>
</script>
<script> // CREATE AN ARRAY OF EMPLOYEE OBJECTS -------------------------------------
var Employees = [
{firstName:"Ann", lastName:"Ricoh", department:"Marketing", city:"Austin", state:"Texas", startDate:"1/12/16", image:"ann_ricoh.jpg"},
{firstName:"Bob", lastName:"Anderson", department:"Human Resource", city:"Round Rock", state:"Texas", startDate:"2/13/16", image:"bob_anderson.jpg"},
{firstName:"Carol", lastName:"Green", department:"Security", city:"Pflugerville", state:"Texas", startDate:"3/14/16", image:"carol_green.jpg"},
{firstName:"Debra", lastName:"Samson", department:"IT", city:"Cedar Park", state:"Texas", startDate:"4/15/16", image:"debra_samson.jpg"}
];
alert(Employees);
</script>
alert("First name of first employee: " + Employees[0].firstName);
// alert("First name of first employee: " + Employees[0].firstName); var numberOfEmployees = Employees.length;
alert("Total Number of Employees is: " + numberOfEmployees);
Now that we have created the data for our Employee Directory app and tested it to see if it is formatted and working correctly, let’s create a jQuery wrapper and then LOOP AROUND the array that we created earlier to generate a ListView to display the employees.
// alert("Total Number of Employees is: " + numberOfEmployees);
$(document).ready(function(e) {
}); // END OF DOCUMENT.READY() FUNCTIONALITY
</script>
$(document).ready(function(e) {
// CREATE A GENERIC FUNCTION THAT WILL CREATE AND REFRESH THE LISVIEW -----------------
for(i = 0; i < numberOfEmployees; i++)
{
alert(Employees[i].firstName + " " + Employees[i].lastName + ": " + Employees[i].department);
}
}); // END OF DOCUMENT.READY() FUNCTIONALITY
</script>
$(document).ready(function(e) {
// CREATE A GENERIC FUNCTION THAT WILL CREATE AND REFRESH THE LISVIEW -----------------
createListView();
function createListView()
{
for(i=0;i<numberOfEmployees;i++)
{
alert(Employees[i].firstName + " " + Employees[i].lastName + ": " + Employees[i].department);
}
}
// alert(Employees[i].firstName + " " + Employees[i].lastName + ": " + Employees[i].department);
var fullName = Employees[i].firstName + " " + Employees[i].lastName;
var imageDirAndName = "images/" + Employees[i].image;
var department = Employees[i].department;
alert (imageDirAndName);
var fullName = Employees[i].firstName + " " + Employees[i].lastName;
var imageDirAndName = "images/" + Employees[i].image;
var department = Employees[i].department;
// alert (imageDirAndName);
$("#myList").append("");



$("#myList").append("<li><a href='#'><img src='images/ann_ricoh.jpg' width='80' height='80' alt=''/><h3>Page</h3><p>Lorem ipsum</p></a><a href='#'>Default</a></li>");
}
// Once a ListView is manipulated, it has to be REFRESHED to see the updated appearance.
$("#myList").listview("refresh");

$("#myList").append("<li><a href='#employee_page'><img src=" + imageDirAndName
+" width='307' height='240' alt=''/><h3 style='color:orange'>" + fullName + "</h3><p>"
+ department + "</p></a><a id='edit' href='#'>Edit " + fullName + "</a></li>");

var customListView = "<li><a href='#employee_page'>"
customListView += "<img src=" + imageDirAndName + " width='80' height='80' alt=''/>"
customListView += "<h3 style='color:orange'>" + fullName + "</h3>"
customListView += "<p>" + department + "</p></a>"
customListView += "<a id='edit' href='#'>Edit " + fullName + "</a></li>"
$("#myList").append(customListView);
// $("#myList").append("<li><a href='#employee_page'><img src="
+ imageDirAndName + " width='307' height='240' alt=''/><h3 style='color:orange'>"
+ fullName + "</h3><p>" + department + "</p></a><a id='edit' href='#'>Edit " + fullName + "</a></li>");
Now that we have a dynamic ListView to display, let’s add some simple code to SEARCH the ListView for the employee we want to view.
<ul id="myList" data-filter="true" data-filter-placeholder="Search for Employee" data-filter-theme="a"
data-role="listview" data-inset="true" data-split-icon="edit" data-split-theme="b" data-theme="b" data-divider-theme="a">
</ul>

Now that we have created a Master Page with a Search function, it’s time to turn our attention on creating the Detail Page that will provide some additional DETAIL information when a selection is made.

</div>
<div data-role="page" id="employee_page">
<div data-role="header">
<a data-icon="home" data-iconpos="left" href="#home_page">HOME</a>
<h1>EMPLOYEE DETAIL</h1>
</div>
<div id="container" data-role="content">
<p>Dynamic content will go here...</p>
</div>
<div style="margin-left:20px;">
<button id="delete_employee_btn" data-icon="delete" data-inline="true" data-mini="true" data-theme="b">DELETE EMPLOYEE</button>
<a id="cancel_delete_employee_btn" href="#home_page" data-role="button" data-icon="back" data-inline="true" data-mini="true" data-theme="b">RETURN</a>
</div>
<div data-role="footer" data-position="fixed">
<h4>Copyright 2016 by RMCS. All rights reserved.</h4>
</div>
</div>
</body>
</html>
$("#myList").listview("refresh");
// VIEW SELECTED INDEX FROM LISTVIEW -------------------------------------------
$(document).on("click", "#myList li a", function() {
selectedIndex = $(this).closest("li").index();
alert("The selected index when the view button is clicked: " + selectedIndex);
});
}); // END OF DOCUMENT.READY() FUNCTIONALITY
// VIEW SELECTED INDEX FROM LISTVIEW -------------------------------------------
$(document).on("click", "#myList li a", function() {
selectedIndex = $(this).closest("li").index();
// alert("The selected index when the view button is clicked: " + selectedIndex);
var currentImage = "images/" + Employees[selectedIndex].image;
alert(currentImage);
}); // END OF DOCUMENT.READY() FUNCTIONALITY
var currentImage = "images/" + Employees[selectedIndex].image;
var fullName = Employees[selectedIndex].firstName + " " + Employees[selectedIndex].lastName;
var city = Employees[selectedIndex].city;
var state = Employees[selectedIndex].state;
var startDate = Employees[selectedIndex].startDate;
var department = Employees[selectedIndex].department;
// alert(currentImage);
// alert(currentImage);
// The method replaceWith() did not work so we had to add the empty() and append() methods
$("#employee_page div#container").empty();
$("#employee_page div#container").append("<img style='border:1px solid black' src= '" + currentImage + "' width='307' height='240' alt=''/>");
$("#employee_page div#container").append("<h1 style = 'color:red'>" + fullName + "</h1>");
$("#employee_page div#container").append("<p><strong style = 'color:red'>City:</strong> " + city + "</p>");
$("#employee_page div#container").append("<p><strong style = 'color:red'>State:</strong> " + state + "</p>");
$("#employee_page div#container").append("<p><strong style = 'color:red'>Start Date:</strong> " + startDate + "</p>");
$("#employee_page div#container").append("<p><strong style = 'color:red'>Department:</strong> " + department + "</p>");
});
});// END OF VIEW SELECTED INDEX FROM LISTVIEW FUNCTIONALITY

Now that we have created a Master and Detail page that we can view. Let’s add the functionality to add more employees to the Employee Directory. We will do this by first creating the STATIC content as usual and then writing the code to save the user input into the array and display it in the Master and Detail page.
<div data-role=”content”> <a id="show_add_employee_form" href="#" data-role="button" data-icon="user" data-iconpos="left" data-mini="true" data-inline="true" data-theme="b">SHOW ADD EMPLOYEE FORM</a> <form id="add_employee_form" method="post" action="">
<div class="ui-body ui-body-a ui-corner-all"> <label for="firstNameID">First Name:</label>
<input id="firstNameID" value="" type="text" data-mini="true">
<label for="lastNameID">Last Name:</label>
<input id="lastNameID" value="" type="text" data-mini="true">
<label for="cityID">City:</label>
<input id="cityID" value="Austin" type="text" data-mini="true">
<label for="stateID">State:</label>
<input id="stateID" value="Texas" type="text" data-mini="true">
<label for="startDateID">Start Date:</label>
<input id="startDateID" value="" type="date" data-mini="true">
<label for="departmentID">Department:</label>
<select id="departmentID" data-mini="true">
<option value="Marketing">Marketing</option>
<option value="Engineering">Engineering</option>
<option value="IT">IT</option>
<option value="Human Resources">Human Resources</option>
<option value="Sales">Sales</option>
<option value="Customer Services">Customer Services</option> <option value="Admin">Admin</option>
</select>
<label id="imageID_Label" for="imageID">Employee Image (If available):</label>
<input id="imageID" type="file" value="" data-mini="true"/> </div>
</form> <a id="add_employee_btn" data-role="button" data-icon="plus" data-iconpos="left" data-inline="true" data-theme="b" data-mini="true">ADD EMPLOYEE</a>
<a id="edit_employee_btn" data-role="button" data-icon="action" data-iconpos="left" data-inline="true" data-theme="b" data-mini="true">SAVE UPDATES</a>
<a id="cancel_add_employee_btn" data-role="button" data-inline="true" data-theme="b" data-mini="true" data-icon="back" data-iconpos="left">RETURN</a> <ul id="myList" data-filter="true" data-filter-placeholder="Search for Employee" data-filter-theme="a" data-inset="true" data-role="listview" data-split-icon="edit" data-split-theme="b" data-theme="b">
<div data-role="content" data-theme="a">

// alert(Employees[0].firstName);
// HIDE ADD EMPLOYEE FORM, BUTTONS, ETC. --------------------------------------
$("#add_employee_form").hide();
$("#add_employee_btn").hide();
$("#cancel_add_employee_btn").hide();
$("#edit_employee_btn").hide();
}); // END OF DOCUMENT.READY() FUNCTIONALITY

// HIDE ADD EMPLOYEE FORM, BUTTONS, ETC. --------------------------------------
$("#add_employee_form").hide();
$("#add_employee_btn").hide();
$("#cancel_add_employee_btn").hide();
$("#edit_employee_btn").hide();
// ANIMATE ADD EMPLOYEE FORM WHEN CLICKED --------------------------------------
$("#show_add_employee_form").click(function()
{
$(this).hide();
$("#add_employee_form").slideDown(500);
$("#add_employee_btn").show();
$("#cancel_add_employee_btn").show();
});
}); // END OF DOCUMENT.READY() FUNCTIONALITY

// ANIMATE ADD EMPLOYEE FORM WHEN CLICKED --------------------------------------
$("#show_add_employee_form").click(function()
{
$(this).hide();
$("#add_employee_form").slideDown(500);
$("#add_employee_btn").show();
$("#cancel_add_employee_btn").show();
});
// CANCEL ADD EMPLOYEE BUTTON FUNCTIONALITY ------------------------------------
$("#cancel_add_employee_btn").click(function(e){
closeForm();
});
function closeForm()
{
$("#add_employee_form").slideUp(500);
$("#show_add_employee_form").show();
$("#firstNameID").val("");
$("#lastNameID").val("");
$("#startDateID").val("");
$("#imageID").val("");
$("#add_employee_btn").hide();
$("#cancel_add_employee_btn").hide();
$("#edit_employee_btn").hide();
}
// ADD EMPLOYEE BUTTON FUNCTIONALITY -----------------------------------------
$("#add_employee_btn").click(function(e){
numberOfEmployees++
var firstNameField = $("#firstNameID").val();
var lastNameField = $("#lastNameID").val();
var cityField = $("#cityID").val();
var stateField = $("#stateID").val();
var startDateField = $("#startDateID").val();
var departmentField = $("#departmentID").val();
var imageField = $("#imageID").val();
Employees.push({firstName:firstNameField, lastName:lastNameField, city:cityField, state:stateField, startDate:startDateField, department:departmentField, image:imageField});alert("What is first name? " + Employees[0].firstName)
alert("Number of Employees: " + Employees.length);
})
// alert("What is first name? " + Employees[0].firstName);
// alert("Number of Employees: " + Employees.length);
$("#myList").empty(); // Clear ListView
createListView(); // Recreate ListView
closeForm(); // Close Form });
for(i=0;i<numberOfEmployees;i++)
{
// alert(Employees[i].firstName + " " + Employees[i].lastName + ": " + Employees[i].department);
var fullName = Employees[i].firstName + " " + Employees[i].lastName;
var imageDirAndName = "images/" + Employees[i].image;
// alert (imageDirAndName);
var department = Employees[i].department;
// If NO image is selected than provide default avatar image
if (imageDirAndName == "images/")
{
imageDirAndName = "images/photo_not_available_large.png";
}
// alert (imageDirAndName);
$("#myList").append("<li><a href='#employee_page'><img src=" + imageDirAndName + " width='80' height='80' alt=''/><h3 style='color:orange'>" + fullName + "</h3><p>" + department + "</p></a><a id='edit' href='#'>Edit " + fullName + "</a></li>");
}
var currentImage = "images/" + Employees[selectedIndex-1].image;
var fullName = Employees[selectedIndex-1].firstName + " " + Employees[selectedIndex-1].lastName;
var city = Employees[selectedIndex-1].city;
var state = Employees[selectedIndex-1].state;
var startDate = Employees[selectedIndex-1].startDate;
var department = Employees[selectedIndex-1].department;
// alert(currentImage);
// If NO image is selected than provide default avatar image
if (currentImage == "images/")
{
currentImage = "images/photo_not_available_large.png";
}
// The method replaceWith() did not work so I add the empty() and append() methods
$("#employee_page div#container").empty();

Now that we have the ability to ADD employees to the ListView, let’s see how to UPDATE existing information for a given employee. Since the major difference between ADDING and UPDATING an employee using a form is that:
// OPEN AND PREPOPULATE ADD EMPLOYEE FORM FOR EDITING --------------------------------------- $(document).on("click", "#myList li a#edit", function() { $("#add_employee_form").slideDown();
$("#show_add_employee_form").hide();
$("#cancel_add_employee_btn").show(); $("#edit_employee_btn").show(); $("#firstNameID").val(Employees[selectedIndex].firstName);
$("#lastNameID").val(Employees[selectedIndex].lastName);
$("#cityID").val(Employees[selectedIndex].city);
$("#stateID").val(Employees[selectedIndex].state);
$("#startDateID").val(Employees[selectedIndex].startDate); $("#departmentID").val(Employees[selectedIndex].department);
$("#imageID").val(Employees[selectedIndex].image);
});
}); // END OF DOCUMENT.READY() FUNCTIONALITY
// SAVE UPDATES BUTTON FUNCTIONALITY ------------------------- $("#edit_employee_btn").click(function(e){ Employees[selectedIndex].firstName = $("#firstNameID").val();
Employees[selectedIndex].lastName = $("#lastNameID").val();
Employees[selectedIndex].city = $("#cityID").val();
Employees[selectedIndex].state = $("#stateID").val();
Employees[selectedIndex].startDate = $("#startDateID").val();
Employees[selectedIndex].department = $("#departmentID").val();
Employees[selectedIndex].image = $("#imageID").val(); $("#myList").empty(); // Clear ListView
createListView(); // Recreate ListView
closeForm(); // Close Form
}); }); // END OF DOCUMENT.READY() FUNCTIONALITY
// CREATE AN ARRAY OF DEPARTMENTS ------------------------------------------
var Departments = ["Marketing", "Engineering", "Human Resource", "Sales", "Customer Services", "Admin", "IT", "Security"];
var totalDepartments = Departments.length;
alert("Total Number of Departments: " + totalDepartments);
// CREATE AN ARRAY OF EMPLOYEE OBJECTS -------------------------------------
<select id="departmentID" data-mini="true"> <option value="Marketing">Marketing</option> <option value="Engineering">Engineering</option> <option value="IT">IT</option> <option value="Human Resources">Human Resources</option> <option value="Sales">Sales</option> <option value="Customer Services">Customer Services</option> <option value="Admin">Admin</option> </select>
<select id="departmentID" data-mini="true">
<script>
for(var i=0; i<totalDepartments; i++)
{
document.write("<option>" + Departments[i] + "</option>");
}
</script>
</select>
<label id="imageID_Label" for="imageID">Employee Image (If available):</label>
<input id="imageID" type="file" value="" data-mini="true"/>
<label id="imageID2_Label" for="imageID2">Employee Image (If new):</label>
<input id="imageID2" type="text" value="" data-mini="true"/>
// ANIMATE ADD EMPLOYEE FORM WHEN CLICKED --------------------------------------
$("#show_add_employee_form").click(function()
{
$(this).hide();
$("#imageID").show();
$("#imageID_Label").show();
$("#imageID2").hide();
$("#imageID2_Label").hide();
$("#add_employee_form").slideDown(500);
$("#add_employee_btn").show();
$("#cancel_add_employee_btn").show();
});
var departmentField = $("#departmentID").val();
var imageField = $("#imageID2").val();
$("#departmentID").val(Employees[selectedIndex].department);
$("#imageID2").val(Employees[selectedIndex].image);
// VIEW SELECTED INDEX FROM LISTVIEW -------------------------------------------
$(document).on("click", "#myList li a", function() {
selectedIndex = $(this).closest("li").index();
// alert("The selected index when the view button is clicked: " + selectedIndex);
$("#imageID").hide();
$("#imageID_Label").hide();
$("#imageID2").show();
$("#imageID2_Label").show();
var currentImage = "images/" + Employees[selectedIndex].image;


$("#imageID2").val(Employees[selectedIndex].image);
$("#departmentID").empty(); // Empty combo box first and then refill it with loop
for(var i=0; i<totalDepartments; i++)
{
var DepartmentArray = Departments[i];
var EmployeeCurrentTitle = Employees[selectedIndex].department;
alert("Current Title: " + EmployeeCurrentTitle + "; " + "Dept Array is: " + DepartmentArray);
}
for(var i=0; i<totalDepartments; i++)
{
var DepartmentArray = Departments[i];
var EmployeeCurrentTitle = Employees[selectedIndex].department;
alert("Current Title: " + EmployeeCurrentTitle + "; " + "Dept Array is: " + DepartmentArray);
if(EmployeeCurrentTitle == DepartmentArray)
{
alert("Match Found")
}
if(EmployeeCurrentTitle != DepartmentArray)
{
alert("Match Not Found")
}
}
});
for(var i=0; i<totalDepartments; i++)
{
var DepartmentArray = Departments[i];
var EmployeeCurrentTitle = Employees[selectedIndex].department;
// alert("Current Title: " + EmployeeCurrentTitle + "; " + "Dept Array is: " + DepartmentArray);
if(EmployeeCurrentTitle == DepartmentArray)
{
// alert("Match Found")
$("#departmentID").append("<option selected='selected'>" + EmployeeCurrentTitle + "</option>");
}
if(EmployeeCurrentTitle != DepartmentArray)
{
// alert("Match Not Found")
$("#departmentID").append("<option>" + DepartmentArray + "</option>");
}
// refresh and force rebuild of the combo box
$("#departmentID").selectmenu("refresh", true); }
});
Now that we can view, add, and update employees in our Employee Directory app. The last major thing we would like to do is to have the ability to DELETE an employee from the Employee Directory. The delete page typically does not need a dedicated page to perform this operation. However, it is best practice that when you delete content from an app to prompt the user about the pending deletion and give the user an opportunity to cancel the deletion if perhaps the user clicked the wrong button or had a change of mind.
// DELETE EMPLOYEE BUTTON FUNCTIONALITY --------------------------------------------
$("#delete_employee_btn").click(function(e){
var choose = confirm("Are you sure you want to delete this employee?");
if (choose == true) {
alert("You pressed the OK button!");
}
else
{
alert("You pressed the Cancel button!");
}
}); // END OF DELETE EMPLOYEE BUTTON FUNCTIONALITY
}); // END OF DOCUMENT.READY() FUNCTIONALITY
// DELETE EMPLOYEE BUTTON FUNCTIONALITY --------------------------------------------
$("#delete_employee_btn").click(function(e){
var choose = confirm("Are you sure you want to delete this employee?");
if (choose == true) {
// alert("You pressed the OK button!");
Employees.splice(selectedIndex,1); // Remove selectedItem form Employees array
$("#myList li").eq(selectedIndex).remove(); // Remove selectedItem from ListView
window.location.href = "#home_page"; // Return to Master Page (e.g., home_page)
}
else
{
// alert("You pressed the Cancel button!");
window.location.href = "#home_page";
}
});
}); // END OF DELETE EMPLOYEE BUTTON FUNCTIONALITY
A login would be nice so that a user would have to log to see the Employee Directory.
Will be added later….
A registration page will also an employee to enter his own information into the Employee Directory.
Will be added later….
Currently, the data is not actually being saved anywhere. If you make changes (add, update or delete an employee(s)) and then close the app and reopen it, the data will be lost. To maintain the data, we will implement a local storage of the array of objects so that when you come back to the app after it has been closed the data will persist.
If you want to store some simple data for your app instead of using a database, you can use an HTML5 feature called Local Storage (similar to a cookie) that allows you to store key/value pairs on the client computer that can be retrieved from the client computer and that can persist after the app is restarted repeatedly.
Local storage, as the name implies, allows you to store data locally within the user's browser. However, unlike cookies, local storage is:
Local storage provides two objects for storing data on the client:
There are five methods that can be used with the local storage object:
However, the current version only support strings. To use an array or JSON object, you need to use two JSON methods:
EXAMPLE:
var ArrayData = [5, 6, 9];
// store array to localstorage
localStorage.setItem("list_data_key", JSON.stringify(ArrayData));
// retrieve stored data (JSON stringified) and convert
var storedData = localStorage.getItem("ArrayData ");
if (storedData) {
ArrayData = JSON.parse(storedData);
}
Other methods that you can use are:
EXAMPLE:
// To remove a SINGLE key
localStorage.removeItem("item_key");
// To clear ALL Local Storage
localStorage.clear();
Employees [...];
var numberOfEmployees = Employees.length;
// alert("NOE " + numberOfEmployees);
// alert(Employees[0].firstName);
// Retrieve stored data (JSON stringified) and convert var storedData = localStorage.getItem("list_data_key"); if(storedData) { alert("What is StoredData value? " + storedData); Employees = JSON.parse(storedData); numberOfEmployees = Employees.length; // Reset number of Employee if data is stored. } else { alert("No data stored") }
// ADD EMPLOYEE BUTTON FUNCTIONALITY ----------------------
... ...
// store array to localstorage
localStorage.setItem("list_data_key", JSON.stringify(Employees));
}); // END OF ADD EMPLOYEE BUTTON FUNCTIONALITY
// SAVE UPDATES BUTTON FUNCTIONALITY ----------------------
... ...
// store array to localstorage
localStorage.setItem("list_data_key", JSON.stringify(Employees));
}); // ENOD OF SAVE UPDATES BUTTON FUNCTIONALITY
// DELETE EMPLOYEE BUTTON FUNCTIONALITY ----------------------
... ...
if (choose == true) {
... // store array to localstorage
localStorage.setItem("list_data_key", JSON.stringify(Employees));
}
else
{
// alert("You pressed the Cancel button!");
window.location.href = "#home_page";
}
}); // END OF DELETE EMPLOYEE BUTTON FUNCTIONALITY
// Retrieve stored data (JSON stringified) and convert /* var storedData = localStorage.getItem("list_data_key"); if(storedData) { alert("What is StoredData value? " + storedData); Employees = JSON.parse(storedData); numberOfEmployees = Employees.length; // Reset number of Employee if data is stored. } else { alert("No data stored") } */
localStorage.clear();

If you currently view the app on a small device, you will notice that the title get truncated by ellipses (e.g., EMPLOYEE DIRECT…) and the footer is rather large. To avoid this minor issue, will we create a media query and set a breakpoint for a maximum side of 480px.
<style>
h1.ui-title{color:red;}
@media (max-width:480px){
/* Header */
h1.ui-title{color:orange;
font-size:10px!important;}
/* Footer */
h4.ui-title{color:gray;
font-size:10px!important;}
}
</style>
</head>


You can always change the style of the page.
<div data-role="page" id="home_page" data-theme="a">
<div data-role="header" data-position="fixed" data-theme="b">
<h1>EMPLOYEE DIRECTORY</h1>
</div>
….
<div data-role="footer" data-position="fixed" data-theme="b">
<h4>Copyrighted 2016 by RMCS. All rights reserved.</h4>
</div>
</div>
<div data-role="page" id="employee_page" data-theme="a">
<div data-role="header data-theme="b"">
<a data-icon="home" data-iconpos="left" href="#home_page">HOME</a>
<h1>EMPLOYEE DETAIL</h1>
</div>
…. <div data-role="footer" data-position="fixed" data-theme="b"> <h4>Copyright 2016 by RMCS. All rights reserved.</h4> </div>
</div>
Currently, the image size does not changed regardless of the size of the device that it is displayed on. In the next step, we will add some CSS to have the image SCALE automatically on smaller devices.
#employee_page img
{
border: 1px solid black;
max-width: 100%;
height: auto;
display: block;
}
</style>
Currently, the Delete Employee dialog box is using a simple JavaScript confirm() method. In the series of upcoming steps, we will replace it with a jQuery Mobile dialog box.
<!--Delete Dialog Page----------------------------------------------------->
<div data-dialog="true" data-theme="b" data-role="page" id="delete_dialog_page">
<div data-role="header">
<h1>Delete Employee</h1>
</div>
<div id="container2" data-role="content">
<!--<p>Dynamic content will go here...</p>-->
<p>Are you sure you want to delete this employee?</p>
</div>
<div style="padding:20px;">
<a href="#" id="confirm_delete" data-role="button" data-theme="a">Yes</a>
<a href="#" data-rel="back" data-role="button" data-theme="a">No</a>
</div>
</div>
</body>
<a href="#delete_dialog_page" data-role="button" id="delete_employee_btn"
data-icon="delete" data-inline="true" data-mini="true" data-theme="b">DELETE EMPLOYEE</a>
.ui-dialog-contain {
width: 92.5%;
max-width: 350px;
margin: 10% auto 15px auto;
padding: 0;
position: relative;
top: -15px;
}
</style>
// DELETE EMPLOYEE BUTTON FUNCTIONALITY --------------------------------------------
/* $("#delete_employee_btn").click(function(e){
...
...
}); */
// DELETE CONFIRMATION BUTTON FUNCTIONALITY -------------------------------------- $("#confirm_delete").click(function(e){ Employees.splice(selectedIndex,1); // Remove selectedItem form Employees array $("#myList li").eq(selectedIndex).remove(); // Remove selectedItem from ListView window.location.href = "#home_page"; // Return to Master Page (e.g., home_page) // store array to localstorage localStorage.setItem("list_data_key", JSON.stringify(Employees)); }); // end of confirm delete
}); // END OF DOCUMENT.READY() FUNCTIONALITY

$("#employee_page div#container").append("<p><strong style = 'color:red'>Start Date:</strong> " + startDate + "</p>");
$("#employee_page div#container").append("<p><strong style = 'color:red'>Department:</strong> " + department + "</p>");
$("#delete_dialog_page div#container2").empty();
$("#delete_dialog_page div#container2").append("<p>Are you sure you want to delete " + fullName + "?</p>");
});
[back to top] Now that we have the majority of the app completed, we would like to turn it into an app that can be installed on a phone or tablet using PhoneGap Build.