AppML 案例: Employees
在这个案例中,我们将探讨如何使用AppML来构建一个员工管理系统。这个系统将允许用户查看、添加、编辑和删除员工信息。我们将通过创建一个简单的HTML页面和一个AppML模型来实现这个系统。
1. 创建HTML页面
首先,我们需要创建一个HTML页面来显示员工信息。这个页面将包含一个表格,用于显示员工列表,以及一些按钮,用于添加、编辑和删除员工。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AppML 案例: Employees</title>
</head>
<body>
<h1>员工列表</h1>
<table id="employeesTable">
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>职位</th>
<th>薪资</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<!-- 员工数据将在这里显示 -->
</tbody>
</table>
<button id="addEmployeeBtn">添加员工</button>
<script src="appml.js"></script>
<script>
// AppML代码将在这里编写
</script>
</body>
</html>
2. 创建AppML模型
接下来,我们需要创建一个AppML模型,用于定义员工数据的结构和操作。这个模型将包含员工的基本信息,如ID、姓名、职位和薪资。
<appml>
<model name="Employee">
<fields>
<field name="ID" type="integer" primary="true" auto="true" />
<field name="Name" type="string" required="true" />
<field name="Position" type="string" required="true" />
<field name="Salary" type="decimal" required="true" />
</fields>
<methods>
<method name="getAll" />
<method name="add" />
<method name="update" />
<method name="delete" />
</methods>
</model>
</appml>
3. 编写AppML代码
现在我们需要编写一些JavaScript代码,用于初始化AppML模型,并将员工数据加载到表格中。
// 初始化AppML模型
const employeeModel = new AppML("Employee");
// 加载员工数据
employeeModel.getAll().then((data) => {
const tableBody = document.getElementById("employeesTable").querySelector("tbody");
tableBody.innerHTML = "";
data.forEach((employee) => {
const row = document.createElement("tr");
row.innerHTML = `
<td>${employee.ID}</td>
<td>${employee.Name}</td>
<td>${employee.Position}</td>
<td>${employee.Salary}</td>
<td>
<button class="editEmployeeBtn">编辑</button>
<button class="deleteEmployeeBtn">删除</button>
</td>
`;
tableBody.appendChild(row);
});
});
// 添加员工按钮点击事件
document.getElementById("addEmployeeBtn").addEventListener("click", () => {
// 弹出添加员工表单
});
// 编辑员工按钮点击事件
document.querySelectorAll(".editEmployeeBtn").forEach((button) => {
button.addEventListener("click", () => {
// 弹出编辑员工表单
});
});
// 删除员工按钮点击事件
document.querySelectorAll(".deleteEmployeeBtn").forEach((button) => {
button.addEventListener("click", () => {
// 删除员工
});
});
4. 完成功能
最后,我们需要实现添加、编辑和删除员工的功能。这可以通过创建一些表单来实现,并将表单数据发送到AppML模型的方法中。
<!-- 添加员工表单 -->
<div id="addEmployeeForm" style="display: none;">
<h2>添加员工</h2>
<form>
<label for="name">姓名:</label>
<input type="text" id="name" required>
<br>
<label for="position">职位:</label>
<input type="text" id="position" required>
<br>
<label for="salary">薪资:</label>
<input type="number" id="salary" required>
<br>
<button type="submit">添加</button>
</form>
</div>
<!-- 编辑员工表单 -->
<div id="editEmployeeForm" style="display: none;">
<h2>编辑员工</h2>
<form>
<label for="name">姓名:</label>
<