CRUD App - Read (Table)
The goal of the homepage is to display all of the data in a nice tabular format. This is possible using an HTML table and some Bootstrap styles.
The Table Structure
First, create the HTML table structure without using any actual data. This will make it easier to visualize the data. Open the views/index.ejs file and remove all the existing code to get started.
- Add an
includeto include the header<%- include('partials/header'); %>
- Under the
include, create adiv - Within the
div, create atablewith aclassof "table" - Within the
table, create atheadwith aclassof "thead-dark" - Within the
thead, create atr - Within the
tr, createths for:- First Name
- Last Name
- Position
- Number
- Under the
theadin thetable, create atbody - In the
tbody, create atr - In the
tr, createtds containing:- Megan
- Rapinoe
- Midfielder
- 15
- At the bottom of the file, under the closing
divtag, add closing tags fordiv,body, andhtml- These were opened in the header.ejs file
- Load up the homepage and verify that the table appears properly!
index.ejs
<%- include('partials/header'); %>
<div>
<table class="table">
<thead class="thead-dark">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Position</th>
<th>Number</th>
</tr>
</thead>
<tbody>
<tr>
<td>Megan</td>
<td>Rapino</td>
<td>Midfielder</td>
<td>15</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>
Using the Data in the Table
Now that the table structure is in place, update the EJS code so that it actually uses the data from the database. It should loop through each player, and create a new table row HTML element with all of their information.
- Under the
tbodyopening tag, create an EJS scriptlet containing aforloop that will loop throughplayers- Make sure to include the closing
<% } %>for the loop under thetrclosing tag!
- Make sure to include the closing
- Within the
tdelements, replace the example data with EJS segments that have data fromplayers[i] - Load up the homepage, and verify that the table is populated with data from the database!
Code
<% for (let i = 0; i < players.length; i++) { %>
<tr>
<td><%= players[i].first_name %></td>
<td><%= players[i].last_name %></td>
<td><%= players[i].position %></td>
<td><%= players[i].number %></td>
</tr>
<% } %>
Conditional Display
If there are no players in the database, a message saying "No players found" should appear instead of the table. Use EJS scriptlets with an if/else to accomplish this.
- Under the
divopening tag, create an EJS scriptlet containing anifstatement - Set the condition of the
ifstatement to check if there are any players in theplayersarray - Under the
tableclosing tag, create an EJS scriptlet that closes theifstatement, and starts anelse - Under the
elsescriptlet, add apwith aclassof "text-center" saying "No players found." - Under the
p, create an EJS scriptlet that closes theelse
Unfortunately, there is no way to test this without removing all keys from the database. Test out the feature after implementing the Delete functionality!
The code in the index.ejs file should look something like this:
<%- include('partials/header'); %>
<div>
<% if (players.length > 0) { %>
<table class="table">
<thead class="thead-dark">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Position</th>
<th>Number</th>
</tr>
</thead>
<tbody>
<% for (let i = 0; i < players.length; i++) { %>
<tr>
<td><%= players[i].first_name %></td>
<td><%= players[i].last_name %></td>
<td><%= players[i].position %></td>
<td><%= players[i].number %></td>
</tr>
<% } %>
</tbody>
</table>
<% } else { %>
<p class="text-center">No players found.</p>
<% } %>
</div>
</div>
</body>
</html>