/*
* exTable 0.1.4 - jQuery plugin
* written by Cyokodog
*
* Copyright (c) 2011 Cyokodog (http://d.hatena.ne.jp/cyokodog/)
* Dual licensed under the MIT (MIT-LICENSE.txt)
* and GPL (GPL-LICENSE.txt) licenses.
*
* Built for jQuery library
* http://jquery.com
*
*/
(function($){
$.ex = $.ex || {};
$.ex.table = function(idx , targets , option){
var o = this,
c = o.config = $.extend({} , $.ex.table.defaults , option);
c.targets = targets;
c.target = c.targets.eq(idx);
c.index = idx;
var className = c.target.attr('className');
c.thead = c.target.find('> thead');
c.theadCols = c.thead.find('> tr:eq(0) > *');
c.tfoot = c.target.find('> tfoot');
c.tfootCols = c.tfoot.find('> tr:eq(0) > *');
c.tbodyCols = c.target.find('> tbody > tr:eq(0) > *');
if (!c.theadCols.size() && !c.tfootCols.size() || !c.tbodyCols.size()) return;
c.container = $('
');
if (c.theadCols.size()) {
c.container.append(c.headContainer = $(''));
c.headTable = c.headContainer.find('> table');
o._fixedWidth(c.theadCols);
}
c.container.append(c.bodyContainer = $(''));
o._fixedWidth(c.tbodyCols);
if (c.tfootCols.size()) {
c.container.append(c.footContainer = $(''));
c.footTable = c.footContainer.find('> table');
o._fixedWidth(c.tfootCols);
}
c.target.after(c.container);
o._cssCopy(c.target,c.container,[
'margin-top','margin-right','margin-bottom','margin-left'
]);
c.target.css({
'table-layout':'fixed',
'margin':0
});
if (c.theadCols.size()) {
c.thead.appendTo(c.headTable)
}
c.target.appendTo(c.bodyContainer)
if (c.tfootCols.size()) {
c.tfoot.appendTo(c.footTable)
}
o.adjustHeight();
if( c.onInit ){
c.onInit.apply( o , [ o ] );
}
}
$.extend($.ex.table.prototype,{
_fixedWidth : function(target){
var o = this, c = o.c;
var width = [];
target.each(function(idx){
width.push($(this).width());
}).
each(function(idx){
$(this).width(width[idx]);
});
return o;
},
_cssCopy : function(from,to,names){
for(var i = 0; i < names.length; i++ ){
var val = from.css(names[i]);
if(val != 'auto') to.css(names[i],val);
}
},
adjustHeight : function(){
var o = this, c = o.config;
var over = c.bodyContainer.height() > c.maxHeight;
var adjustWidth = c.maxHeight && !over ? 0 : c.scrollbarWidth;
c.container.width(c.target.width() + adjustWidth
+ (parseInt(c.target.css('border-left-width'))||0)
+ (parseInt(c.target.css('border-right-width'))||0)
);
!c.headContainer || c.headContainer.css('padding-right', adjustWidth);
!c.footContainer || c.footContainer.css('padding-right', adjustWidth);
c.bodyContainer.css({
'overflow-y' : c.maxHeight ? over ? 'auto' : 'visible' : 'scroll',
height : c.maxHeight ? (over ? c.maxHeight : 'auto') : c.height
});
return o;
},
getIndex : function(){
return this.config.index;
},
getTargets : function(){
return this.config.targets;
},
getTarget: function(){
return this.config.target;
},
getContainer : function(){
return this.config.container;
},
getHead : function(){
return this.config.headContainer;
},
getHeadTable : function(){
return this.config.headTable;
},
getBody : function(){
return this.config.bodyContainer;
},
getBodyTable: function(){
return this.config.target;
},
getFoot : function(){
return this.config.footContainer;
},
getFootTable : function(){
return this.config.footTable;
}
});
$.ex.table.defaults = {
scrollbarWidth :16,
height : 200,
maxHeight : 0,
onInit : null
}
$.fn.exTable = function(option){
var targets = this;
return targets.each(function(idx){
targets.eq(idx).data(
'ex-table',
new $.ex.table(idx,targets,option)
);
});
}
})(jQuery);