jquery datatables api
Posted 看一场塞北的雪
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jquery datatables api相关的知识,希望对你有一定的参考价值。
aoColumnDefs: This array allows you to target a specific column, multiple columns, or all columns, using the aTargets property of each object in the array (please note that aoColumnDefs was introduced in DataTables 1.7). This allows great flexibility when creating tables, as the aoColumnDefs arrays can be of any length, targeting the columns you specifically want. The aTargets property is an array to target one of many columns and each element in it can be:
aoColumnDefs:这个数组允许您针对一个特定列,多个列,或者所有列,使用aTargets属性的数组中的每个对象(请注意,介绍了
aoColumnDefs datatable
1.7)。这提供了很大的灵活性在创建表,因为aoColumnDefs数组可以是任意长度,目标是你特别想要的列。aTargets的属性是一个数组来
目标众多列和每个元素在它可以:
- a string - class name will be matched on the TH for the column
- 0 or a positive integer - column index counting from the left
- a negative integer - column index counting from the right
- the string "_all" - all columns (i.e. assign a default)
aoColumns: If specified, then the length of this
array must be
equal to the number of columns in the original html table. Use
‘null‘ where you wish to use only the default values and
automatically detected options.
aoColumnDefs参数和aoColumns可以一起使用,尽管aoColumnDefs优先aoColumns的灵活性。
如果两者都使用,aoColumns定义将最高优先级。同样,如果相同的列的目标是在aoColumnDefs多次,第一个元素的数组将最高优先级,最后
一个最低的。
aDataSort:定义一个列或多个列自定义排序
- // Using aoColumnDefs
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumnDefs": [
- { "aDataSort": [ 0, 1 ], "aTargets": [ 0 ] },
- { "aDataSort": [ 1, 0 ], "aTargets": [ 1 ] },
- { "aDataSort": [ 2, 3, 4 ], "aTargets": [ 2 ] }
- ]
- } );
- } );
- // Using aoColumns
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumns": [
- { "aDataSort": [ 0, 1 ] },
- { "aDataSort": [ 1, 0 ] },
- { "aDataSort": [ 2, 3, 4 ] },
- null,
- null
- ]
- } );
- } );
asSorting:控制列的升序或降序到自定义列
- // Using aoColumnDefs
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumnDefs": [
- { "asSorting": [ "asc" ], "aTargets": [ 1 ] },
- { "asSorting": [ "desc", "asc", "asc" ], "aTargets": [ 2 ] },
- { "asSorting": [ "desc" ], "aTargets": [ 3 ] }
- ]
- } );
- } );
- // Using aoColumns
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumns": [
- null,
- { "asSorting": [ "asc" ] },
- { "asSorting": [ "desc", "asc", "asc" ] },
- { "asSorting": [ "desc" ] },
- null
- ]
- } );
- } );
复制代码
bSearchable:设置列是否能被快速检索框搜索到
- // Using aoColumnDefs
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumnDefs": [
- { "bSearchable": false, "aTargets": [ 0 ] }
- ] } );
- } );
- // Using aoColumns
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumns": [
- { "bSearchable": false },
- null,
- null,
- null,
- null
- ] } );
- } );
bSortable:启用或禁用列排序。
- // Using aoColumnDefs
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumnDefs": [
- { "bSortable": false, "aTargets": [ 0 ] }
- ] } );
- } );
- // Using aoColumns
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumns": [
- { "bSortable": false },
- null,
- null,
- null,
- null
- ] } );
- } );
bUseRendered :高版本已经废弃这个属性,没有使用过,直接翻译吧!当使用fnRender()为一个
列,您可能希望使用原始的数据(在呈现之前)进行排序和过滤(默认是用于呈现的数据,用户可以看到)。这可能是有用的日期等。请注意,该选项已被弃用,将
被删除的下一个版本的datatable。请用mRender / mData而不是fnRender。
无例子代码;
bVisible:启用或禁用本列显示。
- // Using aoColumnDefs
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumnDefs": [
- { "bVisible": false, "aTargets": [ 0 ] }
- ] } );
- } );
- // Using aoColumns
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumns": [
- { "bVisible": false },
- null,
- null,
- null,
- null
- ] } );
- } );
fnCreatedCell:开发人员可定义的函数,就会调用一个细胞被创建(Ajax源等)或处理输入(DOM源)。这可以作为一种恭维,mRender允许您修改DOM元素(例如添加背景颜色)当元素是可用的。
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumnDefs": [ {
- "aTargets": [3],
- "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
- if ( sData == "1.7" ) {
- $(nTd).css(‘color‘, ‘blue‘)
- }
- }
- } ]
- });
- } );
fnRender:高版本已经废弃这个属性。mRender这个代替
iDataSort:列索引(从0开始!),你想要执行一个在本专栏时被选中进行排序。这可以用于排序在隐藏列例如。这个也没使用过
- // Using aoColumnDefs
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumnDefs": [
- { "iDataSort": 1, "aTargets": [ 0 ] }
- ]
- } );
- } );
- // Using aoColumns
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- "aoColumns": [
- { "iDataSort": 1 },
- null,
- null,
- null,
- null
- ]
- } );
- } );
mData:这个属性可以用来读取JSON数据从任何数据源属性,包括深层嵌套对象/属性。可以给mData在许多不同的方面影响其行为:
- integer - treated as an array index for the data source. This is the default that DataTables uses (incrementally increased for each column).
- string - read an object property from the data source. Note that you can use javascript dotted notation to read deep properties / arrays from the data source.
- null - the sDefaultContent option will be used for the cell (null by default, so you will need to specify the default content you want - typically an empty string). This can be useful on generated columns such as edit / delete action columns.
- function - the
function given will be executed whenever DataTables needs to set or
get the data for a cell in the column. The function takes three
parameters:
- {array|object} The data source for the row
- {string} The type call data requested - this will be ‘set‘ when setting data or ‘filter‘, ‘display‘, ‘type‘, ‘sort‘ or undefined when gathering data. Note that when undefined is given for the type DataTables expects to get the raw data for the object back
- {*} Data to set when the second parameter is ‘set‘.
- // Read table data from objects
- $(document).ready( function() {
- var oTable = $(‘#example‘).dataTable( {
- "sAjaxSource": "sources/deep.txt",
- "aoColumns": [
- { "mData": "engine" },
- { "mData": "browser" },
- { "mData": "platform.inner" },
- { "mData": "platform.details.0" },
- { "mData": "platform.details.1" }
- ]
- } );
- } );
- // Using mData as a function to provide different information for
- // sorting, filtering and display. In this case, currency (price)
- $(document).ready( function() {
- var oTable = $(‘#example‘).dataTable( {
- "aoColumnDefs": [ {
- "aTargets": [ 0 ],
- "mData": function ( source, type, val ) {
- if (type === ‘set‘) {
- source.price = val;
- // Store the computed dislay and filter values for efficiency
- source.price_display = val=="" ? "" : "[ DISCUZ_CODE_7 ]quot;+numberFormat(val);
- source.price_filter = val=="" ? "" : "[ DISCUZ_CODE_7 ]quot;+numberFormat(val)+" "+val;
- return;
- }
- else if (type === ‘display‘) {
- return source.price_display;
- }
- else if (type === ‘filter‘) {
- return source.price_filter;
- }
- // ‘sort‘, ‘type‘ and undefined all just use the integer
- return source.price;
- }
- } ]
- } );
- } );
mRender :This property is the
rendering partner to mData and it is suggested that when you want
to manipulate data for display (including filtering, sorting etc)
but not altering the underlying data for the table, use this
property. mData can actually do everything this property can and
more, but this parameter is easier to use since there is no ‘set‘
option. Like mData is can be given in a number of different ways to
effect its behaviour, with the addition of supporting array syntax
for easy outputting of arrays (including arrays of objects):
- integer - treated as an array index for the data source. This is the default that DataTables uses (incrementally increased for each column).
- string - read an object property from the data source. Note that you can use Javascript dotted notation to read deep properties / arrays from the data source and also array brackets to indicate that the data reader should loop over the data source array. When characters are given between the array brackets, these characters are used to join the data source array together. For example: "accounts[, ].name" would result in a comma separated list with the ‘name‘ value from the ‘accounts‘ array of objects.
- function - the
function given will be executed whenever DataTables needs to set or
get the data for a cell in the column. The function takes three
parameters:
- {array|object} The data source for the row (based on mData)
- {string} The type call data requested - this will be ‘filter‘, ‘display‘, ‘type‘ or ‘sort‘.
- {array|object} The full data source for the row (not based on mData)
- // Create a comma separated list from an array of objects
- $(document).ready( function() {
- var oTable = $(‘#example‘).dataTable( {
- "sAjaxSource": "sources/deep.txt",
- "aoColumns": [
- { "mData": "engine" },
- { "mData": "browser" },
- {
- "mData": "platform",
- "mRender": "[, ].name"
- }
- ]
- } );
- } );
- // Use as a function to create a link from the data source
- $(document).ready( function() {
- var oTable = $(‘#example‘).dataTable( {
- "aoColumnDefs": [ {
- "aTargets": [ 0 ],
- "mData": "download_link",
- "mRender": function ( data, type, full ) {
- return ‘Download‘;
- }
- } ]
- } );
- } );
sCellType:Change the cell type created for the
column - either TD cells or TH cells. This can be useful as TH
cells have semantic meaning in the table body, allowing them to act
as a header for a row (you may wish to add scope=‘row‘ to the TH
elements).
- // Make the first column use TH cells
- $(document).ready( function() {
- var oTable = $(‘#example‘).dataTable( {
- "aoColumnDefs": [ {
- "aTargets": [ 0 ],
- "sCellType": "th"
- } ]
- } );
- } );
sClass:给列加上自己定义的属性
- // Using aoColumnDefs
- $(document).ready( function() {
- $(‘#example‘).dataTable( {
- 以上是关于jquery datatables api的主要内容,如果未能解决你的问题,请参考以下文章
找不到文件'dataTables/jquery.dataTables' Rails 4