Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function( $ ) {
- $.fn.config = function(args) {
- var tbl = $( this )[0];
- tbl.width = args.width ? args.width : '100%';
- tbl.height = args.height ? args.height : '100%';
- $(this).resize( tbl.width, tbl.height);
- return $( this );
- };
- }) ( jQuery ); // config
- (function( $ ) {
- $.fn.reindex = function() {
- this.find('tbody tr:not(.details)').each(function(idx) {
- $(this).find('td:first-child input').val((idx+1));
- });
- return $( this );
- };
- }) ( jQuery ); // reindex
- (function( $ ) {
- $.fn.colorize = function( args=0 ) {
- var tbl = $( this );
- var table = tbl[0];
- var hdr_fg = args.th && args.th.color ? args.th.color : 0;
- var hdr_bg = args.th && args.th.background ? args.th.background : 0;
- var sash = args.th && args.th.sash ? args.th.sash : 0;
- var odd_fg = args.td && args.td.odd_fg ? args.td.odd_fg : 0;
- var odd_bg = args.td && args.td.odd_bg ? args.td.odd_bg : table.background ? table.background : 0;
- var even_fg = args.td && args.td.even_fg ? args.td.even_fg : 0;
- var even_bg = args.td && args.td.even_bg ? args.td.even_bg : 0;
- table.hdr_fg = hdr_fg = hdr_fg ? hdr_fg : table.hdr_fg ? table.hdr_fg : '';
- table.hdr_bg = hdr_bg = hdr_bg ? hdr_bg : table.hdr_bg ? table.hdr_bg : '';
- table.sash = sash = sash ? sash : table.sash ? table.sash : hdr_bg;
- table.odd_fg = odd_fg = odd_fg ? odd_fg : table.odd_fg ? table.odd_fg : '';
- table.odd_bg = odd_bg = odd_bg ? odd_bg : table.odd_bg ? table.odd_bg : '';
- table.even_fg = even_fg = even_fg ? even_fg : table.even_fg ? table.even_fg : '';
- table.even_bg = even_bg = even_bg ? even_bg : table.even_bg ? table.even_bg : '';
- tbl.find("tbody>tr:not(.details):odd *").css("color", odd_fg);
- tbl.find("tbody>tr:not(.details):odd").css("background-color", odd_bg);
- tbl.find("tbody>tr:not(.details):odd input").css("background-color", odd_bg);
- tbl.find("tbody>tr:not(.details):even *").css("color", even_fg);
- tbl.find("tbody>tr:not(.details):even").css("background-color", even_bg);
- tbl.find("tbody>tr:not(.details):even input").css("background-color", even_bg);
- tbl.find("thead>tr *").css( { color: hdr_fg, background: hdr_bg } );
- tbl.find("thead>tr button").css( { "border-right-color": sash } );
- return tbl;
- };
- }) ( jQuery ); // colorize
- (function( $ ) {
- $.fn.sortable = function() {
- function sort() {
- value = ( tr, idx ) =>
- $( tr ).find('td').eq( idx ).find( 'input' ).val();
- column = (idx, asc) => (a, b) => ((v1, v2) =>
- v1 !== '' && v2 !== '' && !isNaN(v1) && !isNaN(v2) ? v1 - v2 : v1.toString().localeCompare(v2)
- ) ( value(asc ? a : b, idx), value(asc ? b : a, idx) );
- var table = tbl[0];
- var idx = $(this).closest('th').index();
- var tbl_rows = tbl.find('>tbody>tr:not(.details)');
- if (typeof table.asc === 'undefined') {
- table.asc = 1;
- }
- Array.from(tbl_rows).sort( column(idx, table.asc = !table.asc) )
- .forEach(tr => tbl.find('>tbody')[0].appendChild(tr) );
- tbl_rows.each(function() {
- var elm = $(this);
- var id = $(this).attr('data-id');
- elm.parent().
- find('tr.details[data-id=' + id + ']').insertAfter(elm);
- });
- tbl.colorize();
- }
- var tbl = $(this);
- var wrap = $('<div class="table wrap"></div>');
- tbl.parent().append( wrap );
- wrap.append( tbl );
- tbl.find('thead th span').on('click', sort);
- tbl.css('display', 'block');
- return tbl;
- };
- }) ( jQuery ); // sortable
- (function( $ ) {
- $.fn.resize = function(width=0, height=0) {
- var tbl = $(this);
- // width = tbl[0].width ? tbl[0].width : width;
- // height = tbl[0].height ? tbl[0].height : height;
- tbl.width( 0 );
- // Todo write code for height.
- var wrap = $(this).closest('.table.wrap');
- wrap.css( { width: width, overflow: 'auto' } );
- var span = $('<span>' +
- tbl.find('td:last-child input').val() + '</span>');
- $('footer').append( span.hide() );
- tbl.find('th span').each(function( ) {
- $(this).width( $(this).closest('button').width() );
- });
- tbl.find('th:last-child span').width( span.width() );
- wrap.on('mouseup', function() {
- wrap.off('mousemove');
- });
- wrap.on('mouseleave', function() {
- wrap.off('mousemove');
- });
- tbl.find('thead th button').on('dblclick', function() {
- var width = 0;
- var idx = $(this).closest('th').index();
- tbl.find('tbody tr:not(.details)').each(function() {
- var input = $(this).find('td').eq( idx ).find('input');
- span.text( input.val() );
- width = span.width() > width ? span.width() : width;
- });
- tbl.find('thead th').eq( idx ).width(width);
- tbl.find('thead th span').eq( idx ).width(width);
- return false;
- });
- wrap.on('mousedown', function(e) {
- var deltaX = 0;
- var curX = e.pageX;
- var elm = $(document.elementFromPoint(e.pageX, e.pageY)).closest('th');
- if(e.offsetX > elm.outerWidth() - 6) {
- wrap.on('mousemove', function(e) {
- deltaX = e.pageX - curX;
- curX = e.pageX;
- elm.width( elm.width() + deltaX );
- elm.find('span').width( elm.find('span').width() + deltaX );
- if ( tbl.width() + deltaX > wrap.width() ) {
- tbl.width(tbl.width() + deltaX);
- elm.find('span').width( elm.find('span').width() + deltaX );
- }
- });
- return false;
- }
- });
- return $(this);
- };
- }) ( jQuery ); // resize
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement