Data source
I often use two kinds, one is the array in JavaScript, by passing an array named Aadata in the initialization object, can also provide the tabular data, the prefix AA indicates that this is an array of arrays, the outer array represents the table row, each row is also an array.
(The name of the field can be used either as a type or as a field name or as a aadata or as a data,aocolumns or as a columns)
Such as:
$ (document). Ready (function() {$ (' #demo '). html (' <table class= "table b-t b-light" id= "DataTables" > </table> "); $ (' #example '). DataTable ({data: [["Trident", "Internet Explorer 4.0", "Win 95+", 4, "X"], ["Trident", "Internet Explorer 5.0", "Win 95+", 5, "C"], ["Trident", "Internet Explorer 6.0", "Win 98+", 6, "A"], ["Trident", "Internet Explorer 7.0", "Win XP sp2+", 7, "A"], ["Gecko", "Firefox 1.5", "Win 98+/osx.2+", 1.8, "A"], ["Gecko", "Firefox 2", "Win 98+/osx.2+", 1.8, "A"], ["Gecko", "Firefox 3", "Win 2k+/osx.3+", 1.9, "A"], ["Webkit", "Safari 1.2", "osx.3", 125.5, "A"], ["Webkit", "Safari 3.0", "osx.4+", 522.1, "A"], columns: [{"Stitle": "Engine"}, {"Stitle": "Browser"}, {"Stitle": "Platform"}, {"Stitle": "Version", "Sclass": "Center"}, {"Stitle": "Grade", "Sclass": "Center", "fnrender": function(obj) { var sreturn = Obj.ada ta[Obj.idatacolumn]; if (Sreturn = = "A" ) {Sreturn = "<b>A</b>";} return Sreturn;}} ], paging: false, order:[[1, "ASC"]], Dom: ' LRTP ', columndefs: [{width: ' 30% ', targets:0},], } ); } );
The Aocolumns parameter is used to define the column of the table, an array in which each object is used to define a column.
For each of the Column objects:
Stitle defining the title of a column
Sclass defining the style of a column
The Fnrender function is used to render the column, and the function passes a parameter object whose Idatacolumn property represents the current column index, AData represents the current row array. The result returned by the function is populated in the cell.
If data is a two-dimensional array that is passed from the background, data can be assigned the following values:
Aadata was backstage.
The second way is to read the contents of the data table from an HTML table (instead of an AJAX or JavaScript data source), which by default reads the information in the table into an array of internal data tables. Each array element represents a column.
The incoming data format is as follows (such as reading the returned data format from the database):
[{ "name": " ...", "position": "...", "office": " ...", "age": " ... "," start_date ":" ... "," salary ":" ... "},{" name ":" ... "," position ":" ... "," office ":" ... " , "Age": "...", "start_date": "...", "salary": "..."},
The format of the table initialization is as follows:
$ (document). Ready (function() { $ (' #example '). DataTable { "columns": [ {"Data": "Name",}, {"Data": "Position" ,}, {"Data": "Office",}, {" Data ":" Age ",}, {" Data ":" Start_date "false,}]," Order ": [[3," desc "]]," Spaginationtype ":" F Ull_numbers ",});} );
Using Columndefs to hide columns
$ (document). Ready (function() { $ (' #example '). DataTable ({ "columndefs": [ { " Targets ": [2false// cannot be searched by the third column field content as a keyword }, {" Targets ": [3False }]} );
Vertical and horizontal scrolling
Vertical Scrolling $ (document). Ready (function() { $ (' #example '). DataTable ({"scrolly": "200px"true False }); ); // horizontal scrolling $ (document). Ready (function() {$ (' #example 'True,});});
Digital expression
$ (document). Ready (function () { //$433.060,00 $ (' #example '). DataTable ({ "language" : {"decimal": ",", "thousands": "." } } );} );
Language selection
$ (document). Ready (function() {$ (' #example '), "opaginate" : {" Sfirst ":" Home ", "Snext": "Next Page"
Paging Length Selection
$ (document). Ready (function() { $ (' #example '). DataTable ({ "lengthmenu": [[10, 25, 50,-1], [10, 25 , "All"]] }); );
Multiple Table control elements
$ (document). Ready (function() { $ (' #example '). DataTable ({ "dom": ' < "top" iflp< "clear" >>rt< "Bottom" iflp< "clear" >> " }"; ); // IFLP information is available in the header and tail of the table, and is synchronized on the top and bottom
custom toolbar
$ (document). Ready (function() { $ (' #example '). DataTable ({ "dom": ' < "toolbar" >frtip " } ); add_server' ><span> Add physical machine </span></button> "); $(". add_server"). Click (function() {location.href ="/server/import " })});
Columns render column rendering
Manipulating the contents of a cell, this technique can be useful for adding links, content-based rules that require specifying colors or any other form of text manipulation.
$ (document). Ready (function() { $ (' #example '). DataTable ({ "columndefs": [ { functionreturn data + ' (' + row[3]+ ') ';}, "Targets": 0false, "targets": [3 ]}]});} );
set default properties for a table --setting defaults
$.extend ($.fn.datatable.defaults, { false, false});
Dom/jquery Events
$ (document). Ready (function() { $ (' #example '). dataTable (); function this). EQ (0). Text (), alert (' You clicked on ' +name+ ' \ ' s row ' );}); );
Row callback
$ (document). Ready (function() {$ (' #example '). DataTable ({"Createdrow": function (row, data, index) { if (Data[5].replace (/[\$,]/g, ') * 1 > 150000 ) {$ (' TD ', Row). EQ (5). addclass (' highlight ');}}, "RowCallback": function(row, data) { /// IP column Add link if(DATA.IP) {$ (' Td:eq (3) ', row). html (' <a target= "_ Blank "href=http://' + Data.ip + ' > ' + data.ip + ' </a> '); } }, } );} );
Http://www.cnblogs.com/zhoujie/p/js4.html
Front-end common function record (iii)-datatables table initialization (RPM)