1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| (function($) { $.fn.sortingTable = function(options){ $("head").append("<style type='text/css'>.link { cursor: pointer})</style>")
var opts = $.extend({}, $.fn.sortingTable.defaults, options)
var tableId = this.attr('id') var formName = opts.formName
$("form[name='" + formName + "']").append('<input type="hidden" name="orderBy" value="">')
if( opts.sortColumns.length > 0 ){ $.each(opts.sortColumns, function(col){ $('#' + tableId + " thead th:eq(" + (col - 1) +")").addClass("link") }) }else if(opts.excludeSortColumns.length == 0) { this.find("thead th").addClass("link") }
if( opts.excludeSortColumns.length > 0 ){ this.find('thead th').each(function(index, dom){ if($.inArray((index + 1), opts.excludeSortColumns) == -1){ $(this).addClass("link") } }) }
this.find("thead th").each(function(){ if( $(this).hasClass("link") ) { $(this).click(function(){ var id = $(this).attr('id') $(":hidden[name='orderBy']").val(id)
var form = $("form[name='" + formName + "']") form.submit() }) } })
sorting(this, opts.sortBy, opts.order)
$.each(opts.mergeColumns, function(num){ _w_table_rowspan('#' + tableId, num) }) }
var sorting = function(table, sortBy, order){ var position = 0 var header = '' table.find("thead th").each(function(index, dom){ if(sortBy == $(dom).attr('id')){ position = index header = $(dom).text() } })
table.find("thead th:eq(" + position + ")").html("<font color='yellow'>" + header + "</font> <img src='images/arrow_" + order + ".gif'/>") }
var _w_table_rowspan = function(_w_table_id,_w_table_colnum){ _w_table_firsttd = "" _w_table_currenttd = "" _w_table_SpanNum = 0 _w_table_Obj = $(_w_table_id + " tr td:nth-child(" + _w_table_colnum + ")") _w_table_Obj.each(function(i){ if(i==0){ _w_table_firsttd = $(this) _w_table_SpanNum = 1 }else{ _w_table_currenttd = $(this) if(_w_table_firsttd.text()==_w_table_currenttd.text()){ //var firstSection = _w_table_firsttd.parent().find('td:eq(4)').text() //var currentSection = _w_table_currenttd.parent().find('td:eq(4)').text()
_w_table_SpanNum++ _w_table_currenttd.hide() _w_table_firsttd.attr("rowSpan",_w_table_SpanNum) }else{ _w_table_firsttd = $(this) _w_table_SpanNum = 1 } } }) }
$.fn.sortingTable.defaults = { formName: 'form1', sortBy: '', order: 'asc', sortColumns: [], excludeSortColumns: [], mergeColumns: [] } })(jQuery)
|