Codepaste
Paste some code
Upload a file...
Open »
You have no editable files. Paste some code, or add a public key and you'll see the files you can edit listed here.
Add a public key...
File
Save as a new paste
Properties...
Email to a friend...
Download the code...
Help
The Codepaste forum
About...
test
// Static instances array in which we'll keep all the instances of SortableData SortableData.instances = []; function SortableData( data, headings, settings ) { this.data = data; this.headings = headings; this.settings = settings; // Add this to the static SortableData object for whatsit... access from HTML this.id = SortableData.instances.length; SortableData.instances.push( this ); this.page = 0; this.page_size = 0; this.sort_key = null; this.reverse = false; /* * Sorting the array based on a specific field. * @param fieldIndex int which field to sort on. */ this.sort = function( fieldIndex ) { // The comparison function for array sort var sortCompare = function( type, key, a, b, reverse ) { // Values to compare var one = a[key]; var two = b[key]; // What to do in different type situations var result = 0; switch( type ) { case 'String': result = ( two < one ) - ( one < two ); break; case 'Number': result = one - two; break; } // flip the result if ( reverse ) return 0 - result; return result; } var sortFunction = null; var type = this.headings[fieldIndex].type; if ( type == 'unsortable') return; var key = this.headings[fieldIndex].key; reverse = false; if ( this.sort_key == key ) reverse = !this.reverse; this.sort_key = key; this.reverse = reverse; // Create the appropriate sort function to pass to array.sort switch( type ) { case 'String': sortFunction = function( a,b ) { return sortCompare( 'String', key, a, b, reverse ); }; break; case 'Number': sortFunction = function( a,b ) { return sortCompare( 'Number', key, a, b, reverse ); }; break; } // Don't sort it if it's not a valid type if ( sortFunction != null ) { this.data = this.data.sort( sortFunction ); this.page = 0; } }; /** * Create and update the HTML for the table. */ this.redraw = function() { var out = ""; // Pages if ( this.page_size > 0 ) { var total_pages = this.data.length/this.page_size; out += "
"; for( var k=0; k < total_pages; k++ ) { out += "
" + (k+1) + "
"; } out += "
"; } // Data table out += "
"; out += "
"; // Headings for ( var t=0; t < this.headings.length; t++ ) out += "
" + this.headings[t].value + "
"; out += "
"; // Data for( var i=this.page_size*this.page; (i < this.data.length) && ( this.page_size == 0 || i < (this.page+1)*this.page_size ); i++ ) { out += "
"; for ( var j=0; j < this.headings.length; j++ ) { out += "
" + this.data[i][this.headings[j].key] + "
"; } out += "
"; } out += "
"; // Update, if applicable var container = document.getElementById("_SortableData_" + this.id); if ( container != null && typeof( container ) !=='undefined' ) { container.innerHTML = out; } return out; }; /** * Fake constructor function. */ this.init = function() { // Overwrite default data with data from the settings object if ( this.settings != null && typeof(this.settings) !== 'undefined' ) { for( var key in this.settings ) { if ( this.hasOwnProperty( key ) && typeof( this[key] ) !== 'undefined' ) this[key] = this.settings[key]; } } // Create the container object var out = "
"; out += this.redraw(); out += "
"; // Output it document.write( out ); } // Execute the init() function! this.init(); }; /** * Change the page a table is showing. * A static function that acts on a specific SortableData instance, by id. * @param sdid the SortableData object's id. * @param page which page to go to. */ SortableData.showPage = function( sdid, page ) { if ( sdid >= 0 && sdid < SortableData.instances.length ) { SortableData.instances[sdid].page = page; SortableData.instances[sdid].redraw( ); } }; /** * Sort a table on a new column. * A static function that acts on a specific SortableData instance, by id. * @param sdid the SortableData object's id. * @param page which page to go to. */ SortableData.sortOn = function( sdid, field ) { if ( sdid >= 0 && sdid < SortableData.instances.length ) { SortableData.instances[sdid].sort( field ); SortableData.instances[sdid].redraw( ); } }
hide
Web development by Altered Effect