But it is calling the empModel.getEmployees() method, which we haven’t written yet, and also needs the emplistall.dt file, which we haven’t created yet, so let’s resolve that.
First, let’s define the getEmployees() method at the end of source\empmodel.d:
You should make sure the order of fields in prepareEmployee() follows the order of fields in the MySQL employees table. Sometimes MySQL does not save the fields in the same order as in your SQL CREATE TABLE statement. Check the Schemas view of MySQL Browser.
Now let’s create views\emplistall.dt.
extends layout
block maincontent
include csstable.dt
div.table-wrapper
table
tr
th Employee Id
th First name
th Last name
th Department
th Phone number
th Email address
th Street address
th City
th Province
th PostCode
th Action
-foreach(e; emps)
tr
td #{e.empid}
td #{e.fname}
td #{e.lname}
td #{e.deprt}
td #{e.phone}
td #{e.email}
td #{e.street}
td #{e.city}
td #{e.province}
td #{e.postcode}
td
form.form-hidden(method="get", action="edit_employee")
input(type="hidden", name="id", value="#{e.id}")
input(type="image", src="images/pencil.ico", height="15px")
|
form.form-hidden(method="get", action="delete_employee")
input(type="hidden", name="id", value="#{e.id}")
input(type="image", src="images/trash.ico", height="15px")
|
As mentioned before, we can mix D code inside a Diet template file with – (hyphen)
Remember that we passed an Employee array emps to the template views\emplistall.dt:
which will iterate through all of emps, which is an array of Employees, with e representing the current Employee record being processed inside the loop.
To display the value of a variable passed to the template, we use the construct #{variable}. This is equivalent to JSP’s <%= variable %> construct.
Hence the line
td #{e.deprt}
means display the value of e.deprt inside that table cell.
And here is views\csstable.dt which is needed by emplistall.dt.
Compile and run the app and refresh the browser. Click on New employee again and add another employee, then click the Submit button.
And after adding some employees, you should see this:
I got the icons for the pencil and the trash bin by simply googling for ‘pencil icon’ and ‘trash bin icon’ and saving the icons into the \public\images\ folder.
In the views\emplistall.dt file, we have these lines