当前位置: 首页 > 知识库问答 >
问题:

Datatable jquery ajax php无法获取表中的数据(服务器端处理)

谷泳
2023-03-14

我在下面的示例中使用datatablehttps://datatables.net/examples/data_sources/server_side.html

所以我的桌子是:

<table cellpadding="0" cellspacing="0" border="0" class="display" id="tabellaGlossario">
    <thead>
        <th>
             <td>Voce</td>
             <td>Sinonimi</td>
             <td>Sigla</td>
             <td>Macrosettore</td>
             <td>Microsettore</td>      
             <td>Sinonimi</td>
             <td>Sigla</td>
             <td>Macrosettore</td>
             <td>Microsettore</td>           
        </th>
    </thead>
    <tfoot>
        <th>
             <td>Voce</td>
             <td>Sinonimi</td>
             <td>Sigla</td>
             <td>Macrosettore</td>
             <td>Microsettore</td>      
             <td>Sinonimi</td>
             <td>Sigla</td>
             <td>Macrosettore</td>
             <td>Microsettore</td>           
        </th>
    </tfoot>
</table>

我的js:

oTable = $('#tabellaGlossario').dataTable({
        "bJQueryUI": true,
        "sPaginationType": "full_numbers",
        "sDom": '<""f>t<"F"lp>',
        "processing": true,
        "serverSide": true,
        "ajax": "Modules/Glossario/View/Glossario.Table.View.php?lingua_select=2",
    });

我的ajax返回:

{
  "draw": 1,
  "recordsTotal": 1,
  "recordsFiltered": 1,
  "data": [
    [
      "1",
      "2",
      "1",
      "1",
      "1",
      "Parola italiana",
      "Sinonimo italiano",
      "Sigla ita",
      "Note ita"
    ]
  ]
}

我的问题是,我总是得到“表中没有可用数据”作为表结果。但是正如您所看到的,ajax有一些结果(本例中为1)。我的代码似乎与官方示例中的代码相同。

无法理解为什么数据不显示在表中(并且我在浏览器控制台中没有得到错误)。

共有1个答案

封昊天
2023-03-14

您使用的是动态加载还是任何类型的路由?例如angularjs ngroute或某些框架。

在这种情况下,它不能工作(不像你正在做的那样)。您可以遵循以下指南或示例http://jsfiddle.net/qu4a7j24/3/

<div ng-app='testTableApp'>

    <div class="container">
        <div ng-controller="mainTable">
            <form action="" method="POST" class="form-horizontal" role="form">
                <div class="form-group">
                    <legend>Filters</legend>
                </div>
                <div class="form-group">
                    <div class="col-sm-10 col-sm-offset-2">
                        <input type="text" value="0" ng-change='reloadData()' ng-model="start">
                        <input type="text" value="50" ng-change='reloadData()' ng-model="end">

                    </div>
                </div>
            </form>

            <table datatable="" dt-options="dtOptions" dt-columns="dtColumns" class="table table-striped table-bordered"></table>
        </div>
    </div>
</div>

var testTableApp = angular.module( 'testTableApp', ['ngRoute', 'ngResource', 'datatables', 'datatables.tabletools', 'datatables.bootstrap', 'datatables.fixedheader'] );
console.log( testTableApp );
testTableApp.controller("mainTable", 
[ '$scope', 'DTOptionsBuilder', 'DTColumnBuilder',
    function ( $scope, DTOptionsBuilder, DTColumnBuilder){
        $scope.dataSource = "http://dt.ishraf.com/ajax.php";
        $scope.start = 0;
        $scope.end = 5000;


        $scope.getDataSource = function(obj,prefix){
            var src = $scope.dataSource;

            var str = [];
            for(var p in obj) {
                if (obj.hasOwnProperty(p)) {
                    var k = prefix ? prefix + "[" + p + "]" : p, v = obj[p];
                    str.push(typeof v == "object" ?
                    serialize(v, k) :
                    encodeURIComponent(k) + "=" + encodeURIComponent(v));
                }
            }
            return src + "?" + str.join("&");
        }

        var dsParams = {
            start : $scope.start,
            end : $scope.end
        }

        $scope.dsString = $scope.getDataSource( dsParams );


        $scope.buildTable = function(){
            return DTOptionsBuilder
                .newOptions()
                .withOption('ajax', {
                    // Either you specify the AjaxDataProp here
                    dataSrc: 'data',
                    url: $scope.dsString,
                    type: 'POST'
                }).
                withOption( 'lengthMenu', [
                    [10, 20, 50, 100, 150, 300, 500],
                    [10, 20, 50, 100, 150, 300, 500]
                ])                
                .withTableTools('bower_components/datatables-tabletools/swf/copy_csv_xls_pdf.swf')
                .withTableToolsButtons([
                    {
                        "sExtends": "copy",
                        "sButtonText": "<i class='fa fa-copy'></i>&nbsp;|&nbsp;Copy",
                        "fnInit": function (nButton, oConfig) {
                            $(nButton).addClass('btn btn-success');
                        }
                    },
                    {
                        "sExtends": "print",
                        "sButtonText": "<i class='fa fa-print'></i>&nbsp;|&nbsp;Print",
                        "fnInit": function (nButton, oConfig) {
                            $(nButton).addClass('btn btn-danger');
                        }
                    },
                    {
                        "sExtends": "csv",
                        "sButtonText": "<i class='fa fa-file-o'></i>&nbsp;|&nbsp;CSV",
                        "fnInit": function (nButton, oConfig) {
                            $(nButton).addClass('btn btn-primary');
                        }
                    },
                    {
                        "sExtends": "pdf",
                        "sButtonText": "<i class='fa fa-file-pdf-o'></i>&nbsp;|&nbsp;PDF",
                        "fnInit": function (nButton, oConfig) {
                            $(nButton).addClass('btn btn-warning');
                        }
                    }
                ])
                .withFixedHeader({
                    bottom: true
                })
                .withDOM('<"clear"><"#top.hidden-print"<".row"<".col-md-6"i><".col-md-6"f>><".row"<".col-md-6"l><".col-md-6"p>><"clear">T>rt')
                ;            
        }


        $scope.dtOptions = $scope.buildTable();

        $scope.buildColumns = function(){
            return [
                DTColumnBuilder.newColumn('id').withTitle('ID'),
                DTColumnBuilder.newColumn('firstName').withTitle('First name'),
                DTColumnBuilder.newColumn('lastName').withTitle('Last name'),
                DTColumnBuilder.newColumn('city').withTitle('city'),
                DTColumnBuilder.newColumn('state').withTitle('state'),
                DTColumnBuilder.newColumn('zip').withTitle('zip'),
                DTColumnBuilder.newColumn('country').withTitle('country'),
                DTColumnBuilder.newColumn('phone').withTitle('phone'),
                DTColumnBuilder.newColumn('email').withTitle('email')
            ];
        }

        $scope.dtColumns = $scope.buildColumns();


        $scope.reloadData = reloadData;
        $scope.dtInstance = {};

        function reloadData() {
            var resetPaging = false;
            $scope.dtInstance.reloadData(callback, resetPaging);
        }

        function callback(json) {
            console.log(json);
        }

    }
]);

或者只是动态创建表(.loadjquery可能很有用)

 类似资料:
  • 我是新来的数据。我试图找到解决方案的服务器端处理,因为过去两天,但没有找到解决方案。 我的JS代码是 可数据以表格式呈现JSON。但是排序、分页和搜索操作是行不通的。无论我从下拉列表中选择了多少值,它都会在第一页显示所有结果 在底部,它还显示了像“显示0到0的0个条目(从NaN总条目中过滤)”这样的消息 如果我通过服务器端:false。一切正常。但我希望服务器端处理相同的数据 任何帮助都将不胜感激

  • 我是android开发的新手。我正在开发一个简单的应用程序,其中包含1个活动,1个广播接收器和1个通知服务。有些警报设置在活动中,我想在设备启动/重启时重新设置它们。 下面是我在MainActivity中的代码: 我从活动中设置的警报中正确地获取所有通知。但获取NullpointerException 这是我从意图中读到额外内容的地方。我错过了什么?任何帮助都很感激。 附言。所有必要的权限都在清单

  • 我有以下电话: 和以下路线: 我试图在'//get Hello'上获取名为'Hello'的参数。我尝试使用req.params,但它返回{}。 调用是否有问题,或者如何获取hello参数?提前谢谢

  • 我试图使Angularjs服务器端分页在这个链接https://l-lin.github.io/angular-datatables/#/serverSideProcessing 所以我用这个暗号 我在dataSrc参数中手动添加了recordsTotal、recordsFiltered和row 这是添加recordsTotal、recordsFiltered和row之前和之后的json数据 添加

  • 我正在使用org.springframework.data.jpa.repository.JpaRepository保存方法来保存一个实体。我认为保存方法来自CrudRepository接口。我从服务类调用这个保存方法。 但问题是< code > DataIntegrityViolationException 在catch块中没有被捕获。相反,我在下面的日志中看到。 和 我甚至尝试捕获Constr

  • 问题内容: 我的MySQL连接字符串是: 我的问题是: 我应该写什么查询来获取存在的数据库名称? 我应该写什么查询来获取服务器版本? 我的错误是因为我的连接字符串以“。 ”而不是“。”结尾。 问题答案: 使用的数据库。