All Downloads are FREE. Search and download functionalities are using the official Maven repository.

META-INF.web.lib.js.bee.table.js Maven / Gradle / Ivy

There is a newer version: 6.2.04
Show newest version
/*!
 * 表格数据绑定插件 主要是对
的HTML进行数据绑定 * * table属性说明: id:表格唯一ID,Table对象变量名,可通过Table.{id}来引用对象 * res:取数据的URL接口,其中{}为变量名,与condition中的输入项对应,[]分级,如果分组中的{}存在,则分组存在 * 引用公共变量:${公共变量名称}, 如,有公共变量为: var context_path="asfsdf"; 引用方法为: * ${context_path}/core/******** condition:参数输入项,必须包含有form表单 * auto:是否自动加载表格数据,如果为false需要手工加载数据 key:key或唯一组合,多个属性用逗号“,”隔开 * subtotal:需要做小计属性,多个属性用逗号“,”隔开 showblankrow:是否自动填充空行,默认为false,不自动填充空行 * treefield:树型字段名称,对应列自动变成树型结构 * * th属性说明: name:与返回的数据对象中的属性名对应,其中checkbox、select等html标签、serial为保留名称 * type:数据类型,string、number、date、serial,checkbox、select等html标签 * formatter:格式化,主要用于数字类型与日期类型 span:是否需要合并结果相同列,为true是合并 * class:样式,如果包含有sort样式时,该列可做排序查询 * * 分页说明: 在tfoot中有一个class="pagebar"的容器,容器的属性有 pagesize:每页显示的数量 * simple:是否为简单分页,默认为true简单分页 * setting:分页配置,"pagesize,total,first,prev,next,last,number,pageno,jump" * pagesize:每页显示的数量,默认为20条; total:显示总记录数; first:显示第一页功能键; prev:显示上一页功能键; * next:显示下一页功能键: last:显示最后一页功能键; pageno:显示当前页码与总页数; number:显示可点击的页码数量功能键; * jump:显示自定义跳转功能区。 number:显示可点击的页码数量,默认为显示5个页码。当在setting中指定pages时,些属性生效。 * * 内部方法: * bindData(data) -自定义数据绑定 * del(id) -删除数据方法,如果ID为空时默认删除选中的数据 * * 扩展方法: * 以下方法放在jquery的加载方法中或者Table.method()中,分别使用如下: * a:jquery * $(document).ready(function() { * //放在这里,如: * Table.tableID.changeParams=function(params){ // 在这里修改参数 } * }) * * b:Table.method() * Table.method({ tableId:{ * changeParams:function(params){ // 在这里修改参数 } * } * }) * * 表格对象引用 通过"Table.{id}"来引用,如表格的ID为tableID,则引用表格的对象为"Table.tableID" * * 1)手工绑定数据 要自行绑定数据,请实现bind方法,如下 * Table.{id}.bind=function(data){//在这里绑定数据列表,data就是后台接口返回的json数据 } * * 2)数据绑定完后要处理其他业务 绑定完数据后要处理其他业务,请实现binded方法,如下: * Table.{id}.binded=function(data){ //在这里绑定完数据列表后的回调方法,data就是后台接口返回的json数据 } * * 3)请求发送前修改提交的参数 * Table.{id}.changeParams=function(params){ // 在这里修改参数 } * * 4)绑定数据前修改数据集 * Table.{id}.changeData=function(data){ // 在这里修改原数据 } * * @author chenxiang */ ;var dialog=top.dialog?top.dialog.get(window):null;var tables={};var Table=function(table){var me=this;var $tb=this._table=$(table);this.res=$tb.attr("res");this.key=$tb.attr("key");this.delapi=$tb.attr("del")||this.res?(this.res.substr(0,this.res.indexOf("'"))||this.res.substr(0,this.res.indexOf("?"))||this.res.substr(0,this.res.indexOf("#"))||this.res):null;this.intervalTime=parseInt($tb.attr("interval")||0)||0;this.type=$tb.attr("type")||($tb.hasClass("tree")?"tree":null);this.treefield=$tb.attr("treefield");this.dataname=$tb.attr("dataname");this.showblankrow=$tb.attr("showblankrow");var _method=$tb.attr("method");this.method=_method=="post"?$.post:_method=="put"?$.put:_method=="delete"?$.del:$.get;var _isinit=false;if(this.id=$tb.attr("id")){var _tmp=Table[this.id];if(_tmp){$.each(_tmp,function(k,item){me[k]=item})}tables[this.id]=Table[this.id]=Table.tables[this.id]=this}var strToNumObj=function(str){if(!str){return null}var arr=str.split(","),obj={value:{},title:{}};for(var i=0;i1?tmp[1]:"小计"}return obj};if($tb.attr("subtotal")){var subObj=strToNumObj($tb.attr("subtotal"));if(subObj){this._subtotal=subObj.value;this._subtotal_title=subObj.title}}if($tb.attr("average")){var subObj=strToNumObj($tb.attr("average"));if(subObj){this._average=subObj.value;this._average_title=subObj.title}}this.spans=[];try{var _init=$tb.attr("init");var uinit=_init?eval($tb.attr("init")):null;if(uinit&&typeof(uinit)=="function"){uinit()}}catch(e){}this._thead=$tb.children("thead").eq(0);var heads=this.heads=this._thead.children().children();var sortIndex=0;var dFields;try{dFields=JSON.parse($tb.attr("fields"))}catch(e){}this._fields=dFields||heads.length>0?{}:null;this.initHead(heads);this._loading=this._loading.replace(/{colspan}/g,heads.length);this._template=this._thead.next("tbody.template");this._nodata="{massage}";this._tbody=$("");if(this._thead.length>0){this._thead.after(this._tbody)}else{this._table.prepend(this._tbody)}this._tfoot=$tb.children("tfoot").eq(0);if($tb.attr("total")){this.total=new Table.total($tb.attr("total"));this._tbody.after(this.total.tbody=$(""))}this.pagebar=new Pagebar(this);if(_isinit==false){this.init()}_isinit=true};Table.tables={};Table.template={head:'#',headth:'${title}',search:{input:'${title}:',date:'${title}:',time:'${title}:'},nodata:"{massage}"};Table.total=function(options){if(!options){return}var that=this;try{that.options=eval("("+options+")");that.clear()}catch(e){alert("数据格式错误:"+options)}return this};Table.total.prototype={options:null,tbody:null,count:0,subtotal:function(){var f=arguments[0];var d=this.options.subtotal.total;for(var e=0;e1?this.options.average.subtotal||{}:{};for(var e=0;e0){c.$condition[0].reset()}this.params={};return this},params:{}};if(c.res.indexOf("?")>0){var d=$.getParams(c.res);if(d){c.param.params=d;if(d.pagesize>0){c.pagebar.setPagesize(d.pagesize)}c.res=c.res.substr(0,c.res.indexOf("?"))}}c.initCondition();if(c.intervalTime>0){c.interval=setInterval(function(){c.go()},c.intervalTime*1000)}},initHead:function(h,i){var j=this;if(h.params){j._table.prepend(j._thead=$(Table.template.head.replace(/{key}/g,this.key)));var f=j._thead.children();$.each(h.params,function(b,a){if(i&&i.indexOf(a.name)<0){return}var c=a.name.endsWith("date")?Table.template.search.date:a.name.endsWith("time")?Table.template.search.time:Table.template.search.input;f.append(Table.template.headth.replace("${title}",c).bind(a))});var h=j._thead.children(":last-child").children();j.initHead(h);j._nodata=Table.template.nodata.replace("{rowspan}",h.length);return}var g={};h.each(function(){var a=$(this);var b=a.attr("name");if(a.attr("span")){j.spans.push(a.index())}if(!b){return}j._fields=j._fields?j._fields:{};j._fields[a.attr("name")]={formatter:a.attr("formatter")||"",type:a.attr("type")||(a.attr("name").endsWith("time")||a.attr("name").endsWith("date")?"date":null),eq:a.find(":checkbox").val()};if(b.endsWith("date")||b.endsWith("time")){id=b+new Date().getTime();a.find("input").attr("id",id);a.addClass("sort");laydate.render({elem:"#"+id,range:true,done:function(d,e){d=d?d:undefined;if(j.param.get(this.elem[0].name)==d){return}if(d){j.param.set(this.elem[0].name,d)}else{j.param.remove(this.elem[0].name)}j.go()}})}if(a.is(".sort")){var c=$("").css("cursor","pointer");a.prepend(c).attr("title","排序方式为:升级》降序》无");j.sorts.push(a.attr("sort")||a.attr("name"));c.click(function(){if(a.is(".desc")){a.removeClass("desc")}else{if(a.is(".asc")){a.removeClass("asc").addClass("desc")}else{a.addClass("asc")}}if(a.is(".desc")){g[a.attr("name")]="-"}else{if(a.is(".asc")){g[a.attr("name")]="+"}else{delete g[a.attr("name")]}}var d=[];$.each(g,function(e,k){d.push(e+k)});j.param.set("orderby",d.join(","));j.pagebar.pageno=1;j.go()})}if(a.attr("type")=="checkbox"){a.find(":checkbox").click(function(){var d=$(this);if(this.checked){j._tbody.find(">tr>td>:checkbox[name="+this.value+"]:not('input:disabled')").prop("checked",true)}else{j._tbody.find(">tr>td>:checkbox[name="+this.value+"]:not('input:disabled')").prop("checked",false)}j._tbody.find(">tr>td>:checkbox[name="+this.value+"]:not('input:disabled')").each(function(){if(j.checkboxClick&&typeof(j.checkboxClick)=="function"){j.checkboxClick(this)}})})}});h.find("input[type='text']").on("change",function(){var a=this.value.trim();if(a){j.param.set(this.name,a);$(this).parent().siblings(".suptitle").show()}else{j.param.remove(this.name);$(this).parent().siblings(".suptitle").hide()}j.$condition.find("input[name="+this.name+"]").val(a);this.blur();j.go()});if(j.pagebar){j.pagebar.setColumn(Object.keys(j._fields).length)}},initCondition:function(){if(!this._table.attr("condition")){return}var b=this;b.$condition=$("#"+this._table.attr("condition")).find("form:eq(0)");b.$condition.submit(function(a){a.returnValue=false;a.preventDefault();a.stopPropagation();b._data=null;b.pagebar.pageno=1;b.go();return false})},changeCondParams:null,key:null,res:null,showblankrow:null,bind:null,binded:null,param:null,sorts:[],method:null,$condition:null,getKeys:function(){var d=this._thead.find(":checkbox").val();if(!d){return}var c=[];this._tbody.find(">tr>td>input:checked[name="+d+"]").each(function(){c.push(this.value)});return c},go:function(){var b=this;this._getData();return this},download:function(){var d=this;d._initConData();var f=$.extend({pagesize:d.pagebar.pagesize,pageno:d.pagebar.pageno},d.param.params,d.param.condParams);f=d.changeParams&&typeof(d.changeParams)=="function"?d.changeParams(f):f;f._t=new Date().getTime();var e=d.getUrl?d.getUrl():d._getUrl();$.download(e+"/@export",f,function(a){console.log("导出成功")})},del:function(b){this.remove(b)},remove:function(f){var e=this;if(!e.delapi){alert("请配置删除数据的接口。");return}if(!e.key){alert("请配置数据资源的KEY。");return}var d={};d[e.key]=f||e.getKeys().join(",");if(!d[e.key]){alert("请选择需要删除的数据");return}if(!confirm("确定删除选中的数据吗?")){return}$.del(e.delapi,d,function(a){e.go()})},_table:null,getUrl:null,_getUrl:function(){var b=this;return this.res.replace(/\${(.)*?}/g,function(a){return self[a.replace(/[\${}]+/g,"")]||$.getParam(a.replace(/[\${}]+/g,""))||""}).replace(/{(.)*?}/g,function(a){var d=a.replace(/[{}]+/g,"");return b.param.params[d]||$.getParam(d)||(top.data||{})[d]||(b.param.condParams||{})[d]||a}).replace(/\[.*?{.*?}\]/g,"").replace(/[\[\]]/g,"").replace(/{.*?}/g,"")},_pg:null,_thead:null,_tbody:null,_tfoot:null,_fields:null,_data:null,_tr:"{td}",_td:"{value}",_tdtree:"

{value}

",_checkbox:"",_loading:"正在加载数据,请稍后……",_loadtime:null,changeData:null,changeParams:null,checkboxClick:null,_initConData:function(){var c=this;if(!c.$condition){return}var d=c.$condition?c.$condition.serializeArray():[];if(c.changeCondParams&&typeof(c.changeCondParams)=="function"){c.changeCondParams(d)}c.param.condParams={};$.each(d,function(b,a){if(!a.value&&a.value!="0"&&a.value!==false){delete c.param.params[a.name];return}if(c.param.condParams[a.name]===undefined){c.param.condParams[a.name]=a.value}else{c.param.condParams[a.name]+=","+a.value}})},loading:function(){if(this.pagebar.$loading){this.pagebar.$loading.show()}},loaded:function(){if(this.pagebar.$loading){this.pagebar.$loading.hide()}},bindData:function(b){this._bindData(b)},toGetUrl:function(){var h=this;h._initConData();var e=h.getUrl?h.getUrl():h._getUrl();var g=$.extend({pagesize:h.pagebar.pagesize,pageno:h.pagebar.pageno},h.param.params,h.param.condParams);g=h.changeParams&&typeof(h.changeParams)=="function"?h.changeParams(g):g;var f=[];$.each(g||{},function(a,b){f.push(a+"="+encodeURIComponent(decodeURIComponent(b)))});return e+=(e.indexOf("?")?"&":"?")+f.join("&")},_getData:function(){var d=this;if(!d.res){return}d.sending=true;d.loading();d._initConData();var c=$.extend({pagesize:d.pagebar.pagesize,pageno:d.pagebar.pageno},d.param.params,d.param.condParams);c=d.changeParams&&typeof(d.changeParams)=="function"?d.changeParams(c):c;c._t=new Date().getTime();d.method(d.getUrl?d.getUrl():d._getUrl(),c,function(a){d._data=a;if(a.total!==undefined){d.pagebar.total=Math.max(a.total,a.data.length);d.pagebar.pagenos=Math.ceil(Math.max(a.total,a.data.length)/d.pagebar.pagesize)||1}$.each(d._subtotal||{},function(b,f){d._subtotal[b]=0});d._bindData(a);d.sending=false})},_bindData:function(j){var f=this;var h={};$.extend(h,j);h.data=h[f.dataname]||h.data||h;if(h.data&&h.data.length>f.pagebar.pagesize){h.data=h.data.slice(f.pagebar.pagesize*(f.pagebar.pageno-1),f.pagebar.pagesize*f.pagebar.pageno)}if(f.changeData&&typeof(f.changeData)=="function"){f.changeData(h)}clearInterval(f._loadtime);f.bind&&typeof(f.bind)=="function"?f.bind(h):f._bind(h);if(f.pagebar){f.pagebar.loaded()}f._thead.find("[type=checkbox] :checkbox").removeAttr("checked");for(var g=0;gf._table.parent().width()){f._table.parent().css("overflow-x","auto")}if(!f.resizable){f.resizable=f._table.resizableColumns({})}else{var i=f._table.data("resizableColumns");i.syncHandleWidths()}},_bindByTemplate:function(j){var k=this;var m=[];var l=k._template.clone(true);if(k.type=="tree"&&k.treefield){l.children().attr("parent_id","${parent_id}").attr("_id","${"+k.key+"}");var h=k._thead.find("th[name="+k.treefield+"]").index();var i=l.children().children().eq(h);i.attr("class","tree layer${layer}");i.html("

"+i.text()+"

")}var n=l.html();$.each(j.data||[],function(d,c){c.serial=d;c.layer=0;c.arrow=c.children?"arrow":"noarrow";var b=k._bindLine(c);var a=n.bind(b,k._data.dict);m.push(a.bind(j).clear("--"));if(c.children){m=m.concat(k._bindChildren(c.children,c.layer+1,n))}});return m},_bindLine:function(g,e){var h=this;if(h.total){h.total.total(g)}var f=$.extend({},h.param.params,g);$.each(h._fields||Object.keys(g),function(a,b){if(b&&b.type=="serial"){f.serial=(g.serial||0)+1+h.pagebar.pagesize*(h.pagebar.pageno-1)}else{var c=f[a]==undefined?"":f[a];if(h._subtotal&&h._subtotal[a]!==undefined){h._subtotal[a]+=parseFloat(c)||0}if(b.type=="time"||a.endsWith("time")){c=Util.formatDate(c,b.formatter)}else{if(b.type=="date"||a.endsWith("date")){c=Util.formatDate(c,b.formatter||"yyyy-MM-dd")}else{if(b.type=="number"){c=Util.formatNumber(c,b.formatter)}else{if(b.type=="checkbox"){c=h._checkbox.replace(/{value}/g,g[b.eq]).replace(/{key}/g,b.eq)}else{if(c instanceof Array&&c.length>0&&c[0] instanceof Array){var d=[];for(var i=0;i"):JSON.stringify(c)}}}}}f[a]=c}});return f},_bindNoTemplate:function(f){var d=this;var e=[];$.each(f.data||[],function(j,c){if(d.total){d.total.total(c)}var i=d._tr,b=[];c.serial=j;var a=d._bindLine(c);$.each(d._fields||Object.keys(c),function(h,l){var g=d.treefield==h?d._tdtree:d._td;b.push(g.replace(/{key}/g,h).replace(/{value}/g,a[h]).bind(a,d._data.dict))});e.push(i.replace("{td}",b.join("")).replace("${parent_id}",c.parent_id||"").replace("{id}",c[d.key]))});return e},_bindChildren:function(h,f,j){var i=this;if(!i.serial){i.serial=0}var g=[];j=j||i._template.html();$.each(h||[],function(m,d){d.layer=f||0;d.serial=i.serial++;d.arrow=d.children?"arrow":"noarrow";var a=i._bindLine(d,j);if(j){var b=j.bind(a,i._data.dict);g.push(b)}else{var e=i._tr.replace("{show}",d.layer==0?"":"reduce"),c=[];$.each(i._fields||Object.keys(d),function(k,l){var o=i.treefield==k?i._tdtree:i._td;c.push(o.replace(/{key}/g,k).replace(/{value}/g,a[k]).bind(a,i._data.dict))});g.push(e.replace("{td}",c.join("")).replace("${parent_id}",d.parent_id||"").replace("{id}",d[i.key]))}if(d.children){g=g.concat(i._bindChildren(d.children,d.layer+1,j))}});return g},_initTree:function(){var d=this;var f=function(b){var a=d._table.find("tr[parent_id='"+b+"']");a.removeClass("reduce")};var e=function(b){var a=d._table.find("tr[parent_id='"+b+"']");a.each(function(){e($(this).attr("_id"))});a.addClass("reduce");a.find(".spread").removeClass("spread")};d._table.find("span.arrow").on("click",{},function(a){var b=$(this);b.parent().toggleClass("spread");var c=b.parents("tr").attr("_id");b.parent().hasClass("spread")?f(c):e(c)})},_bind:function(r){var i=new Date().getTime();var n=this;if(n.total){if(n.pagebar.simple&&n.pagebar.pageno==1||!n.pagebar.simple){n.total.clear()}}if(!n._fields&&r.api){n.initHead(r.api,Object.keys(r.data[0])||Object.keys(r.data))}var p=n._template.length>0?n._bindByTemplate(r):n.type=="tree"?n._bindChildren(r.data):n._bindNoTemplate(r);n.serial=0;if(p.length==0){p.push(n._nodata.replace(/{massage}/g,r.massage||"找不到相关数据!"))}if(n.total){var t=[];$.each(n.total.options||{},function(b,c){var a=n._tr,d=[],e=0;$.each(n._fields||{},function(f,g){e++;if(c.total[f]!==undefined){if(d.length==0){d.push(""+c.title+"")}var h=c.total[f];if(g.type=="date"&&g.formatter){h=Util.formatDate(h,g.formatter)}else{if(g.type=="number"){h=Util.formatNumber(h,g.formatter)}}d.push(""+h+"")}else{if(d.length==0){return}else{d.push(" ")}}});t.push(a.replace("{td}",d.join("")))});n.total.tbody.html(t.join(""))}if(n._subtotal){var q=[],o=0;$.each(n._fields||{},function(c,a){o++;if(n._subtotal[c]!==undefined){if(q.length==0){q.push(""+n._subtotal_title[c]+"")}var b=n._subtotal[c];if(a.type=="date"&&a.formatter){b=Util.formatDate(b,a.formatter)}else{if(a.type=="number"){b=Util.formatNumber(b,a.formatter)}}q.push(""+b+"")}else{if(q.length==0){return}else{q.push(" ")}}});p.push(""+q.join("")+"")}if(n._average){var q=[],o=0;$.each(n._fields||{},function(c,a){o++;if(n._subtotal[c]!==undefined){if(q.length==0){q.push("平均值:")}var b=n._subtotal[c]/r.data.length;if(a.type=="date"&&a.formatter){b=Util.formatDate(b,a.formatter)}else{if(a.type=="number"){b=Util.formatNumber(b,a.formatter)}}q.push(""+b+"")}else{if(q.length==0){return}else{q.push(" ")}}});p.push(""+q.join("")+"")}if(n.showblankrow&&!n.pagebar.simple){var u=this._thead.children(":last-child").children().length;var q=[];for(s=0;s ")}q=q.join("");for(var s=(r.data||[]).length;s"+q+"")}}if(n.pagebar&&n.pagebar.simple){var v=document.body.clientHeight;n.pagebar.pageno==1?n._tbody.empty().html(p.join("")):n._tbody.append(p.join(""));n._tbody.find(">._loading").remove();var m=document.body.clientHeight;scrollTo(scrollX,scrollY+m-v)}else{n._tbody.empty().html(p.join(""))}n._tbody.find(">tr>td[name=checkbox]>:checkbox:not('input:disabled')").each(function(){$(this).unbind("change").change(function(){if(n.checkboxClick&&typeof(n.checkboxClick)=="function"){n.checkboxClick(this)}})});if(n.type=="tree"){n._initTree()}},spans:[],rowspan:function(f){var d=this,e;d._table.find(">tbody:eq(0)>tr").each(function(a){$("td:eq("+f+")",this).filter(":visible").each(function(b){if(e!=null&&$.trim($(this).text())&&$(this).text()==e.text()){e.attr("rowspan",(parseInt(e.attr("rowspan"))||1)+1);$(this).hide()}else{e=$(this)}})})}};Table.init=function(b){$("table[res]",b||document).each(function(){if($(this).attr("init")===false){return}Table.from(this)})};Table.from=function(c){if(typeof(c)=="string"){c=$("#"+c)[0]}var d=new Table(c);d.param.params=$.extend(d.param.params,$.getParams());if($(c).attr("auto")=="true"){d.go()}return d};Table.reload=function(b){if(b){if(typeof(b)=="string"){b=b.split(",")}$.each(b,function(e,f){var a=Table.tables[f];if(a){a.go()}});return}$.each(Table.tables||{},function(d,a){a.go()})};Table.method=function(b){$.each(b,function(a,d){Table[a]=d})};$(document).ready(function(){Table.init()});




© 2015 - 2024 Weber Informatics LLC | Privacy Policy