数据表简单连接查询

时间:2014-05-20 18:45:23

标签: javascript php mysql sql jquery-datatables

我正在使用Datatables Editor汇总一个简单的连接查询但是我遇到了一些我不理解的错误。我的代码如下:

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 );
            }

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

  • 错误{“错误”:“SQLSTATE [42S22]:未找到列:1054未知 '字段中的'tblActions.id'列 列表'”, “数据”:[], “向tblUsers”:[{ “值”: “1”, “标签”: “1”},{ “值”: “2”, “标签”: “2” }]}
  • DataTables警告:table id = tblActions - 无效的JSON响应。对于 有关此错误的更多信息,请参阅 http://datatables.net/tn/1

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

谢谢:)

1 个答案:

答案 0 :(得分:0)

您必须更改主键的名称。

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

 "idSrc": "actionID",

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

这也可以起作用

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