技术问询:JavaScript代码值表格化展示及数据库业务流程实现需求
实现Intervention创建流程中的技师费率表格展示(基于给定数据库结构)
我来帮你梳理下如何基于给定的数据库结构,实现创建Intervention时展示技师费率表格的功能。先从数据库表的结构入手,再一步步拆解流程和实现代码:
一、数据库表结构明细
先把5张表的结构整理成清晰的表格,方便理解关联关系:
| 表名 | 字段列表 | 核心关联说明 |
|---|---|---|
metier | id(主键), libelle_metier | 存储职业分类,是tache表的父表 |
tache | id(主键), libelle_tache, metier_id(外键关联metier.id) | 存储具体任务,每个任务归属一个职业分类 |
technicien | id(主键), actif, user_id | 存储技师信息,actif标记技师是否可用 |
tarificationtache | id(主键), tarif, tache_id(外键关联tache.id), technicien_id(外键关联technicien.id) | 存储每个技师对应不同任务的费率,是tache和technicien的关联中间表 |
intervention | id(主键), discription, technicien_id, tarification_id, client_id... | 存储服务干预记录,关联具体技师、费率和客户 |
二、创建Intervention的核心流程
整个流程分为三步,前端需要配合后端接口完成数据联动:
- 第一步:选择职业类型
用户从下拉框或列表中选择一个metier,前端将选中的metier.id传给后端,请求该职业下的所有tache数据。 - 第二步:选择关联任务
用户从返回的tache列表中选中一个任务,前端将选中的tache.id传给后端,请求该任务对应的所有技师及费率数据(需要关联technicien和tarificationtache表,且只返回actif=1的可用技师)。 - 第三步:渲染技师费率表格
后端返回数据后,前端将数据渲染成直观的表格,展示技师信息和对应费率,用户选择后即可创建intervention记录。
三、JavaScript前端实现示例
下面是一个基础的前端实现思路,包含DOM结构、事件监听和表格渲染逻辑:
1. 基础HTML结构
<!-- 职业选择下拉框 --> <select id="metierSelect"> <option value="">请选择职业类型</option> </select> <!-- 任务选择下拉框 --> <select id="tacheSelect" disabled> <option value="">请先选择职业类型</option> </select> <!-- 技师费率表格容器 --> <div id="tarifTableContainer"></div>
2. JavaScript逻辑
// 初始化职业下拉框 async function loadMetiers() { const response = await fetch('/api/metiers'); // 后端接口:获取所有metier const metiers = await response.json(); const select = document.getElementById('metierSelect'); metiers.forEach(metier => { const option = document.createElement('option'); option.value = metier.id; option.textContent = metier.libelle_metier; select.appendChild(option); }); } // 职业选择变化时,加载对应任务 document.getElementById('metierSelect').addEventListener('change', async function() { const metierId = this.value; const tacheSelect = document.getElementById('tacheSelect'); tacheSelect.disabled = !metierId; tacheSelect.innerHTML = '<option value="">加载中...</option>'; if (!metierId) { tacheSelect.innerHTML = '<option value="">请先选择职业类型</option>'; document.getElementById('tarifTableContainer').innerHTML = ''; return; } const response = await fetch(`/api/taches?metierId=${metierId}`); // 后端接口:根据metierId获取tache const taches = await response.json(); tacheSelect.innerHTML = '<option value="">请选择任务</option>'; taches.forEach(tache => { const option = document.createElement('option'); option.value = tache.id; option.textContent = tache.libelle_tache; tacheSelect.appendChild(option); }); }); // 任务选择变化时,加载并渲染技师费率表格 document.getElementById('tacheSelect').addEventListener('change', async function() { const tacheId = this.value; const container = document.getElementById('tarifTableContainer'); container.innerHTML = '<p>加载中...</p>'; if (!tacheId) { container.innerHTML = ''; return; } const response = await fetch(`/api/tarifications?tacheId=${tacheId}`); // 后端接口:根据tacheId获取技师及费率 const tarifications = await response.json(); // 渲染表格 if (tarifications.length === 0) { container.innerHTML = '<p>暂无可用技师及费率信息</p>'; return; } let tableHtml = ` <table border="1" cellpadding="8" cellspacing="0"> <thead> <tr> <th>技师ID</th> <th>是否可用</th> <th>费率</th> <th>操作</th> </tr> </thead> <tbody> `; tarifications.forEach(item => { tableHtml += ` <tr> <td>${item.technicien_id}</td> <td>${item.actif ? '是' : '否'}</td> <td>${item.tarif}</td> <td><button onclick="selectTarification(${item.id}, ${item.technicien_id})">选择</button></td> </tr> `; }); tableHtml += '</tbody></table>'; container.innerHTML = tableHtml; }); // 选中费率后,后续创建intervention的逻辑 function selectTarification(tarificationId, technicienId) { // 这里可以把选中的tarificationId和technicienId存储起来,用于后续创建intervention的请求 console.log('选中的费率ID:', tarificationId, '技师ID:', technicienId); // 比如跳转到创建intervention的页面,或者弹出表单填写其他信息 } // 页面加载时初始化职业列表 window.addEventListener('load', loadMetiers);
四、后端接口注意事项
后端需要提供三个核心接口,且查询时要注意关联表的过滤:
/api/metiers:查询所有metier数据/api/taches?metierId=xxx:通过metier_id关联查询tache表/api/tarifications?tacheId=xxx:关联tarificationtache和technicien表,查询条件为tache_id=xxx且technicien.actif=1,返回包含technicien_id、actif、tarif的组合数据
这样就能完整实现创建Intervention时的技师费率表格展示功能了。
内容的提问来源于stack exchange,提问作者chagou2704
相关产品推荐
相关产品推荐

