Sharing of jQuery plug-in DataTables paging development technology
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 '<input type="checkbox"/>'; return data; } }, { "data": "field1" }, { "data": "field2" , //此列名要与服务端返回的JSON串中的值一致 "render": function ( data, type, full, meta ) { return '<p style="text-align:left">'+data+'</p>'; } }, //其余字段定义省略 ] });
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": [{}] //获取到的记录集合 }
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:
$('#dtList').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, //开启服务端请求模式 'pagingType': 'full_numbers', "retrieve":true, "ajax": { //这边就不需要dataSrc属性来处理值了 "type": "GET", "url": "{% url 'scheme_list_json' %}", "data":{ 'sSearch':'', 'csrfmiddlewaretoken': '{{ csrf_token }}'}, }, "columns": [ //表格要显示的列定义 { "data": 0, "visible":false, "render": function ( data, type, full, meta ) { //return '<input type="checkbox"/>'; return data; } }, { "data": 1 "bSortable":false }, { "data": 2, "bSortable":false, //此字段不排序 "render": function ( data, type, full, meta ) { return '<p style="text-align:left">'+data+'</p>'; } }, //其余字段定义省略 ...... ], "aaSorting": [ //指定排序的列(索引从0开始)及规则 [ 8, "asc" ], [ 9, "asc" ], [ 10, "desc" ] ], "drawCallback": function( settings ) { //绘制表格时的回调函数 $("th").removeClass("sorting_asc"); //删除排序图标,升序样式 $("th").removeClass("sorting_desc");//降序样式 });
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 = ['jyjhbid', 'tzbd', 'clsc', 'clzt', 'jlscrq', 'jlxgrq', 'clcz'] # 排序 order_columns = ['jyjhbid', 'tzbd', 'clsc', 'clzt', 'jlscrq', 'jlxgrq', 'clcz']
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
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s

jQuery is a popular JavaScript library widely used in web development. During web development, it is often necessary to dynamically add new rows to tables through JavaScript. This article will introduce how to use jQuery to add new rows to a table, and provide specific code examples. First, we need to introduce the jQuery library into the HTML page. The jQuery library can be introduced in the tag through the following code:
