Posted By

aamirrajpoot on 09/18/10


Tagged

sorter


Versions (?)

DIV Sorter


 / Published in: JavaScript
 

This is a jQuery plugin. It is alternate of Table Sorter, as it will sort the DIV

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. (function($) {
  7. $.fn.divSorter = function(settings) {
  8. settings = jQuery.extend({
  9. container: '',
  10. column_prefix: 'sortindex_',
  11. class_normal: 'div_sorter_normal_class',
  12. class_asc: 'div_sorter_asc_class',
  13. class_desc: 'div_sorter_desc_class',
  14. classNotToUse:'nosort'
  15. }, settings);
  16.  
  17.  
  18.  
  19. return this.each(function(){
  20.  
  21. this_id = $(this).attr('id');
  22.  
  23.  
  24. $('#'+this_id+' div').each(function(i){
  25.  
  26.  
  27. if( !$(this).hasClass(settings.classNotToUse) ){
  28.  
  29. $(this).bind('click', function(){
  30. $('#'+this_id+' div').removeClass(settings.class_asc);
  31. $('#'+this_id+' div').removeClass(settings.class_desc);
  32.  
  33.  
  34. div_sorter_sort_column_function( settings.container, settings.column_prefix + (i+1), this, settings.class_desc, settings.class_asc);
  35.  
  36. }).addClass(settings.class_normal);
  37. }
  38. });
  39. });
  40. };
  41. })(jQuery);
  42.  
  43. var div_sorter_column_index;
  44. function div_sorter_sort_column_function( Div_container, column_div, obj, class_desc, class_asc ){
  45.  
  46. if(div_sorter_column_index && div_sorter_column_index == column_div){
  47. $(obj).addClass(class_desc);
  48. div_columns_data = $("div." + column_div ).sort(function(a,b){
  49.  
  50. if ($(a).attr('isNum') != undefined){
  51. return $(a).attr('isNum') - $(b).attr('isNum') ;
  52. }
  53. else
  54. return a.innerHTML < b.innerHTML ? 1 : -1;
  55. });
  56. div_sorter_column_index = null;
  57. }
  58. else{
  59. $(obj).addClass(class_asc);
  60. div_columns_data = $("div." + column_div ).sort(function(a,b){
  61. if ($(a).attr('isNum') != undefined)
  62. return $(b).attr('isNum') - $(a).attr('isNum');
  63. else
  64. return a.innerHTML > b.innerHTML ? 1 : -1;
  65. });
  66. div_sorter_column_index = column_div;
  67. }
  68.  
  69. for(i = 0; i < div_columns_data.length; i++){
  70. $('#' + Div_container).append( $(div_columns_data[i]).parent() );
  71. }
  72. }

Report this snippet  

You need to login to post a comment.