如何在jquery数据表中查找行长度

时间:2017-09-02 07:05:44

标签: jquery datatables

我试图在数据表加载时获得行长度。但它没有达到长度。

$("button#searchbutton").click(function() {
                          var txnid = $("#transactionid").val();
                          var oTable = $('#datatable').DataTable({
                              "ajax" : "/doTxnSearchList?txnid="+txnid,
                              "bDestroy":true,
                                "columns":[
                                    {"data": "txnid" },
                                    {"data": "fromDate"},                                        
                                    {"data": "amount"},                                        
                                    {"data": "mobileno"}
                                   ],
                                   "order": [[ 1, "desc" ]],
                                           "language": {
                                               "lengthMenu": "| View _MENU_ records per page",
                                               "zeroRecords": "Nothing found - sorry",
                                               "infoEmpty": "No records available",
                                               "infoFiltered": "(filtered from _MAX_ total records)"
                                           },

                                           "pagingType": "full_numbers",
                                          "lengthChange": false,
                                       //   "sScrollY": "250px",
                                          "bAutoWidth": false,
                                         // "bScrollCollapse": true,
                                          "fnInitComplete": function() {
                                            this.css("visibility", "visible");
                                          },
                          });                           
                          alert(oTable.fnGetData(this).length);
                      });

我正在尝试在加载表格后在警报alert(oTable.fnGetData(this).length);中获取长度。

我的代码有什么问题吗?

2 个答案:

答案 0 :(得分:0)

使用initComplete api从ajax调用中查找数据长度

在您的代码中

$("button#searchbutton").click(function() {
                      var txnid = $("#transactionid").val();
                      var oTable = $('#datatable').DataTable({
                          "ajax" : "/doTxnSearchList?txnid="+txnid,
                          "bDestroy":true,
                            "columns":[
                                {"data": "txnid" },
                                {"data": "fromDate"},                                        
                                {"data": "amount"},                                        
                                {"data": "mobileno"}
                               ],
                               "order": [[ 1, "desc" ]],
                                       "language": {
                                           "lengthMenu": "| View _MENU_ records per page",
                                           "zeroRecords": "Nothing found - sorry",
                                           "infoEmpty": "No records available",
                                           "infoFiltered": "(filtered from _MAX_ total records)"
                                       },

                                       "pagingType": "full_numbers",
                                      "lengthChange": false,
                                   //   "sScrollY": "250px",
                                      "bAutoWidth": false,
                                     // "bScrollCollapse": true,
                                      "fnInitComplete": function() {
                                        this.css("visibility", "visible");
                                      },
                                      "initComplete":function(settings,json)
                                       {
                                        console.log(json.length);
                                       }

                      });                           
                   });

答案 1 :(得分:0)

注意:您尝试使用较旧的API方法访问1.10 API。正如您所见,fnGetData已被弃用。

以下是有关API的详细信息:https://datatables.net/upgrade/1.10-convert

解决方案oTable.rows().count()您已使用此API。

alert(oTable.fnGetData(this).length); //replace this code with the following code 

alert(oTable.rows().count());  //it will number rows into your datatable

工作示例:http://jsfiddle.net/dipakthoke07/xwjga044/175/

参考:https://datatables.net/reference/api/count()

希望这会对你有所帮助。