我得到了以下表格:- person{id,last,first,designation_id} designation{id,name}。我在顶部有一个包含所有名称名称的下拉列表。最初,当页面加载时,系统应该加载行轴上的所有人员,然后当用户选择特定名称时,行轴将更改并显示具有此 designation_id 的人. 如果有人真的可以帮助我这样做,请在下面找到我的代码。
下拉列表没问题,它加载了所有名称下拉部分:
<div class="space">
Employee: <select id="employee"></select>
</div>
loadResources();
document.querySelector("#employee").addEventListener("change", (ev) => {
loadEvents();
});
async function loadResources() {
const {data} = await DayPilot.Http.get("backend_designations.php");
var select = document.querySelector("#employee");
data.forEach(function(item) {
var option = document.createElement("option");
option.value = item.id;
option.innerText = item.name;
select.appendChild(option);
});
loadEvents();
}
我遇到的唯一问题是当页面加载行轴中没有数据时,即使选择一个选项。
index_admin.html 中的行轴代码
function loadEvents() {
const employee = document.querySelector("#employee").value;
const url = `backend_employees_admin.php?resource=${employee}`;
dp.rows.load(url, () => {
});
}
backend_employees_admin.php 编码:
<?php
require_once '_db.php';
if ($_GET["resource"]) {
$stmt = $db->prepare('SELECT * FROM person WHERE designation_ID = :resource ORDER BY last, first ');
$stmt->bindParam(':resource', $_GET['resource']);
}
else
{
$stmt = $db->prepare("SELECT * FROM person ORDER BY last, first ");
}
$stmt->execute();
$list = $stmt->fetchAll();
class Employee {}
$result = array();
foreach($list as $employee) {
$r = new Employee();
$r->id = $employee['id'];
$r->name = $employee['last'].', '.$employee['first'];
//$r->Designation = $employee['Designation'];
$r->resource=$employee['designation_ID'];
$result[] = $r;
}
header('Content-Type: application/json');
echo json_encode($result);
请如果有人可以为我改进代码。