在DataTables中显示行的限制 [英] Limit of displaying rows in DataTables

查看:107
本文介绍了在DataTables中显示行的限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Datatables 2.0。
如何设置显示行数限制。
我有1000行,所有都在一页上显示。
在Datatables 1.9上,它与'iDisplaylenght'一致。
但是在Datatable 2.0中我试图设置'lenght:10'就像在这个文件中一样: http://next.datatables.net/manual/server-side
但它不起作用。

I use Datatables 2.0. How can i set the limit of display number of rows. I have 1000 rows and all is showing on one page. At Datatables 1.9 it was going with 'iDisplaylenght'. But in Datatable 2.0 i´v tried to set 'lenght:10' like in this docu: http://next.datatables.net/manual/server-side but it dosen´t work.

非常感谢!

编辑:

http://img4web.com/g/GC8P7

在上面这个链接上你可以看到我的问题。
不管我从复选框中选择哪个号码都没关系。

on this link above u can see my problem. It dosen´t matter which number i choose from the checkbox.

我发现了@Julian的链接(谢谢),如果我不要使用ajax调用,它工作正常。但是如果我从服务器端获取我的数据,那么我的所有数据都会显示出来。请参阅链接。所以它必须是服务器端的问题?!​​

I have found out, with the link from @Julian(thanks), that if i don´t use the ajax call, it works fine. But if i get my data from the server-side all of my datas are showing. See the link. So it must be a problem on the server-side?!

@SuppressWarnings("unchecked")
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String sql = "SELECT UID,LastName,FirstName FROM employe LIMIT 1000";

    int rows=0;
    int count = 0;
    JSONArray arrayback = new JSONArray();
    JSONObject object = new JSONObject();
    PrintWriter out = response.getWriter();

    try {

        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/check","****","****");
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        ResultSetMetaData meta = rs.getMetaData();
        rows= meta.getColumnCount();        

        while(rs.next())
        {
            JSONArray array = new JSONArray();
            array.add(rs.getString("UID"));
            array.add(rs.getString("LastName"));
            array.add(rs.getString("FirstName"));
            count++;
            arrayback.add(array);

        }

        object.put("aaData", arrayback);
        object.put("sEcho",1);
        object.put("iTotalRecords", count);
        object.put("iTotalDisplayRecords", count);      
        out.print(object);          


        con.close();
}catch(exception e)
{
e.printStackTrace();
    }
}

这是我的服务器代码。

推荐答案

您是否正在为数据表使用服务器端处理?因为您指向docs的链接指向Datatables的服务器端配置。客户端可以通过经典数据表方法轻松呈现Imho 1000行。您链接页面上指出的选项也是服务器端脚本处理表的正确数据。这意味着, length 值告诉服务器(!)他必须返回多少行。因此,您的页面与数据表的客户端设置无关。

Are you using server-side processing for your datatable? Because your link to the docs points to the server-side configuration for Datatables. Imho 1000 rows should be easily rendered by the client via "classic" Datatable-methods. Also the options pointed out on your linked page are for the server-side script to process the correct data for the table. That means, the length value tells the server(!) how many rows he has to return. So your page have nothing to do with the "client-side" setup of the datatable.

因此,要调整客户端上显示的行数使用 pageLength 值:

So, to adjust the displayed number of rows on the "client-side" use the pageLength value:

$(document).ready(function() {
    $('#example').dataTable( {
        "pageLength": 20 
    } );
} );

您还可以调整 lengthMenu 进行设置下拉选择,用户可以在其中指定每页要查看的行数:

You can also adjust the lengthMenu to setup the dropdown-selection where the user can specify how much rows he wants to see per page:

$(document).ready(function() {
    $('#example').dataTable( {
        "lengthMenu": [20, 40, 60, 80, 100],
        "pageLength": 20
    } );
} );

在此查看工作演示: http://jsfiddle.net/vf5wA/2/

编辑:

我认为您的问题是,您要按照每个请求返回完整的项目集。据我所知,Datatables的工作方式略有不同。在SQL语句中,您将LIMIT设置为1000,这意味着您的api将返回1000行。但是Datatable为您提供了一个参数iDisplayLength,它应该通过AJAX-Call传递给您的api。然后,iDisplayLength中指定的项目数将为您的SQL查询设置LIMIT。

I think your problem is, that you are returning the full set of items per each single request. As far as I know, Datatables works a little bit different. On your SQL-Statement you are setting a LIMIT to 1000, that means that 1000 rows will be returned by your api. But the Datatable gives you an parameter "iDisplayLength" which should be passed to your api via AJAX-Call. The number of items specified in "iDisplayLength" will then set the LIMIT for your SQL-Query.

例如:

AJAX-Call: https://api.example.com/datatable/something?iDisplayLength=50

在您的服务器上,iDisplayLength - 参数将设置LIMIT进行查询:

And on your server, the "iDisplayLength"-Parameter will set the LIMIT for querying:

String sql = "SELECT UID,LastName,FirstName FROM employe LIMIT {iDisplayLength}";
object.put("iTotalRecords", {iDisplayLength});

我会尝试,但我从来没有对Datatables使用过服务器端处理,所以这只是在黑暗中拍摄。

I would try that, but I have never used server-side processing for Datatables, so this is just a shot in the dark.

这篇关于在DataTables中显示行的限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆