Home > Web Front-end > JS Tutorial > body text

Sharing of jQuery plug-in DataTables paging development technology

小云云
Release: 2017-12-30 14:41:22
Original
1959 people have browsed it

This article mainly shares with you the experience of developing jQuery plug-in DataTables paging. It has certain reference value. Interested friends can refer to it. I hope it can help everyone.

Purpose of writing a blog: not for popularity, just to keep notes; it doesn’t matter if it’s long-winded, I’m afraid there are omissions and I won’t be able to remember them later.

Development environment: Python 3.6.0, Anaconda 4.3.1, Django, JetBrains PyCharm 2017.1.5

Simply organized according to the development process of my project.

1. The first version, no paging.

I have used DataTables before (more Datagrid in easyui, and the previous server was PHP), so I thought there is not much difference between the front-end/client, and the server is the same anyway. Return a JSON string according to the standard format (Django’s code will be sorted out later). In addition, I didn’t think about table paging at the beginning. I just had three field sorting requirements (the sorting has been done and returned on the server side).
DataTables basic code:


//表格的HTML代码略过
$('#dtList').DataTable({
        "bPaginate": false, //翻页功能
        "bInfo": false,//页脚信息
        "ordering": false, //不排序
        "searching": false,  //搜索框,不显示
        "bLengthChange": false, //改变每页显示数据数量,不显示
        "iDisplayLength": 10,  //每页默认显示数量(不显示了,这个设置也起不了作用)
        "serverSide":true, //服务端
        "retrieve":false, //意思是如果已经初始化了,则继续使用之前的Datatables实例。

        "ajax": {
          "type": "POST",
          "url": "/manage/getlist/",
          "data":{'csrfmiddlewaretoken': '{{ csrf_token }}'}, //Django的token值
          "dataSrc": function (result) { //使用dataSrc属性来设置获取到的数据格式,其值是服务端拼好的key-value(数据字段名称-字段值)【服务端走了弯路,后续有时间再写文章说明】
            var json = JSON.parse(result).data;
            return json;
          }
        },
        "columns": [ //表格要显示的列定义(字段名称做了处理)
          { "data": "field0",
            "visible":false,
            "render": function ( data, type, full, meta ) {
              //return &#39;<input type="checkbox"/>&#39;;
              return data;
            }
          },
          { "data": "field1" },
          { "data": "field2" , //此列名要与服务端返回的JSON串中的值一致
            "render": function ( data, type, full, meta ) {
              return &#39;<p style="text-align:left">&#39;+data+&#39;</p>&#39;;
            }
          },
          //其余字段定义省略
          ]
        });
Copy after login

2. Add paging function

After writing the first program for a few days, I was ready to add paging. Function.
DataTables JSON string format requirements:


{
  "draw": n, 
  "recordsTotal": n, //总记录数
  "recordsFiltered": n, //条件过滤后的记录数,与总记录数可能会不同
  "data": [{}] //获取到的记录集合
}
Copy after login

According to the previous experience in processing the Datagrid component in easyui, get the total number of records, the number of filtered records, and other values. Just send back the JSON string, and the client can directly implement paging.

But, here comes the question: How to control the value of draw?

After checking the information and tracking the browser header, I found out that draw is an attribute value of DataTables itself. It will be transmitted to the server every time the data is obtained. There is no need to change this value, just directly Just return it in a JSON string. I tried it, but I don’t know what the problem is. It seems that paging is not working, and I always feel that this control method is a bit tiring.

Change the idea and check the information again, that is, server-side paging (requesting corresponding data on demand), there is really: django-datatables-view (datatables for django, https://pypi.python.org/ pypi/django-datatables-view), just follow the installation steps. In this way, you don’t have to worry about how to spell the JSON string values. This component will be ready.

Improved front-end code:


$(&#39;#dtList&#39;).DataTable({
    "bPaginate": true, //翻页功能
    "bInfo": true,//页脚信息
    "ordering":true, //开启排序
    "searching": false, //搜索框,不显示
    "bLengthChange": true, //改变每页显示数据数量,不显示
    "aLengthMenu": [[5, 10, 25, 50, -1], [5, 10, 25, 50, "All"]], //每页显示记录菜单选项
    "iDisplayLength": 10, //每页默认显示数量
    "serverSide":true, //开启服务端请求模式
    &#39;pagingType&#39;: &#39;full_numbers&#39;,
    "retrieve":true,

    "ajax": { //这边就不需要dataSrc属性来处理值了
     "type": "GET",
     "url": "{% url &#39;scheme_list_json&#39; %}",
     "data":{
      &#39;sSearch&#39;:&#39;&#39;,
      &#39;csrfmiddlewaretoken&#39;: &#39;{{ csrf_token }}&#39;},
     },
    "columns": [ //表格要显示的列定义
     { "data": 0,
      "visible":false,
      "render": function ( data, type, full, meta ) {
       //return &#39;<input type="checkbox"/>&#39;;
       return data;
      }
     },
     { "data": 1
     "bSortable":false
     },
     { "data": 2,
      "bSortable":false, //此字段不排序
      "render": function ( data, type, full, meta ) {
       return &#39;<p style="text-align:left">&#39;+data+&#39;</p>&#39;;
      }
     },
     //其余字段定义省略
     ......
     ],
    "aaSorting": [ //指定排序的列(索引从0开始)及规则
     [ 8, "asc" ],
     [ 9, "asc" ],
     [ 10, "desc" ]
     ],
    "drawCallback": function( settings ) { //绘制表格时的回调函数
     $("th").removeClass("sorting_asc"); //删除排序图标,升序样式
     $("th").removeClass("sorting_desc");//降序样式 
   });
Copy after login

The sorting is successful, but a problem arises: after turning on the sorting function, there will be Sorting icon, but we don’t need it (just sorting, no clicking), so add the drawCallback callback function in the above code and remove its style.
PS:
There is a pitfall - the number of columns of the server's return field and the sorting field must correspond one to one, and the front-end DataTables display field column definitions must also be in this order, otherwise the front-end sorting will be chaotic, such as :


 # 需要显示的字段
 columns = [&#39;jyjhbid&#39;, &#39;tzbd&#39;, &#39;clsc&#39;, &#39;clzt&#39;, &#39;jlscrq&#39;, &#39;jlxgrq&#39;, &#39;clcz&#39;] 

 # 排序
 order_columns = [&#39;jyjhbid&#39;, &#39;tzbd&#39;, &#39;clsc&#39;, &#39;clzt&#39;, &#39;jlscrq&#39;, &#39;jlxgrq&#39;, &#39;clcz&#39;]
Copy after login

I am new to django-datatables-view and thought that the two collection values ​​​​can be different, so. . . . Walked more miles.

Related recommendations:

Solution to jQuery Datatables header misalignment

What to do if Datatables header misalignment

Introduction to jquery plug-in datatables attributes and detailed explanation of creating paging and sorting examples

The above is the detailed content of Sharing of jQuery plug-in DataTables paging development technology. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template