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!

实现方法:1、用“$("img").delay(毫秒数).fadeOut()”语句,delay()设置延迟秒数;2、用“setTimeout(function(){ $("img").hide(); },毫秒值);”语句,通过定时器来延迟。

区别:1、axios是一个异步请求框架,用于封装底层的XMLHttpRequest,而jquery是一个JavaScript库,只是顺便封装了dom操作;2、axios是基于承诺对象的,可以用承诺对象中的方法,而jquery不基于承诺对象。

修改方法:1、用css()设置新样式,语法“$(元素).css("min-height","新值")”;2、用attr(),通过设置style属性来添加新样式,语法“$(元素).attr("style","min-height:新值")”。

增加元素的方法:1、用append(),语法“$("body").append(新元素)”,可向body内部的末尾处增加元素;2、用prepend(),语法“$("body").prepend(新元素)”,可向body内部的开始处增加元素。

在Web开发中,数据表格是我们经常需要用到的组件。而分页,则是在处理大量数据时常见的需求之一。如果要实现一个大量数据的分页数据表格,很多人可能会选择使用前端框架比如Vue、React或者Angular。然而,使用后端语言PHP和前端插件DataTables也可以轻松创建分页数据表格,并且更加灵活和可定制性更高。下面我们就来看一下如何利用PHP和DataTab

删除方法:1、用empty(),语法“$("div").empty();”,可删除所有子节点和内容;2、用children()和remove(),语法“$("div").children().remove();”,只删除子元素,不删除内容。

on()方法有4个参数:1、第一个参数不可省略,规定要从被选元素添加的一个或多个事件或命名空间;2、第二个参数可省略,规定元素的事件处理程序;3、第三个参数可省略,规定传递到函数的额外数据;4、第四个参数可省略,规定当事件发生时运行的函数。

去掉方法:1、用“$(selector).removeAttr("readonly")”语句删除readonly属性;2、用“$(selector).attr("readonly",false)”将readonly属性的值设置为false。


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

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

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
