我在mongodb中有2个相关文档:部门和职能。每个部门都有一套职能。我想用expressjs在相同的路线上打印它们。
路线是这样:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<style>
table {
border-collapse: collapse;
border: 1px solid #000;
}
table td {
border: 1px solid #000;
}
td {
text-align: center;
font-size: 0;
width: 30px;
height: 30px;
background-color: #fff;
font-weight: bold;
}
tr.hover {
color: #fff;
background-color: #000;
}
/* tr:hover td {
background-color: transparent;
} */
td.hover {
color: #fff;
background-color: #000;
}
td.center {
color: #fff;
background-color: #fff;
outline: 2px red solid;
}
</style>
<script>
$(document).ready(function() {
var HEIGHT = 5;
var WIDTH = 5;
$('td').hover(function() {
var self = $(this);
var table = self.parent().parent();
var column = self.index();
var row = self.parent().index();
var current = table.find(`tr:eq(${row}) td:eq(${column})`)
current.toggleClass('center')
if (column > 0) {
var before = table.find(`tr:eq(${row}) td:eq(${column - 1})`)
before.toggleClass('hover');
}
if (row < HEIGHT - 1) {
var bottom = table.find(`tr:eq(${row + 1}) td:eq(${column})`)
bottom.toggleClass('hover');
}
if (column < WIDTH - 1) {
var next = table.find(`tr:eq(${row}) td:eq(${column + 1})`)
next.toggleClass('hover');
}
if (row > 0 && column < WIDTH - 1) {
var nextUp = table.find(`tr:eq(${row - 1}) td:eq(${column + 1})`)
nextUp.toggleClass('hover');
}
});
});
</script>
</head>
<body>
<table>
<tr>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
</table>
</body>
</html>
模型是:
//==============INDEX ROUTE================
router.get('/departments', function(req, res){
//Get all departments from database
Department.find({}).populate("occupations").exec(function(err, allDepartments){
if(err){
console.log(err);
} else {
res.render('departments/index', {departments: allDepartments});
}
});
});
和
var departmentSchema = new mongoose.Schema({
departmentName: String,
occupations: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "Occupation"
},
occupationName: String
}
});
我想在屏幕上这样打印: