我有一个名为“活动”的复选框,当员工进入系统时它总是被选中,并且它会显示活动员工的数据。现在,如果我取消选中该复选框,它应该显示非活动工作人员的数据,在我的情况下它不起作用。请检查我的代码,如果您为我提供一些解决方案,这将对我有帮助
这是我的复选框代码
<div class="col-lg-3">
<label for="activeWorker">
<input type="checkbox" id="activeWorker" name="activeWorker" value="active" checked> Active Workers
</label>
</div>
这是我根据所选日期范围重新加载数据的函数代码
function reload() {
var dateFrom = $("#dateFrom").val();
var dateTo = $("#dateTo").val();
var coid = $("#selectCompanyId").val();
var activeStatus = $("#activeWorker").val();
var dataURL;
alert("You have clicked the reload button. Reloading the data for the date range:" + dateFrom + " to " + dateTo + " For Company : " + coid);
if (activeTab == "#tab-1") { // Pending
dataURL = "data/data_workerSurvey.php?mod=pending&dateFrom=" + dateFrom + "&dateTo=" + dateTo + "&coid";
$('#tab1_tblSurveyPending').DataTable().ajax.url(dataURL).load();
} else if (activeTab == "#tab-2") { // Completed
dataURL = "data/data_workerSurvey.php?mod=completed&dateFrom=" + dateFrom + "&dateTo=" + dateTo + "&coid=" + coid;
$('#tab2_tblSurveyCompleted').DataTable().ajax.url(dataURL).load();
} else { // All
dataURL = "data/data_workerSurvey.php?mod=all&dateFrom=" + dateFrom + "&dateTo=" + dateTo + "&coid=" + coid;
$('#tab3_tblSurveyAll').DataTable().ajax.url(dataURL).load();
}
}
这是我的数据库代码
case "pending":
$dateFrom = $_GET['dateFrom']; // Every time will have value
$dateTo = $_GET['dateTo']; // Every time will have value
$coid = $_GET['coid']; // This is not certain, depends on user selection
$activeStatus = TRUE; // TRUE - Showing active worker; FALSE - showing inactive worker
if ($activeStatus) {
$whereClause = " AND a.biodata_status_id IN ('E', 'P') ";
} else {
$whereClause = " AND a.biodata_status_id = 'N' ";
}
if ($coid !== "" && $coid !== null) {
$sql = "SELECT a.worker_id, a.worker_name, a.batchno, a.passport_no, a.company, a.arrival_date, a.plks, a.co_id, IF(DATEDIFF(a.plks, a.arrival_date) < 365 * 2, DATE_ADD(a.plks, INTERVAL 1 YEAR), a.plks) AS contract_end_date
FROM rbs_worker a
LEFT JOIN tbl_contract_extension_survey b ON (b.workerid = a.id AND b.status = 1 AND b.contract_end_date >= a.plks)
LEFT JOIN rbs_payroll_company c ON c.co_id = a.co_id
WHERE a.worker_id IS NOT NULL AND a.arrival_date IS NOT NULL AND a.plks IS NOT NULL
AND a.status = 'A'
AND a.co_id = %s
AND IF(DATEDIFF(a.plks, a.arrival_date) < 365 * 2, DATE_ADD(a.plks, INTERVAL 1 YEAR), a.plks) >= %s
AND IF(DATEDIFF(a.plks, a.arrival_date) < 365 * 2, DATE_ADD(a.plks, INTERVAL 1 YEAR), a.plks) <= %s AND b.id IS NULL" . $whereClause;
$rs = DB::query($sql, $coid, $dateFrom, $dateTo);
} else {
$sql = "SELECT a.worker_id, a.worker_name, a.batchno, a.passport_no, a company, a.arrival_date, a.plks, a.co_id, IF(DATEDIFF(a.plks, a.arrival_date) < 365 * 2, DATE_ADD(a.plks, INTERVAL 1 YEAR), a.plks) AS contract_end_date
FROM rbs_worker a
LEFT JOIN tbl_contract_extension_survey b ON (b.workerid = a.id AND b.status = 1 AND b.contract_end_date >= a.plks)
LEFT JOIN rbs_payroll_company c ON c.co_id = a.co_id
WHERE a.worker_id IS NOT NULL AND a.arrival_date IS NOT NULL AND a.plks IS NOT NULL
AND a.status = 'A'
AND IF(DATEDIFF(a.plks, a.arrival_date) < 365 * 2, DATE_ADD(a.plks, INTERVAL 1 YEAR), a.plks) >= %s
AND IF(DATEDIFF(a.plks, a.arrival_date) < 365 * 2, DATE_ADD(a.plks, INTERVAL 1 YEAR), a.plks) <= %s AND b.id IS NULL" . $whereClause;
$rs = DB::query($sql, $dateFrom, $dateTo);
}
我希望如果工作人员取消选中该框,他们应该能够查看不活动的工作人员数据。
一些事情:
您将复选框的值设置为“活动”,但没有 JS 代码来控制它,因此它始终处于活动状态。
您有一个变量“activeStatus”,它获取复选框的值,如上所述,该复选框始终“活动”,但您没有显示它的使用位置。它不会附加到 dataURL 的任何地方,因此它永远不会发送到服务器。
我想这就是您将数据库代码中的 $activeStatus 设置为 true 的原因?!应该是$_GET['activeStatus'],应该添加到dataURL中,但不是通过检查值的方式,而是通过检查checked属性是否为true。
如果您使用表单提交方法,名称/值组合仅适用于表单字段。如果您使用 AJAX,它们毫无价值,请使用 id 和字段的状态/内容。所以在这种情况下:
// will be true or false
let activeStatus = $("#activeWorker").prop('checked');
...然后:
dataURL = "data/data_workerSurvey.php?mod=pending&activeStatus=" + activeStatus + dateFrom=" + dateFrom + "&dateTo=" + dateTo + "&coid";
然后在你的数据库代码中你可以说:
$activeStatus = (bool)$_GET['activeStatus'];
尝试一下。