Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
348 views
in Technique[技术] by (71.8m points)

javascript - Limit of displaying rows in DataTables

I use Datatables 2.0. How can I set the limit of display number of rows?

I have 1000 rows that are all showing on one page. At Datatables 1.9 it was going with 'iDisplaylenght'. But in Datatable 2.0 I've tried to set 'lenght:10' like in this documentation: http://next.datatables.net/manual/server-side but it doesn't work.

EDIT: enter image description here enter image description here enter image description here enter image description here

on this link above you can see my problem. It doesn't matter which number I choose from the checkbox.

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

This is my server code.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

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

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

See a working demo here: http://jsfiddle.net/vf5wA/2/

EDIT:

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.

E.g.:

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

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...