【问题标题】:Datatables Simple Join Query数据表简单连接查询
【发布时间】:2014-05-20 18:45:22
【问题描述】:

我正在使用数据表编辑器组合一个简单的连接查询,但是我遇到了我不理解的错误。我的代码如下:

HTML

            <!-- Actions -->
            <div id="divActions" class="tab-pane fade">
                <ul class="nav nav-list">
                    <!-- Actions Tab Header -->
                    <legend id="lgdActions">Actions</legend>
                    <table id="tblActions" class="display" cellspacing="0" width="100%"></table>
                </ul>
            </div>

Javascript

             Actions Datagrid
            $('#tblActions').DataTable({
                dom: "Tfrtip",
                lengthChange: false,
                bAutoWidth: false,
                jQueryUI: true,
                bProcessing: true,
                bServerSide: true,
                ajax: {
                    url: "dataGridQuery.php",
                    data: { "gridNumber": 2 },
                    type: 'POST'
                },
                columns: [
                    { title: "Action ID", data: "tblActions.actionID", width: "10%" },
                    { title: "Time", data: "tblActions.actionTime" },
                    { title: "Action Taken", data: "tblActions.actionTaken" },
                    { title: "User", data: "tblUsers.userID" }
                ]
                tableTools: {
                    sRowSelect: "os",
                    aButtons: [
                        { sExtends: "editor_create", editor: editor1 },
                        { sExtends: "editor_edit",   editor: editor1 },
                        { sExtends: "editor_remove", editor: editor1 }
                    ]
                }
            });

PHP

            // Obtain Action Grid
            if(isset($_GET['gridNumber']) && $_GET['gridNumber']==2){ 
            //
            $data = Editor::inst( $db, 'tblActions' )
                ->field(
                    Field::inst( 'tblActions.actionID' ),
                    Field::inst( 'tblActions.actionTime' ),
                    Field::inst( 'tblActions.actionTaken' ),
                    Field::inst( 'tblActions.actionUserID' ),
                    Field::inst( 'tblUsers.userID' )
                )
                ->leftJoin( 'tblUsers', 'tblUsers.userID', '=', 'tblActions.actionUserID' )
                ->process($_POST)
                ->data();
            //
            if ( ! isset($_POST['action']) ) {
                // Get a list of sites for the `select` list
                $data['tblUsers'] = $db
                    ->selectDistinct( 'tblUsers', 'userID as value, userID as label' )
                    ->fetchAll();
            }
            // Echo 
            echo json_encode( $data );
            }

运行此代码时收到的错误是:

  • 错误 {"error":"SQLSTATE[42S22]: 未找到列:1054 未知 “字段”中的“tblActions.id”列 list'","data":[],"tblUsers":[{"value":"1","label":"1"},{"value":"2","label":"2" }]}
  • DataTables 警告:表 id=tblActions - JSON 响应无效。为了 有关此错误的更多信息,请参阅 http://datatables.net/tn/1。

此代码来自下载编辑器 v1.3.1 时提供的示例。我对插件有点熟悉,但是当出现这样的错误时发现它令人困惑。任何帮助将不胜感激。

谢谢:)

【问题讨论】:

  • tblActions.id 表中是否有 id 字段?
  • tblActions 的主键是 actionID。没有“id”字段。
  • 看起来您可能需要在某处更改主键。默认情况下,它似乎正在寻找“id”作为主键。 datatables.net/forums/discussion/19199/custom-primary-key
  • 我的解决方案有效吗?
  • adpro-我将在完成其他工作后对其进行审核并提供后续跟进。谢谢:)

标签: javascript php mysql sql jquery-datatables


【解决方案1】:

您必须更改主键的名称。

我想如果你在某处添加以下内容。

 "idSrc": "actionID",

http://datatables.net/forums/discussion/19199/custom-primary-key

这也可能有效

        $data = Editor::inst( $db, 'tblActions' )
            ->pkey( 'actionID' )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-13
    • 1970-01-01
    相关资源
    最近更新 更多