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
include
to include the header<%- include('partials/header'); %>
- Under the
include
, create adiv
- Within the
div
, create atable
with aclass
of "table" - Within the
table
, create athead
with aclass
of "thead-dark" - Within the
thead
, create atr
- Within the
tr
, createth
s for:- First Name
- Last Name
- Position
- Number
- Under the
thead
in thetable
, create atbody
- In the
tbody
, create atr
- In the
tr
, createtd
s containing:- Megan
- Rapinoe
- Midfielder
- 15
- At the bottom of the file, under the closing
div
tag, 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
tbody
opening tag, create an EJS scriptlet containing afor
loop that will loop throughplayers
- Make sure to include the closing
<% } %>
for the loop under thetr
closing tag!
- Make sure to include the closing
- Within the
td
elements, 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
div
opening tag, create an EJS scriptlet containing anif
statement - Set the condition of the
if
statement to check if there are any players in theplayers
array - Under the
table
closing tag, create an EJS scriptlet that closes theif
statement, and starts anelse
- Under the
else
scriptlet, add ap
with aclass
of "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>