WordPress.com



/*

• jQuery.datables.js

* File: jquery.dataTables.js

* Version: 1.4.3

* CVS: $Id$

* Description: Paginate, search and sort HTML tables

* Author: Allan Jardine (sprymedia.co.uk)

* Created: 28/3/2008

* Modified: $Date$ by $Author$

* Language: Javascript

* License: GPL v2 or BSD 3 point style

* Project: Mtaala

* Contact: allan.jardine@sprymedia.co.uk

*

* Copyright 2008-2009 Allan Jardine, all rights reserved.

*

* This source file is free software, under either the GPL v2 license or a

* BSD style license, as supplied with this software.

*

* This source file is distributed in the hope that it will be useful, but

* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY

* or FITNESS FOR A PARTICULAR PURPOSE. See the license files for details.

*

* For details pleease refer to:

*/

/*

* When considering jsLint, we need to allow eval() as it it is used for reading cookies and

* building the dynamic multi-column sort functions.

*/

/*jslint evil: true */

(function($) {

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

* DataTables variables

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*

* Variable: dataTableSettings

* Purpose: Store the settings for each dataTables instance

* Scope: jQuery.fn

*/

$.fn.dataTableSettings = [];

/*

* Variable: dataTableExt

* Purpose: Container for customisable parts of DataTables

* Scope: jQuery.fn

*/

$.fn.dataTableExt = {};

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

* DataTables extensible objects

*

* The $.fn.dataTableExt object is used to provide an area where user dfined plugins can be

* added to DataTables. The following properties of the object are used:

* oApi - Plug-in API functions

* aTypes - Auto-detection of types

* oSort - Sorting functions used by DataTables (based on the type)

* oPagination - Pagination functions for different input styles

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*

* Variable: oApi

* Purpose: Container for plugin API functions

* Scope: jQuery.fn.dataTableExt

*/

$.fn.dataTableExt.oApi = { };

/*

* Variable: oPagination

* Purpose: Container for the various type of pagination that dataTables supports

* Scope: jQuery.fn.dataTableExt

*/

$.fn.dataTableExt.oPagination = {

/*

* Variable: two_button

* Purpose: Standard two button (forward/back) pagination

* Scope: jQuery.fn.dataTableExt.oPagination

*/

"two_button": {

/*

* Function: oPagination.two_button.fnInit

* Purpose: Initalise dom elements required for pagination with forward/back buttons only

* Returns: -

* Inputs: object:oSettings - dataTables settings object

* function:fnCallbackDraw - draw function which must be called on update

*/

"fnInit": function ( oSettings, fnCallbackDraw )

{

oSettings.nPrevious = document.createElement( 'div' );

oSettings.nNext = document.createElement( 'div' );

if ( oSettings.sTableId !== '' )

{

oSettings.nPaginate.setAttribute( 'id', oSettings.sTableId+'_paginate' );

oSettings.nPrevious.setAttribute( 'id', oSettings.sTableId+'_previous' );

oSettings.nNext.setAttribute( 'id', oSettings.sTableId+'_next' );

}

oSettings.nPrevious.className = "paginate_disabled_previous";

oSettings.nNext.className = "paginate_disabled_next";

oSettings.nPaginate.appendChild( oSettings.nPrevious );

oSettings.nPaginate.appendChild( oSettings.nNext );

$(oSettings.nPaginate).insertAfter( oSettings.nTable );

$(oSettings.nPrevious).click( function() {

oSettings.iDisplayStart -= oSettings.iDisplayLength;

/* Correct for underrun */

if ( oSettings.iDisplayStart < 0 )

{

oSettings.iDisplayStart = 0;

}

fnCallbackDraw( oSettings );

} );

$(oSettings.nNext).click( function() {

/* Make sure we are not over running the display array */

if ( oSettings.iDisplayStart + oSettings.iDisplayLength < oSettings.aiDisplay.length )

{

oSettings.iDisplayStart += oSettings.iDisplayLength;

}

fnCallbackDraw( oSettings );

} );

},

/*

* Function: oPagination.two_button.fnUpdate

* Purpose: Update the two button pagination at the end of the draw

* Returns: -

* Inputs: object:oSettings - dataTables settings object

* function:fnCallbackDraw - draw function which must be called on update

*/

"fnUpdate": function ( oSettings, fnCallbackDraw )

{

oSettings.nPrevious.className =

( oSettings.iDisplayStart === 0 ) ?

"paginate_disabled_previous" : "paginate_enabled_previous";

oSettings.nNext.className =

( oSettings.iDisplayEnd == oSettings.aiDisplay.length ) ?

"paginate_disabled_next" : "paginate_enabled_next";

}

},

/*

* Variable: iFullNumbersShowPages

* Purpose: Change the number of pages which can be seen

* Scope: jQuery.fn.dataTableExt.oPagination

*/

"iFullNumbersShowPages": 5,

/*

* Variable: full_numbers

* Purpose: Full numbers pagination

* Scope: jQuery.fn.dataTableExt.oPagination

*/

"full_numbers": {

/*

* Function: oPagination.full_numbers.fnInit

* Purpose: Initalise dom elements required for pagination with a list of the pages

* Returns: -

* Inputs: object:oSettings - dataTables settings object

* function:fnCallbackDraw - draw function which must be called on update

*/

"fnInit": function ( oSettings, fnCallbackDraw )

{

var nFirst = document.createElement( 'span' );

var nPrevious = document.createElement( 'span' );

var nList = document.createElement( 'span' );

var nNext = document.createElement( 'span' );

var nLast = document.createElement( 'span' );

nFirst.innerHTML = oSettings.oLanguage.oPaginate.sFirst;

nPrevious.innerHTML = oSettings.oLanguage.oPaginate.sPrevious;

nNext.innerHTML = oSettings.oLanguage.oPaginate.sNext;

nLast.innerHTML = oSettings.oLanguage.oPaginate.sLast;

nFirst.className = "paginate_button first";

nPrevious.className = "paginate_button previous";

nNext.className="paginate_button next";

nLast.className = "paginate_button last";

oSettings.nPaginate.appendChild( nFirst );

oSettings.nPaginate.appendChild( nPrevious );

oSettings.nPaginate.appendChild( nList );

oSettings.nPaginate.appendChild( nNext );

oSettings.nPaginate.appendChild( nLast );

$(nFirst).click( function () {

oSettings.iDisplayStart = 0;

fnCallbackDraw( oSettings );

} );

$(nPrevious).click( function() {

oSettings.iDisplayStart -= oSettings.iDisplayLength;

/* Correct for underrun */

if ( oSettings.iDisplayStart < 0 )

{

oSettings.iDisplayStart = 0;

}

fnCallbackDraw( oSettings );

} );

$(nNext).click( function() {

/* Make sure we are not over running the display array */

if ( oSettings.iDisplayStart + oSettings.iDisplayLength < oSettings.aiDisplay.length )

{

oSettings.iDisplayStart += oSettings.iDisplayLength;

}

fnCallbackDraw( oSettings );

} );

$(nLast).click( function() {

var iPages = parseInt( (oSettings.aiDisplay.length-1) / oSettings.iDisplayLength, 10 ) + 1;

oSettings.iDisplayStart = (iPages-1) * oSettings.iDisplayLength;

fnCallbackDraw( oSettings );

} );

/* Take the brutal approach to cancelling text selection */

$('span', oSettings.nPaginate).bind( 'mousedown', function () { return false; } );

$('span', oSettings.nPaginate).bind( 'selectstart', function () { return false; } );

oSettings.nPaginateList = nList;

},

/*

* Function: oPagination.full_numbers.fnUpdate

* Purpose: Update the list of page buttons shows

* Returns: -

* Inputs: object:oSettings - dataTables settings object

* function:fnCallbackDraw - draw function which must be called on update

*/

"fnUpdate": function ( oSettings, fnCallbackDraw )

{

var iPageCount = jQuery.fn.dataTableExt.oPagination.iFullNumbersShowPages;

var iPageCountHalf = Math.floor(iPageCount / 2);

var iPages = parseInt( (oSettings.aiDisplay.length-1) / oSettings.iDisplayLength, 10 ) + 1;

var iCurrentPage = parseInt( oSettings.iDisplayStart / oSettings.iDisplayLength, 10 ) + 1;

var sList = "";

var iStartButton;

var iEndButton;

if (iPages < iPageCount)

{

iStartButton = 1;

iEndButton = iPages;

}

else

{

if (iCurrentPage = (iPages - iPageCountHalf))

{

iStartButton = iPages - iPageCount + 1;

iEndButton = iPages;

}

else

{

iStartButton = iCurrentPage - Math.ceil(iPageCount / 2) + 1;

iEndButton = iStartButton + iPageCount - 1;

}

}

}

for ( var i=iStartButton ; i y) ? 1 : 0));

},

"string-desc": function ( a, b )

{

var x = a.toLowerCase();

var y = b.toLowerCase();

return ((x < y) ? 1 : ((x > y) ? -1 : 0));

},

/*

* html sorting (ignore html tags)

*/

"html-asc": function ( a, b )

{

var x = a.replace( //g, "" ).toLowerCase();

var y = b.replace( //g, "" ).toLowerCase();

return ((x < y) ? -1 : ((x > y) ? 1 : 0));

},

"html-desc": function ( a, b )

{

var x = a.replace( //g, "" ).toLowerCase();

var y = b.replace( //g, "" ).toLowerCase();

return ((x < y) ? 1 : ((x > y) ? -1 : 0));

},

/*

* date sorting

*/

"date-asc": function ( a, b )

{

var x = Date.parse( a );

var y = Date.parse( b );

if ( isNaN( x ) )

{

x = Date.parse( "01/01/1970 00:00:00" );

}

if ( isNaN( y ) )

{

y = Date.parse( "01/01/1970 00:00:00" );

}

return x - y;

},

"date-desc": function ( a, b )

{

var x = Date.parse( a );

var y = Date.parse( b );

if ( isNaN( x ) )

{

x = Date.parse( "01/01/1970 00:00:00" );

}

if ( isNaN( y ) )

{

y = Date.parse( "01/01/1970 00:00:00" );

}

return y - x;

},

/*

* numerical sorting

*/

"numeric-asc": function ( a, b )

{

var x = a == "-" ? 0 : a;

var y = b == "-" ? 0 : b;

return x - y;

},

"numeric-desc": function ( a, b )

{

var x = a == "-" ? 0 : a;

var y = b == "-" ? 0 : b;

return y - x;

}

};

/*

* Variable: aTypes

* Purpose: Container for the various type of type detection that dataTables supports

* Scope: jQuery.fn.dataTableExt

* Notes: The functions in this array are expected to parse a string to see if it is a data

* type that it recognises. If so then the function should return the name of the type (a

* corresponding sort function should be defined!), if the type is not recognised then the

* function should return null such that the parser and move on to check the next type.

* Note that ordering is important in this array - the functions are processed linearly,

* starting at index 0.

*/

$.fn.dataTableExt.aTypes = [

/*

* Function: -

* Purpose: Check to see if a string is numeric

* Returns: string:'numeric' or null

* Inputs: string:sText - string to check

*/

function ( sData )

{

var sValidChars = "0123456789.-";

var Char;

for ( i=0 ; i' - div with a class

* Examples: '', ''

*/

this.sDomPositioning = 'lfrtip';

/*

* Variable: sPaginationType

* Purpose: Note which type of sorting should be used

* Scope: jQuery.dataTable.classSettings

*/

this.sPaginationType = "two_button";

/*

* Variable: sAjaxSource

* Purpose: Source url for AJAX data for the table

* Scope: jQuery.dataTable.classSettings

*/

this.sAjaxSource = null;

/*

* Variable: iCookieDuration

* Purpose: The cookie duration (for bStateSave) in seconds - default 2 hours

* Scope: jQuery.dataTable.classSettings

*/

this.iCookieDuration = 60 * 60 * 2;

}

/*

* Variable: oApi

* Purpose: Container for publicly exposed 'private' functions

* Scope: jQuery.dataTable

*/

this.oApi = {};

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

* API functions

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*

* Function: fnDraw

* Purpose: Redraw the table

* Returns: -

* Inputs: -

*/

this.fnDraw = function()

{

_fnReDraw( _fnSettingsFromNode( this[0] ) );

};

/*

* Function: fnFilter

* Purpose: Filter the input based on data

* Returns: -

* Inputs: string:sInput - string to filter the table on

* int:iColumn - optional - column to limit filtering to

* bool:bEscapeRegex - optional - escape regex characters or not - default true

*/

this.fnFilter = function( sInput, iColumn, bEscapeRegex )

{

var oSettings = _fnSettingsFromNode( this[0] );

if ( typeof bEscapeRegex == 'undefined' )

{

bEscapeRegex = true;

}

if ( typeof iColumn == "undefined" || iColumn === null )

{

/* Global filter */

_fnFilterComplete( oSettings, {"sSearch":sInput, "bEscapeRegex": bEscapeRegex}, 1 );

}

else

{

/* Single column filter */

oSettings.aoPreSearchCols[ iColumn ].sSearch = sInput;

oSettings.aoPreSearchCols[ iColumn ].bEscapeRegex = bEscapeRegex;

_fnFilterComplete( oSettings, oSettings.oPreviousSearch, 1 );

}

};

/*

* Function: fnSettings

* Purpose: Get the settings for a particular table for extern. manipulation

* Returns: -

* Inputs: -

*/

this.fnSettings = function( nNode )

{

return _fnSettingsFromNode( this[0] );

};

/*

* Function: fnSort

* Purpose: Sort the table by a particular row

* Returns: -

* Inputs: int:iCol - the data index to sort on. Note that this will

* not match the 'display index' if you have hidden data entries

*/

this.fnSort = function( aaSort )

{

var oSettings = _fnSettingsFromNode( this[0] );

oSettings.aaSorting = aaSort;

_fnSort( oSettings );

};

/*

* Function: fnAddData

* Purpose: Add new row(s) into the table

* Returns: array int: array of indexes (aoData) which have been added (zero length on error)

* Inputs: array:mData - the data to be added. The length must match

* the original data from the DOM

* or

* array array:mData - 2D array of data to be added

* bool:bRedraw - redraw the table or not - default true

* Notes: Warning - the refilter here will cause the table to redraw

* starting at zero

* Notes: Thanks to Yekimov Denis for contributing the basis for this function!

*/

this.fnAddData = function( mData, bRedraw )

{

var aiReturn = [];

var iTest;

if ( typeof bRedraw == 'undefined' )

{

bRedraw = true;

}

/* Find settings from table node */

var oSettings = _fnSettingsFromNode( this[0] );

/* Check if we want to add multiple rows or not */

if ( typeof mData[0] == "object" )

{

for ( var i=0 ; i ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download