我已经在这里的 jquery-jtable Github 问题部分提出了这个问题:https ://github.com/hikalkan/jtable/issues/703 。目前似乎没有太多的知识可以分享,这个问题严重阻碍了我项目的进一步发展。
我几乎可以肯定我错过了一些相对简单的东西,因为根据这里的文档:http ://www.jtable.org/ApiReference#fopt-options这真的应该很简单,没有问题。
请注意,此代码的简化版本应说明问题,并不能准确代表代码的实现方式。也就是说,我正在严格尝试解决可重现的问题,而不是如何在我更大的项目中最好地使用它。这是该问题的复制粘贴:
让我们制作 SQL 表:
--Create "employee titles" table
create table employee_titles (employeetitleid int not null IDENTITY, employeetitle varchar(50) not null,
constraint PK_employee_titles_employeetitleid
primary key clustered (employeetitleid))
go
现在让我们制作jtable:
<html>
<head>
<link href="/jtabphp/themes/redmond/jquery-ui-1.8.16.custom.css" rel="stylesheet" type="text/css" />
<link href="/jtabphp/scripts/jtable/themes/lightcolor/blue/jtable.css" rel="stylesheet" type="text/css" />
<script src="/jtabphp/scripts/jquery-1.6.4.min.js" type="text/javascript"></script>
<script src="/jtabphp/scripts/jquery-ui-1.8.16.custom.min.js" type="text/javascript"></script>
<script src="/jtabphp/scripts/jtable/jquery2.3.0.jtable.js" type="text/javascript"></script>
</head>
<body>
<div id="EmployeeTitles" style="width: 600px;"></div>
<script type="text/javascript">
$(document).ready(function () {
//Prepare jTable
$('#EmployeeTitles').jtable({
title: 'Employee Titles',
actions: {
listAction: 'PersonActions.php?action=list',
},
fields: {
employeetitleid: {
key: true,
create: false,
edit: false,
title: 'Title ID',
width: '10%'
},
employeetitle: {
title: 'Employee Title',
options: 'DropdownSelectors.php?Selector=employeetitle',
optionsSorting: 'text',
width: '45%'
}
}
});
//Load person list from server
$('#EmployeeTitles').jtable('load');
});
</script>
</body>
</html>
让我们创建运行 SQL 查询的 personactions.php 文件以获取 action==list:
<?php
// Connect to SQL Server
include '../../phpconfig/connectstrings.php';
try
{
$conn = new PDO ( "sqlsrv:server = $serverstring; Database = $databasestring", "$usernamestring", "$passwordstring");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch ( PDOException $e )
{
print( "Error connecting to SQL Server." );
die(print_r($e));
}
catch(Exception $e)
{
die(var_dump($e));
}
try {
//Getting records (listAction)
if($_GET["action"] == "list")
{
//Get records from database
$sql_select = "SELECT employeetitleid, employeetitle FROM employee_titles";
$stmt = $conn->query($sql_select);
//Add all records to an array
$rows = $stmt->fetchall(PDO::FETCH_ASSOC);
//Return result to jTable
$jTableResult = array();
$jTableResult['Result'] = "OK";
$jTableResult['Records'] = $rows;
print json_encode($jTableResult);
}
//Creating a new record (createAction)
else if($_GET["action"] == "create")
{
//Insert record into database
$sql_insert = "INSERT INTO employee_titles (employeetitle) VALUES (?)";
$stmt = $conn->prepare($sql_insert);
$stmt->bindValue(1, $_POST['employeetitle']);
$stmt->execute();
//Get last inserted record (to return to jTable)
$sql_select = "SELECT employeetitleid, employeetitle FROM employee_titles WHERE employeetitleid = @@IDENTITY";
$stmt = $conn->prepare($sql_select);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
//Return result to jTable
$jTableResult = array();
$jTableResult['Result'] = "OK";
$jTableResult['Record'] = $row;
print json_encode($jTableResult);
}
//Updating a record (updateAction)
else if($_GET["action"] == "update")
{
//Update record in database
$sql_update = "UPDATE employee_titles SET employeetitle = ? WHERE employeetitleid = ?;";
$stmt = $conn->prepare($sql_update);
$stmt->bindValue(1, $_POST['employeetitle']);
$stmt->bindValue(2, $_POST['employeetitleid']);
$stmt->execute();
//Return result to jTable
$jTableResult = array();
$jTableResult['Result'] = "OK";
print json_encode($jTableResult);
}
//Deleting a record (deleteAction)
else if($_GET["action"] == "delete")
{
//Delete from database
$sql_delete = "DELETE FROM employee_titles WHERE employeetitleid = ?;";
$stmt = $conn->prepare($sql_delete);
$stmt->bindValue(1, $_POST['employeetitleid']);
$stmt->execute();
//Return result to jTable
$jTableResult = array();
$jTableResult['Result'] = "OK";
print json_encode($jTableResult);
}
//Close database connection
$conn = null;
}
catch(Exception $ex)
{
//Return error message
$jTableResult = array();
$jTableResult['Result'] = "ERROR";
$jTableResult['Message'] = $ex->getMessage();
print json_encode($jTableResult);
}
?>
最后,让我们创建将查询下拉内容的 DropdownSelectors.php 文件。根据我构建此文件的方式,我将得到 2 个不同的结果,但都不令人满意。
在第一个示例中,我将制作 [DisplayText] == [Value]。这将在 jtable 视图中正确显示员工职位信息,并正确填充创建/编辑的下拉列表。但是,下拉列表报告的 [Value] 对于以后的查询来说几乎没有它实际上是employeetitleid 而不是只是重复employeetitle 时有用。两个示例中显示的代码与 jtable 预期的数组类型完美匹配,如下所示:http ://www.jtable.org/apireference#fopt-options 。这不应该有争议,因为创建/编辑下拉菜单在任一示例中都可以使用。
示例 DropdownSelectors.php #1:
<?php
// Connect to SQL Server
include '../../../phpconfig/connectstrings.php';
try
{
$conn = new PDO ( "sqlsrv:server = $serverstring; Database = $databasestring", "$usernamestring", "$passwordstring");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch ( PDOException $e )
{
print( "Error connecting to SQL Server." );
die(print_r($e));
}
catch(Exception $e)
{
die(var_dump($e));
}
if ($_GET['Selector'] == "employeetitle") {
$sql_select = "SELECT employeetitle [DisplayText], employeetitle [Value] FROM employee_titles";
$stmt = $conn->prepare($sql_select);
$stmt->execute();
$rows= $stmt->fetchAll(PDO::FETCH_ASSOC);
$options[Result] = 'OK';
$options[Options] = $rows;
print json_encode($options);
}
?>
在第二个示例中,我将从employee_titles 表的不同列中提取[DisplayText] 和[Value]。这将导致 jtable 的员工标题列为空,但仍会正确填充下拉列表以进行创建/编辑。在这种情况下,下拉列表报告的 [Value] 对于以后的查询非常有用,因为它实际上报告了employeetitleid,而不仅仅是重复的employeetitle。两个示例中显示的代码与 jtable 预期的数组类型完美匹配,如下所示:http ://www.jtable.org/apireference#fopt-options 。这不应该有争议,因为创建/编辑下拉菜单在任一示例中都可以使用。但是,显示的 jtable 列显示为空白是完全不能接受的。
示例 DropdownSelectors.php #2:
<?php
// Connect to SQL Server
include '../../../phpconfig/connectstrings.php';
try
{
$conn = new PDO ( "sqlsrv:server = $serverstring; Database = $databasestring", "$usernamestring", "$passwordstring");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch ( PDOException $e )
{
print( "Error connecting to SQL Server." );
die(print_r($e));
}
catch(Exception $e)
{
die(var_dump($e));
}
if ($_GET['Selector'] == "employeetitle") {
$sql_select = "SELECT employeetitle [DisplayText], employeetitleid [Value] FROM employee_titles";
$stmt = $conn->prepare($sql_select);
$stmt->execute();
$rows= $stmt->fetchAll(PDO::FETCH_ASSOC);
$options[Result] = 'OK';
$options[Options] = $rows;
print json_encode($options);
}
?>
既然您拥有完全重现这个非常可重现的问题所需的所有代码,谁能告诉我如何解决它,以便列出的字段显示 DisplayText 并且下拉选项发出等同于 ID # 的 vlaue?我开始相信 jtable 本身存在显示错误,并且某处的小修复会导致信息出现在 jtable 视图中。
请参阅链接的 github 问题,了解我尝试过的一些解决方法以及它们为什么不起作用。