强大 JavaScript&jQuery数据表插件 Datatable.js

  • 源码大小:50.18KB
  • 所需积分:1积分
  • 源码编号:19JP-3484
  • 浏览次数:1092次
  • 最后更新:2023年06月18日
  • 所属栏目:表格
本站默认解压密码:19jp.com 或 19jp_com

简介

对于一个希望以易读、用户友好和快速的方式显示任何类型信息的网站来说,数据表是最重要和有用的元素之一

借助datatable.js JavaScript库,您可以轻松生成一个功能丰富的数据表,支持任何数据类型,如静态HTML表、js数组、js对象和Ajax JSON数据。它也是一个强大的JavaScript数据表插件,省去了构建自己的分页、过滤和排序引擎的所有麻烦。兼容jQuery和Vanilla JavaScript。

如何使用它:

1.在文档中下载并加载Datatable.js插件的文件。

  1. <!-- Vanilla JS -->
  2. <link rel="stylesheet" href="css/datatable.min.css" />
  3. <script src="js/datatable.min.js"></script>
  4.  
  5. <!-- jQuery -->
  6. <link rel="stylesheet" href="css/datatable.min.css" />
  7. <script src="js/datatable.min.js"></script>
  8. <script src="/path/to/cdn/jquery.min.js"></script>
  9. <script src="js/datatable.jquery.min.js"></script>

2.在HTML表旁边创建一个空的分页容器。

  1. <table class="table">
  2. <thead>
  3. ... table header ...
  4. </thead>
  5. <tbody>
  6. ... table body ...
  7. </tbody>
  8. </table>
  9. <!-- Pagination Links Will Be Placed Here -->
  10. <div id="myPagi"></div>

3.调用HTML表上的函数,插件会处理剩下的部分。

  1. // Vanilla JavaScript
  2. var myTable = new DataTable(document.querySelector('.table'), {
  3.  
  4. // number of entries per page
  5. pageSize: 10,
  6.  
  7. // enable SORT on specific columns
  8. sort: [true, true, false],
  9.  
  10. // enable FILTER on specific columns
  11. filters: [true, false, 'select'],
  12. // selector of pagination control
  13. pagingDivSelector: "#myPagi",
  14.  
  15. });
  16.  
  17. // jQuery
  18. $('.table').datatable({
  19.  
  20. // number of entries per page
  21. pageSize: 10,
  22.  
  23. // enable SORT on specific columns
  24. sort: [true, true, false],
  25.  
  26. // enable FILTER on specific columns
  27. filters: [true, false, 'select'],
  28. // selector of pagination control
  29. pagingDivSelector: "#myPagi",
  30. });

4.该插件不仅适用于堆叠的HTML表,还适用于以下任何数据类型:

  1. var myTable = new DataTable(document.querySelector('.table'), {
  2. data: [
  3. ['jQuery', 'Script'],
  4. ['Google', 'Com'],
  5. ['Facebook', 'Com']
  6. ]
  7. });
  8.  
  9. // OR
  10. var myTable = new DataTable(document.querySelector('.table'), {
  11. data: [
  12. {firstname: 'jQuery', lastname: 'Script'},
  13. {firstname: 'Google', lastname: 'Com'},
  14. {firstname: 'Facebook', lastname: 'Com'}
  15. ]
  16. });
  17.  
  18. // OR AJAX (JSON) Data
  19. var myTable = new DataTable(document.querySelector('.table'), {
  20. data: {
  21. // path to the service
  22. url: "/path/to/get",
  23.  
  24. // post or get
  25. type: "post",
  26.  
  27. // total amount of data to load,
  28. size: null,
  29.  
  30. // determine whether to load all data in one AJAX call
  31. allInOne: false,
  32.  
  33. // set to a positive value to refresh the data every X milliseconds
  34. refresh: false,
  35. }
  36. });

5.用于自定义数据表的所有默认选项和回调函数。

  1. var myTable = new DataTable(document.querySelector('.table'), {
  2. /**
  3. * Specify whether the type of the column should be deduced or not. If this option
  4. * is true, the type is not deduced (mainly here for backward compatibility).
  5. *
  6. * @see dataTypes
  7. */
  8. forceStrings: false,
  9.  
  10. /**
  11. * Specify the class of the table.
  12. *
  13. */
  14. tableClass: 'datatable',
  15.  
  16. /**
  17. * Specify the selector for the paging div element.
  18. *
  19. */
  20. pagingDivSelector: '.paging',
  21.  
  22. /**
  23. * Specify the class for the paging div element.
  24. *
  25. */
  26. pagingDivClass: 'text-center',
  27.  
  28. /**
  29. * Specify the class for the paging list element.
  30. *
  31. */
  32. pagingListClass: 'pagination',
  33.  
  34. /**
  35. * Specify the class for the paging list item elements.
  36. *
  37. */
  38. pagingItemClass: '',
  39.  
  40. /**
  41. * Specify the class for the paging list link elements.
  42. *
  43. */
  44. pagingLinkClass: '',
  45.  
  46. /**
  47. * Specify the href attribute for the paging list link elements.
  48. *
  49. */
  50. pagingLinkHref: '',
  51.  
  52. /**
  53. * Specify the tabindex attribute for the paging list link elements when
  54. * disabled.
  55. *
  56. */
  57. pagingLinkDisabledTabIndex: false,
  58.  
  59. /**
  60. * Specify the selector for the counter div element.
  61. *
  62. * @see counterText
  63. */
  64. counterDivSelector: '.counter',
  65.  
  66. /**
  67. * Specify the selector the loading div element.
  68. *
  69. * @see data
  70. */
  71. loadingDivSelector: '.loading',
  72.  
  73. /**
  74. * Sepcify the sort options.
  75. *
  76. * @type boolean|string|Array|Object
  77. */
  78. sort: false,
  79.  
  80. /**
  81. * Specify the default sort key.
  82. *
  83. * @type boolean|int|string.
  84. */
  85. sortKey: false,
  86.  
  87. /**
  88. * Specify the default sort directions, 'asc' or 'desc'.
  89. *
  90. */
  91. sortDir: 'asc',
  92.  
  93. /**
  94. * Specify the number of columns, a value of -1 (default) specify
  95. * the the number of columns should be retrieved for the <thead>
  96. * elements of the table.
  97. *
  98. */
  99. nbColumns: -1,
  100.  
  101. /**
  102. * Specify the number of elements to display per page.
  103. *
  104. */
  105. pageSize: 20,
  106.  
  107. /**
  108. * Specify the number of pages to display in the paging list element.
  109. *
  110. */
  111. pagingNumberOfPages: 9,
  112.  
  113. /**
  114. * Specify the way of identifying items from the data array:
  115. *
  116. * - if this option is false (default), no identification is provided.
  117. * - if a Function is specified, the function is used to identify:
  118. * function (id, item) -> boolean
  119. * - if an int or a string is specified, items are identified by the
  120. * value corresponding to the key.
  121. *
  122. * @type boolean|int|string|Function.
  123. *
  124. */
  125. identify: false,
  126.  
  127. /**
  128. * Callback function when the table is updated.
  129. *
  130. */
  131. onChange: function (oldPage, newPage) { },
  132.  
  133. /**
  134. * Function used to generate content for the counter div element.
  135. *
  136. */
  137. counterText: function (currentPage, totalPage,
  138. firstRow, lastRow,
  139. totalRow, totalRowUnfiltered) {
  140. var counterText = 'Page ' + currentPage + ' on ' + totalPage
  141. + '. Showing ' + firstRow + ' to ' + lastRow + ' of ' + totalRow + ' entries';
  142. if (totalRow != totalRowUnfiltered) {
  143. counterText += ' (filtered from ' + totalRowUnfiltered + ' total entries)';
  144. }
  145. counterText += '.';
  146. return counterText;
  147. },
  148.  
  149. /**
  150. * Content of the paging item pointing to the first page.
  151. *
  152. */
  153. firstPage: '&lt;&lt;',
  154.  
  155. /**
  156. * Content of the paging item pointing to the previous page.
  157. *
  158. */
  159. prevPage: '&lt;',
  160.  
  161. /**
  162. *
  163. */
  164. pagingPages: false,
  165.  
  166. /**
  167. * Content of the paging item pointing to the next page.
  168. *
  169. */
  170. nextPage: '&gt;',
  171.  
  172. /**
  173. * Content of the paging item pointing to the last page.
  174. *
  175. */
  176. lastPage: '&gt;&gt;',
  177.  
  178. /**
  179. * Specify the type of the columns:
  180. *
  181. * - if false, the type is not deduced and values are treated as strings.
  182. * - if true, the type is deduced automatically.
  183. * - if an Array is specified, the type of each column is retrieve from the
  184. * array values, possible values are 'int', 'float' <> 'double', 'date' <> 'datetime',
  185. * false <> true <> 'string' <> 'str'. A function can also be specified to convert
  186. * the value.
  187. *
  188. * @see forceStrings
  189. *
  190. */
  191. dataTypes: true,
  192.  
  193. /**
  194. * Specify the filter options.
  195. *
  196. */
  197. filters: {},
  198.  
  199. /**
  200. * Specify the placeholder for the textual input filters.
  201. */
  202. filterText: 'Search... ',
  203.  
  204. /**
  205. * Specify the placeholder for the select input filters.
  206. */
  207. filterEmptySelect: '',
  208.  
  209. /**
  210. *
  211. */
  212. filterSelectOptions: false,
  213.  
  214. /**
  215. *
  216. */
  217. filterInputClass: 'form-control',
  218.  
  219. /**
  220. *
  221. */
  222. filterSelectClass: 'form-control',
  223.  
  224. /**
  225. * Callback function before the display is reloaded.
  226. *
  227. */
  228. beforeRefresh: function () { },
  229.  
  230. /**
  231. * Callback function after the display has been reloaded.
  232. *
  233. */
  234. afterRefresh: function () { },
  235.  
  236. /**
  237. * Function used to generate the row of the table.
  238. *
  239. */
  240. lineFormat: function (id, data) {
  241. var res = document.createElement('tr');
  242. res.dataset.id = id;
  243. for (var key in data) {
  244. if (data.hasOwnProperty(key)) {
  245. res.innerHTML += '<td>' + data[key] + '</td>';
  246. }
  247. }
  248. return res;
  249. }
  250. });
  251.  
  252. // OR
  253. var myTable = new DataTable(document.querySelector('.table'), {
  254. data: [
  255. {firstname: 'jQuery', lastname: 'Script'},
  256. {firstname: 'Google', lastname: 'Com'},
  257. {firstname: 'Facebook', lastname: 'Com'}
  258. ]
  259. });
  260.  
  261. // OR AJAX (JSON) Data
  262. var myTable = new DataTable(document.querySelector('.table'), {
  263. data: {
  264. // path to the service
  265. url: "/path/to/get",
  266.  
  267. // post or get
  268. type: "post",
  269.  
  270. // total amount of data to load,
  271. size: null,
  272.  
  273. // determine whether to load all data in one AJAX call
  274. allInOne: false,
  275.  
  276. // set to a positive value to refresh the data every X milliseconds
  277. refresh: false,
  278. }
  279. });
  280.  
  281. // jQuery
  282. $('.table').datatable({
  283.  
  284. // number of entries per page
  285. pageSize: 10,
  286.  
  287. // enable SORT on specific columns
  288. sort: [true, true, false],
  289.  
  290. // enable FILTER on specific columns
  291. filters: [true, false, 'select'],
  292. // selector of pagination control
  293. pagingDivSelector: "#myPagi",
  294.  
  295. });

6.API方法。

  1. // refresh the data table
  2. myTable.refresh();
  3.  
  4. // destroy the plugin
  5. myTable.destroy();
  6.  
  7. // set an option
  8. myTable.option(OptionName, Value);
  9.  
  10. // load the specified page
  11. myTable.page(number);
  12.  
  13. // get the current page number
  14. myTable.page();
  15.  
  16. // reset all filters
  17. myTable.reset-filters();
  18.  
  19. // retrieve all the element
  20. myTable.select();
  21.  
  22. // retrieve the first element found with the specified ID
  23. myTable.select(ID);
  24.  
  25. // retrieve a filtered set of elements
  26. myTable.select(FUNCTION);
  27.  
  28. // add more data to the data array
  29. myTable.insert(Element/[E1, E2, ...]);
  30.  
  31. // update the first element found with the specified ID with the new specified data
  32. myTable.update(ID, Element);
  33.  
  34. // delete the first element found with the specified ID otherwise
  35. myTable.delete(ID);
  36.  
  37. // delete all the element matching the specified function if a function is specified
  38. myTable.delete(FUNCTION);

预览截图