【问题标题】:Pagination jTable in PHPPHP中的分页jTable
【发布时间】:2014-11-07 08:05:57
【问题描述】:

如何在jTable中使用分页使用PHP? 我在employeeTable.php 中有以下代码

    <script src="jtable.2.4.0/jquery.jtable.min.js" type="text/javascript"></script>

    //Get record count
    $result = mysql_query("SELECT COUNT(*) AS RecordCount FROM employee;");
    $row = mysql_fetch_array($result);
    $recordCount = $row['RecordCount'];

    //Get records from database
    $result = mysql_query("SELECT * FROM employee ORDER BY '" . $_REQUEST["jtSorting"] . "' LIMIT '" . $_REQUEST["jtStartIndex"] . "','" . $_REQUEST["jtPageSize"] . "';");

    //Add all records to an array
    $rows = array();
    while($row = mysql_fetch_array($result))
    {
        $rows[] = $row;
    }

    //Return result to jTable
    $jTableResult = array();
    $jTableResult['Result'] = "OK";
    $jTableResult['TotalRecordCount'] = $recordCount;
    $jTableResult['Records'] = $rows;
    print json_encode($jTableResult);

然后我意识到问题出在这里

$result = mysql_query("SELECT * FROM employee ORDER BY '" . $_REQUEST["jtSorting"] . "' LIMIT '" . $_REQUEST["jtStartIndex"] . "','" . $_REQUEST["jtPageSize"] . "';");

如果我更改 $_REQUEST["jtSorting"] = rowname, $_REQUEST["jtStartIndex"] = number, $_REQUEST["jtPageSize"] = number,它可以工作。 但如果我不更改它,它会显示“与服务器通信时发生错误”。

这是jquery.jtable.min.js中的代码,当有关于jtSorting,jtStartIndex,jtPageSize的行时

/* Adds jtSorting parameter to a URL as query string.
    *************************************************************************/
    _addSortingInfoToUrl: function (url) {
        if (!this.options.sorting || this._lastSorting.length == 0) {
            return url;
        }

        var sorting = [];
        $.each(this._lastSorting, function (idx, value) {
            sorting.push(value.fieldName + ' ' + value.sortOrder);
        });

        return (url + (url.indexOf('?') < 0 ? '?' : '&') + 'jtSorting=' + sorting.join(","));
    },

    /* Overrides _createJtParamsForLoading method to add sorging parameters to jtParams object.
    *************************************************************************/
    _createJtParamsForLoading: function () {
        var jtParams = base._createJtParamsForLoading.apply(this, arguments);

        if (this.options.sorting && this._lastSorting.length) {
            var sorting = [];
            $.each(this._lastSorting, function (idx, value) {
                sorting.push(value.fieldName + ' ' + value.sortOrder);
            });

            jtParams.jtSorting = sorting.join(",");
        }

        return jtParams;
    }

});
})(jQuery);

谁能帮我理解一下?

【问题讨论】:

    标签: javascript php jquery-jtable


    【解决方案1】:

    我认为你应该经历一次。 Listaction jtable for Pagination and sorting table

    它需要 jQuery.Deferred 来返回数据。

    把它当作

    listAction: function (postData, jtParams) {
        return $.Deferred(function ($dfd) {
            $.ajax({
                url: '/Employee_Controller/EmployeeList_method?jtStartIndex=' + jtParams.jtStartIndex + '&jtPageSize=' + jtParams.jtPageSize + '&jtSorting=' + jtParams.jtSorting,
                type: 'POST',
                dataType: 'json',
                data: postData,
                success: function (data) {
                    $dfd.resolve(data);
                },
                error: function () {
                    $dfd.reject();
                }
            });
        });
    }
    

    手动发布它的值。我希望这可能会有所帮助。

    【讨论】:

      【解决方案2】:

      选项将在 $_GET 对象中接收

      喜欢:

      $jtStartIndex=$_GET['jtStartIndex'];
      $jtPageSize=$_GET['jtPageSize'];
      $jtSorting=$_GET['jtSorting'];
      

      示例:

      $query="select * FROM products ORDER BY $jtSorting LIMIT $jtStartIndex, $jtPageSize;";
      

      在 jtable 设置中:

      paging: true, //Enable paging
      pageSize: 10, //Set page size (default: 10)
      sorting: true, //Enable sorting
      defaultSorting: 'name ASC' ,
                  actions: {
                      listAction: 'data/products.php'//Set default sorting
                  },
      ...
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-04
        • 1970-01-01
        • 2017-07-12
        • 1970-01-01
        相关资源
        最近更新 更多