Advertisement
Uno-Dan

Jquery Plugins

Dec 22nd, 2020
835
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. (function( $ ) {
  4.     $.fn.reindex = function() {
  5.         $(this).find('tbody tr:not(.details)').each(function(idx) {
  6.             $(this).find('td:first-child input').val((idx+1));
  7.         });
  8.         $(this).find("tbody>tr:not(.details):odd").css("background-color", "#d7d7d7");
  9.         $(this).find("tbody>tr:not(.details):even").css("background-color", "#ffffff");
  10.         $(this).find("tbody>tr:not(.details):odd input").css("background-color", "#d7d7d7");
  11.         $(this).find("tbody>tr:not(.details):even input").css("background-color", "#ffffff");
  12.         return $(this);
  13.     };
  14. }) ( jQuery );
  15.  
  16. (function( $ ) {
  17.     $.fn.sortable = function() {
  18.         function sorter() {
  19.             getCellValue = ( tr, idx ) =>
  20.                 $( tr ).find('td').eq( idx ).find( 'input' ).val();
  21.            
  22.             comparer = (idx, asc) => (a, b) => ((v1, v2) =>
  23.                 v1 !== '' && v2 !== '' && !isNaN(v1) && !isNaN(v2) ? v1 - v2 : v1.toString().localeCompare(v2)
  24.                 ) ( getCellValue(asc ? a : b, idx), getCellValue(asc ? b : a, idx) );
  25.  
  26.             var tbl_rows = table.find('>tbody>tr:not(.details)');
  27.  
  28.             var idx = $(this).closest('th').index();
  29.  
  30.             var tbl = table[0];
  31.             if (typeof tbl.asc === 'undefined') {
  32.                 tbl.asc = 1;
  33.             }
  34.            
  35.             Array.from(tbl_rows).sort( comparer(idx, tbl.asc = !tbl.asc) )
  36.                 .forEach(tr => table.find('>tbody')[0].appendChild(tr) );
  37.  
  38.             tbl_rows.each(function() {
  39.                 var elm = $(this);
  40.                 var id = $(this).attr('data-id');
  41.                 var details = elm.parent().find('tr.details[data-id=' + id + ']');
  42.                 details.insertAfter(elm);
  43.             });
  44.         }
  45.        
  46.         var table = $(this);
  47.         var wrap = $('<div class="table wrap"></div>');
  48.         table.parent().append( wrap );
  49.         wrap.append( table );
  50.         table.find('thead th span').on('click', sorter);
  51.         table.css('display', 'block');
  52.        
  53.         return table;
  54.     };
  55. }) ( jQuery );
  56.  
  57. (function( $ ) {
  58.     $.fn.size = function(table_width=0) {
  59.         var table = $(this);
  60.         table.width( 0 );
  61.        
  62.         var wrap = $(this).closest('.table.wrap');
  63.         wrap.css( { width: table_width, overflow: 'auto' } );
  64.        
  65.         var span = $('<span>' +
  66.             table.find('td:last-child input').val() + '</span>');
  67.         $('footer').append( span.hide() );
  68.  
  69.         table.find('th span').each(function( ) {
  70.             $(this).width( $(this).closest('button').width() );
  71.         });
  72.         table.find('th:last-child span').width( span.width() );
  73.  
  74.         wrap.on('mouseup', function() {  
  75.             wrap.off('mousemove');
  76.         });
  77.         wrap.on('mouseleave', function() {  
  78.             wrap.off('mousemove');
  79.         });
  80.  
  81.         table.find('thead th button').on('dblclick', function() {
  82.             var width = 0;
  83.             var idx = $(this).closest('th').index();
  84.             table.find('tbody tr:not(.details)').each(function() {
  85.                 var input = $(this).find('td').eq( idx ).find('input');
  86.                 span.text( input.val() );
  87.                 width = span.width() > width ? span.width() : width;
  88.             });
  89.             table.find('thead th').eq( idx ).width(width);
  90.             table.find('thead th span').eq( idx ).width(width);
  91.             return false;
  92.         });
  93.        
  94.         wrap.on('mousedown', function(e) {  
  95.             var deltaX = 0;
  96.             var curX = e.pageX;
  97.             var elm = $(document.elementFromPoint(e.pageX, e.pageY)).closest('th');
  98.  
  99.             if(e.offsetX > elm.outerWidth() - 6) {
  100.                 wrap.on('mousemove', function(e) {
  101.                     deltaX = e.pageX - curX;
  102.                     curX = e.pageX;
  103.                     elm.width( elm.width() + deltaX );
  104.  
  105.                     elm.find('span').width( elm.find('span').width() + deltaX );
  106.                     if ( table.width() + deltaX > wrap.width() ) {
  107.                         table.width(table.width() + deltaX);
  108.                         elm.find('span').width( elm.find('span').width() + deltaX );
  109.                     }
  110.                 });
  111.                 return false;
  112.             }
  113.         });
  114.         return $(this);
  115.     };
  116. }) ( jQuery );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement