Skip to content

Latest commit

 

History

History
1023 lines (1002 loc) · 27.4 KB

jQueryDatatables1.10.0.md

File metadata and controls

1023 lines (1002 loc) · 27.4 KB
title date categories tags
jQueryDatatables1.10.0使用说明
2017-06-08 04:02:40 -0700
前端
jQuery
前端开发插件
javascript

<Excerpt in index | 首页摘要> jQueryDatatables1.10.0使用说明

<The rest of contents | 余下全文>


1. 介绍

Datatables是一款jquery表格插件。它是一个高度灵活的工具,可以将任何HTML表格添加高级的交互功能。

  • 分页,即时搜索和排序
  • 几乎支持任何数据源:DOM, javascript, Ajax 和 服务器处理
  • 支持不同主题 DataTables, jQuery UI, Bootstrap, Foundation
  • 各式各样的扩展: Editor, TableTools, FixedColumns ……
  • 丰富多样的option和强大的API
  • 支持国际化
  • 超过2900+个单元测试
  • 免费开源 ( MIT license )! 商业支持
  • 更多特性请到官网查看

官网:https://www.datatables.net/ 英文官网GitHub:https://github.com/DataTables/DataTables 中文网:http://www.datatables.club/ 中文网GitHub:https://github.com/ssy341/datatables-cn

**特别说明:**实际项目开发中使用的是1.10.0版本,写这个总结的时候,官网已经更新到了1.10.15

2. 使用方法

基于jQuery的,所以首先需要引入jQuery,然后在引入datatable的js和css

<script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.js"></script>
<script type="text/javascript" src="jquery.dataTables.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.15/css/jquery.dataTables.min.css" >

一个简单的demo1.html,注意涉及到ajax请求,涉及到同源策略了,所以要在本地服务器上跑,效果如图:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.js"></script>
	<script type="text/javascript" src="jquery.dataTables.min.js"></script>
	<link rel="stylesheet" href="https://cdn.datatables.net/1.10.15/css/jquery.dataTables.min.css" >
	<style>
		.main{
			width:1100px;
			margin:100px auto;
		}
		td{
			text-align: center;
		}
	</style>
	<title></title>
</head>
<body>
	<div class="main">
		<table cellpadding="0" cellspacing="0" id="example">
			<thead>
			  <tr>
			    <th>Rendering engine</th>
			    <th>Browser</th>
			    <th>Platform(s)</th>
			    <th>Engine version</th>
			    <th>CSS grade</th>
			  </tr>
			</thead>
		    <tbody></tbody>
		</table>
	</div>
</body>
<script type="text/javascript">
	$(document).ready(function() {
		$('#example').dataTable( {
			"bProcessing": true,//当datatable获取数据时候是否显示正在处理提示信息。
			"sAjaxSource": 'arrays.txt'//指定要从哪个URL获取数据
		} );
	} );
</script>
</html>

arrays.txt文件

{
  "aaData": [
    [
      "Trident",
      "Internet Explorer 4.0",
      "Win 95+",
      "4",
      "X"
    ],
    [
      "Trident",
      "Internet Explorer 5.0",
      "Win 95+",
      "5",
      "C"
    ],
    [
      "Trident",
      "Internet Explorer 5.5",
      "Win 95+",
      "5.5",
      "A"
    ],
    [
      "Trident",
      "Internet Explorer 6",
      "Win 98+",
      "6",
      "A"
    ],
    [
      "Trident",
      "Internet Explorer 7",
      "Win XP SP2+",
      "7",
      "A"
    ],
    [
      "Trident",
      "AOL browser (AOL desktop)",
      "Win XP",
      "6",
      "A"
    ],
    [
      "Gecko",
      "Firefox 1.0",
      "Win 98+ / OSX.2+",
      "1.7",
      "A"
    ],
    [
      "Gecko",
      "Firefox 1.5",
      "Win 98+ / OSX.2+",
      "1.8",
      "A"
    ],
    [
      "Gecko",
      "Firefox 2.0",
      "Win 98+ / OSX.2+",
      "1.8",
      "A"
    ],
    [
      "Gecko",
      "Firefox 3.0",
      "Win 2k+ / OSX.3+",
      "1.9",
      "A"
    ],
    [
      "Gecko",
      "Camino 1.0",
      "OSX.2+",
      "1.8",
      "A"
    ],
    [
      "Gecko",
      "Camino 1.5",
      "OSX.3+",
      "1.8",
      "A"
    ],
    [
      "Gecko",
      "Netscape 7.2",
      "Win 95+ / Mac OS 8.6-9.2",
      "1.7",
      "A"
    ],
    [
      "Gecko",
      "Netscape Browser 8",
      "Win 98SE+",
      "1.7",
      "A"
    ],
    [
      "Gecko",
      "Netscape Navigator 9",
      "Win 98+ / OSX.2+",
      "1.8",
      "A"
    ],
    [
      "Gecko",
      "Mozilla 1.0",
      "Win 95+ / OSX.1+",
      1,
      "A"
    ],
    [
      "Gecko",
      "Mozilla 1.1",
      "Win 95+ / OSX.1+",
      1.1,
      "A"
    ],
    [
      "Gecko",
      "Mozilla 1.2",
      "Win 95+ / OSX.1+",
      1.2,
      "A"
    ],
    [
      "Gecko",
      "Mozilla 1.3",
      "Win 95+ / OSX.1+",
      1.3,
      "A"
    ],
    [
      "Gecko",
      "Mozilla 1.4",
      "Win 95+ / OSX.1+",
      1.4,
      "A"
    ],
    [
      "Gecko",
      "Mozilla 1.5",
      "Win 95+ / OSX.1+",
      1.5,
      "A"
    ],
    [
      "Gecko",
      "Mozilla 1.6",
      "Win 95+ / OSX.1+",
      1.6,
      "A"
    ],
    [
      "Gecko",
      "Mozilla 1.7",
      "Win 98+ / OSX.1+",
      1.7,
      "A"
    ],
    [
      "Gecko",
      "Mozilla 1.8",
      "Win 98+ / OSX.1+",
      1.8,
      "A"
    ],
    [
      "Gecko",
      "Seamonkey 1.1",
      "Win 98+ / OSX.2+",
      "1.8",
      "A"
    ],
    [
      "Gecko",
      "Epiphany 2.20",
      "Gnome",
      "1.8",
      "A"
    ],
    [
      "Webkit",
      "Safari 1.2",
      "OSX.3",
      "125.5",
      "A"
    ],
    [
      "Webkit",
      "Safari 1.3",
      "OSX.3",
      "312.8",
      "A"
    ],
    [
      "Webkit",
      "Safari 2.0",
      "OSX.4+",
      "419.3",
      "A"
    ],
    [
      "Webkit",
      "Safari 3.0",
      "OSX.4+",
      "522.1",
      "A"
    ],
    [
      "Webkit",
      "OmniWeb 5.5",
      "OSX.4+",
      "420",
      "A"
    ],
    [
      "Webkit",
      "iPod Touch / iPhone",
      "iPod",
      "420.1",
      "A"
    ],
    [
      "Webkit",
      "S60",
      "S60",
      "413",
      "A"
    ],
    [
      "Presto",
      "Opera 7.0",
      "Win 95+ / OSX.1+",
      "-",
      "A"
    ],
    [
      "Presto",
      "Opera 7.5",
      "Win 95+ / OSX.2+",
      "-",
      "A"
    ],
    [
      "Presto",
      "Opera 8.0",
      "Win 95+ / OSX.2+",
      "-",
      "A"
    ],
    [
      "Presto",
      "Opera 8.5",
      "Win 95+ / OSX.2+",
      "-",
      "A"
    ],
    [
      "Presto",
      "Opera 9.0",
      "Win 95+ / OSX.3+",
      "-",
      "A"
    ],
    [
      "Presto",
      "Opera 9.2",
      "Win 88+ / OSX.3+",
      "-",
      "A"
    ],
    [
      "Presto",
      "Opera 9.5",
      "Win 88+ / OSX.3+",
      "-",
      "A"
    ],
    [
      "Presto",
      "Opera for Wii",
      "Wii",
      "-",
      "A"
    ],
    [
      "Presto",
      "Nokia N800",
      "N800",
      "-",
      "A"
    ],
    [
      "Presto",
      "Nintendo DS browser",
      "Nintendo DS",
      "8.5",
      "C/A<sup>1</sup>"
    ],
    [
      "KHTML",
      "Konqureror 3.1",
      "KDE 3.1",
      "3.1",
      "C"
    ],
    [
      "KHTML",
      "Konqureror 3.3",
      "KDE 3.3",
      "3.3",
      "A"
    ],
    [
      "KHTML",
      "Konqureror 3.5",
      "KDE 3.5",
      "3.5",
      "A"
    ],
    [
      "Tasman",
      "Internet Explorer 4.5",
      "Mac OS 8-9",
      "-",
      "X"
    ],
    [
      "Tasman",
      "Internet Explorer 5.1",
      "Mac OS 7.6-9",
      "1",
      "C"
    ],
    [
      "Tasman",
      "Internet Explorer 5.2",
      "Mac OS 8-X",
      "1",
      "C"
    ],
    [
      "Misc",
      "NetFront 3.1",
      "Embedded devices",
      "-",
      "C"
    ],
    [
      "Misc",
      "NetFront 3.4",
      "Embedded devices",
      "-",
      "A"
    ],
    [
      "Misc",
      "Dillo 0.8",
      "Embedded devices",
      "-",
      "X"
    ],
    [
      "Misc",
      "Links",
      "Text only",
      "-",
      "X"
    ],
    [
      "Misc",
      "Lynx",
      "Text only",
      "-",
      "X"
    ],
    [
      "Misc",
      "IE Mobile",
      "Windows Mobile 6",
      "-",
      "C"
    ],
    [
      "Misc",
      "PSP browser",
      "PSP",
      "-",
      "C"
    ],
    [
      "Other browsers",
      "All others",
      "-",
      "-",
      "U"
    ]
  ]
}

3. 部分API

使用的时候,部分属性

属性名称

取值局限

申明

bAutoWidth

true or false, default true

是否主动策画表格各列宽度

bDeferRender

true or false, default false

用于衬着的一个参数

bFilter

true or false, default true

开关,是否启用客户端过滤功能

bInfo

true or false, default true

开关,是否显示表格的一些信息

bJQueryUI

true or false, default false

是否应用jquery ui themeroller的风格

bLengthChange

true or false, default true

开关,是否显示一个每页长度的选择条(须要分页器支撑)

bPaginate

true or false, default true

开关,是否显示(应用)分页器

bProcessing

true or false, defualt false

当datatable获取数据时候是否显示正在处理提示信息。

bScrollInfinite

true or false, default false

开关,以指定是否无穷迁移转变(与sScrollY共同应用),在大数据量的时辰很有效。当这个标记为true的时辰,分页器就默认封闭

bSort

true or false, default true

开关,是否让各列具有按列排序功能

bSortClasses

true or false, default true

开关,指定当当前列在排序时,是否增长classes ""sorting_1"", ""sorting_2"" and ""sorting_3"",打开后,在处理惩罚大数据时,机能有所丧失

bStateSave

true or false, default false

开关,是否打开客户端状况记录功能。这个数据是记录在cookies中的,打开了这个记录后,即使刷新一次页面,或从头打开浏览器,之前的状况都是保存下来的

sScrollX

""disabled"" or? ""100%""?类似的字符串

是否开启程度迁移转变,以及指定迁移转变区域大小

sScrollY

""disabled"" or ""200px""?类似的字符串

是否开启垂直迁移转变,以及指定迁移转变区域大小

--

--

--

选项

?

?

aaSorting

array array[int,string],如[], [[0,""asc""], [0,""desc""]]

指定按多列数据排序的根据

aaSortingFixed

同上

同上。独一不合点是不克不及被用户的自定义设备冲突

aLengthMenu

default [10, 25, 50, 100],可认为一维数组,也可为二维数组,比如:[[10, 25, 50, -1], [10, 25, 50, "All"]]

这个为选择每页的条目数,当应用一个二维数组时,二维层面只能有两个元素,第一个为显示每页条目数的选项,第二个是关于这些选项的申明

aoSearchCols

default null,?类似:[null, {"sSearch": "My filter"}, null,{"sSearch": "^[0-9]", "bEscapeRegex": false}]

给每个列零丁定义其初始化搜刮列表特点(这一块还没搞懂)

asStripClasses

default [""odd"", ""even""],?比如[""strip1"", ""strip2"", ""strip3""]

指定要被应用到各行的class风格,会主动轮回

bDestroy

true or false, default false

用于当要在同一个元素上履行新的dataTable绑按时,将之前的那个数据对象清除掉,换以新的对象设置

bRetrieve

true or false, default false

用于指明当履行dataTable绑按时,是否返回DataTable对象

bScrollCollapse

true or false, default false

指定恰当的时辰缩起迁移转变视图

bSortCellsTop

true or false, default false

(未知的东东)

iCookieDuration

整数,默认7200,单位为秒

指定用于存储客户端信息到cookie中的时候长度,跨越这个时候后,主动过期

iDeferLoading

整数,默认为null

延迟加载,它的参数为要加载条目标数量,凡是与bServerSide,sAjaxSource等共同应用

iDisplayLength

整数,默认为10

用于指定一屏显示的条数,需开启分页器

iDisplayStart

整数,默认为0

用于指定从哪一条数据开端显示到表格中去

iScrollLoadGap

整数,默认为100

用于指定当DataTable设置为迁移转变时,最多可以一屏显示几许条数据

oSearch

默认{ "sSearch": "", "bRegex": false, "bSmart": true }

又是初始时指定搜刮参数相干的,有点错杂,没搞懂今朝

sAjaxDataProp

字符串,default ""aaData""

指定当从办事端获取表格数据时,数据项应用的名字

sAjaxSource

URL字符串,default null

指定要从哪个URL获取数据

sCookiePrefix

字符串,default ""SpryMedia_DataTables_""

当打开状况存储特点后,用于指定存储在cookies中的字符串的前缀名字

sDom

default lfrtip (when bJQueryUI is false) or <"H"lfr>t<"F"ip> (when bJQueryUI is true)

这是用于定义DataTable布局的一个强大的属性,另开专门文档来补充申明吧

sPaginationType

""full_numbers"" or ""two_button"", default ""two_button""

用于指定分页器风格

sScrollXInner

string default ""disabled""

又是程度迁移转变相干的,没搞懂啥意思


4.一个复杂的例子

5.实现表格内容超出隐藏(有坑)

最终效果:

5.1css样式

我们知道,使用css样式就可以实现超出内容隐藏并且用省略号代替,如下

    table {
        table-layout:fixed;
    }
    .hiddenMax {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

解释说明:

  • table-layout: fixed 由于table-layout的默认值是auto,即table的宽高将取决于其内容的多寡,如果内容的体积无法估测,那么最终表格的呈现形式也无法保证了,fixed一下就好了。(注意:此样式是关键)。实测,如果不加这个样式,整体会乱掉!!!

  • white-space: nowrap 是为了保证无论单元格(TD)中文本内容有多少, 都不会自动换行,此时多余的内容会在水平方向撑破单元格。

  • overflow: hidden 隐藏超出单元格的部分

  • text-overflow: ellipsis 将被隐藏的那部分用省略号代替

5.2给谁用?

在实际应用的时候,我发现把hiddenMax直接给td用不好使,甚至导致整个表格错乱。后来查了查,我直接将返回的内容嵌入到了一个div里面,贴一部分js代码,最后面的那个16就是

"columnDefs": [{
    "targets": [4], // 目标列位置,下标从0开始
    "render": function (data, type, full) { // 返回自定义内容                   
        //return "<font style='color:blue;'>列入计划</font>";
        if (data == 0) {
            return emanager ? "<a href='javascript:editcallrepair(" + full.id + ")'><i class=\"Hui-iconfont\">&#xe6df;待审核</i></a>" : "待审核";
        }
        else if (data == 1) {
            return emanager ? "<a href='javascript:editcallrepair(" + full.id + ")'><i class=\"Hui-iconfont\">&#xe6df;审核不通过</i></a>" : "审核不通过";
        }
        else if(data ==2) {
            return emanager ? "<a href='javascript:editcallrepair(" + full.id + ")'><i class=\"Hui-iconfont\">&#xe6df;暂不处理</i></a>" : "暂不处理";
        }
        else if (data == 3) {
            return emanager ? "<a href='javascript:editcallrepair(" + full.id + ")'><i class=\"Hui-iconfont\">&#xe6df;立刻维修</i></a>" : "立刻维修";
        }
        else if (data == 4) {
            return emanager ? "<a href='javascript:editcallrepair(" + full.id + ")'><i class=\"Hui-iconfont\">&#xe6df;列入计划</i></a>" : "列入计划";
        }
        else{
            return emanager ? "<a href='javascript:editcallrepair(" + full.id + ")'><i class=\"Hui-iconfont\">&#xe6df;维修完成</i></a>" : "维修完成";
        }
    }
},{
    "targets": [16], // 目标列位置,下标从0开始
    "render": function (data, type, full) { // 返回自定义内容
        return "<div class=\"hiddenMax\">" + data + "</div>";
    }
}],
5.3上面5.2的方法太复杂了,实现超出隐藏

只需要在表格初始化的时候加一个样式类就可以了

"aoColumns": [
           { "mData": "id", "sClass": "text-c" },
           { "mData": "id", "sClass": "text-c" },
           { "mData": "recordnumber", "sClass": "text-c" },
           { "mData": "recordstate", "sClass": "text-c" },
           { "mData": "opinionstate", "sClass": "text-c" },
           { "mData": "wxrecordnumber", "sClass": "text-c" },
           { "mData": "serialnumber", "sClass": "text-c" },
           { "mData": "devicename", "sClass": "text-c" },
           { "mData": "modelname", "sClass": "text-c" },
           { "mData": "deptname", "sClass": "text-c" },
           { "mData": "occurredtime", "sClass": "text-c" },
           { "mData": "recordtime", "sClass": "text-c" },                       
           { "mData": "faultlevel", "sClass": "text-c" },
           { "mData": "faulttype", "sClass": "text-c" },
           { "mData": "confirmtime", "sClass": "text-c" },
           { "mData": "confirmor", "sClass": "text-c" },
           { "mData": "faultdesc", "sClass": "text-c" },
           { "mData": "opinion", "sClass": "text-c" }
],

6.Datatables在开启滚动条的时候,会使得页面的tbody和thead分布在两个table

"sScrollX": "100%", //横向滚动条

7.关于使用jQuery datatable的时候想实现dragColumn

7.1.关于datatables鼠标拖动改变列宽的有用讨论信息

https://datatables.net/forums/discussion/26786/basic-column-resizing-plugin https://datatables.net//forums/discussion/comment/112220/#Comment_112220

7.2.jQuery datatables无法使用上述插件的根本原因(参考第六条)

jQuery datatables在本地使用假数据进行渲染的时候,以上插件都可以用,但是将数据从后台拿过来,在其 内部实现的时候,将thead和tbody放在了两个不一样的table里面,鼠标拖动的时候,只能实现改变header,而他的body不跟着动,这样就很恶心

7.3.实现jQuery datatables在线上也能够实现拖动

GitHub:https://github.com/Silvacom/colResize

实测,在datatables1.10.0版本可用!!