1
0
Fork 0
mirror of https://github.com/koalyptus/TableFilter.git synced 2024-05-23 16:52:26 +02:00
TableFilter/dist/TF_Modules/tf_paging.js
2015-03-06 19:49:30 +11:00

1 line
12 KiB
JavaScript

TF.prototype.SetPaging=function(){if(!this.hasGrid&&!this.isFirstLoad)return;if(!this.paging||!this.isPagingRemoved&&!this.isFirstLoad)return;var e=this.fObj;this.pagingTgtId=e.paging_target_id!=undefined?e.paging_target_id:null,this.pagingLength=e.paging_length!=undefined?e.paging_length:10,this.resultsPerPageTgtId=e.results_per_page_target_id!=undefined?e.results_per_page_target_id:null,this.pgSlcCssClass=e.paging_slc_css_class!=undefined?e.paging_slc_css_class:"pgSlc",this.pgInpCssClass=e.paging_inp_css_class!=undefined?e.paging_inp_css_class:"pgNbInp",this.resultsSlcCssClass=e.results_slc_css_class!=undefined?e.results_slc_css_class:"rspg",this.resultsSpanCssClass=e.results_span_css_class!=undefined?e.results_span_css_class:"rspgSpan",this.btnNextPageText=e.btn_next_page_text!=undefined?e.btn_next_page_text:">",this.btnPrevPageText=e.btn_prev_page_text!=undefined?e.btn_prev_page_text:"<",this.btnLastPageText=e.btn_last_page_text!=undefined?e.btn_last_page_text:">|",this.btnFirstPageText=e.btn_first_page_text!=undefined?e.btn_first_page_text:"|<",this.btnNextPageHtml=e.btn_next_page_html!=undefined?e.btn_next_page_html:this.enableIcons?'<input type="button" value="" class="'+this.btnPageCssClass+' nextPage" title="Next page" />':null,this.btnPrevPageHtml=e.btn_prev_page_html!=undefined?e.btn_prev_page_html:this.enableIcons?'<input type="button" value="" class="'+this.btnPageCssClass+' previousPage" title="Previous page" />':null,this.btnFirstPageHtml=e.btn_first_page_html!=undefined?e.btn_first_page_html:this.enableIcons?'<input type="button" value="" class="'+this.btnPageCssClass+' firstPage" title="First page" />':null,this.btnLastPageHtml=e.btn_last_page_html!=undefined?e.btn_last_page_html:this.enableIcons?'<input type="button" value="" class="'+this.btnPageCssClass+' lastPage" title="Last page" />':null,this.pageText=e.page_text!=undefined?e.page_text:" Page ",this.ofText=e.of_text!=undefined?e.of_text:" of ",this.nbPgSpanCssClass=e.nb_pages_css_class!=undefined?e.nb_pages_css_class:"nbpg",this.hasPagingBtns=e.paging_btns==0?!1:!0,this.pagingBtnEvents=null,this.pageSelectorType=e.page_selector_type!=undefined?e.page_selector_type:this.fltTypeSlc,this.onBeforeChangePage=tf_IsFn(e.on_before_change_page)?e.on_before_change_page:null,this.onAfterChangePage=tf_IsFn(e.on_after_change_page)?e.on_after_change_page:null;var t=this.refRow,n=this.nbRows;this.nbPages=Math.ceil((n-t)/this.pagingLength);if(!this.Evt._Paging.next){var r=this;this.Evt._Paging={slcIndex:function(){return r.pageSelectorType==r.fltTypeSlc?r.pagingSlc.options.selectedIndex:parseInt(r.pagingSlc.value)-1},nbOpts:function(){return r.pageSelectorType==r.fltTypeSlc?parseInt(r.pagingSlc.options.length)-1:r.nbPages-1},next:function(){r.Evt._Paging.nextEvt&&r.Evt._Paging.nextEvt();var e=r.Evt._Paging.slcIndex()<r.Evt._Paging.nbOpts()?r.Evt._Paging.slcIndex()+1:0;r.ChangePage(e)},prev:function(){r.Evt._Paging.prevEvt&&r.Evt._Paging.prevEvt();var e=r.Evt._Paging.slcIndex()>0?r.Evt._Paging.slcIndex()-1:r.Evt._Paging.nbOpts();r.ChangePage(e)},last:function(){r.Evt._Paging.lastEvt&&r.Evt._Paging.lastEvt(),r.ChangePage(r.Evt._Paging.nbOpts())},first:function(){r.Evt._Paging.firstEvt&&r.Evt._Paging.firstEvt(),r.ChangePage(0)},_detectKey:function(e){var t=e?e:window.event?window.event:null;if(t){var n=t.charCode?t.charCode:t.keyCode?t.keyCode:t.which?t.which:0;n=="13"&&(r.sorted?(r.Filter(),r.ChangePage(r.Evt._Paging.slcIndex())):r.ChangePage(),this.blur())}},nextEvt:null,prevEvt:null,lastEvt:null,firstEvt:null}}this.Evt._OnSlcPagesChange||(this.Evt._OnSlcPagesChange=function(){r.Evt._Paging._OnSlcPagesChangeEvt&&r.Evt._Paging._OnSlcPagesChangeEvt(),r.ChangePage(),this.blur(),this.parentNode&&tf_isIE&&this.parentNode.focus()});if(this.pageSelectorType==this.fltTypeSlc){var i=tf_CreateElm(this.fltTypeSlc,["id",this.prfxSlcPages+this.id]);i.className=this.pgSlcCssClass,i.onchange=this.Evt._OnSlcPagesChange}if(this.pageSelectorType==this.fltTypeInp){var i=tf_CreateElm(this.fltTypeInp,["id",this.prfxSlcPages+this.id],["value",this.currentPageNb]);i.className=this.pgInpCssClass,i.onkeypress=this.Evt._Paging._detectKey}var s,o,u,a;s=tf_CreateElm("span",["id",this.prfxBtnNextSpan+this.id]),o=tf_CreateElm("span",["id",this.prfxBtnPrevSpan+this.id]),u=tf_CreateElm("span",["id",this.prfxBtnLastSpan+this.id]),a=tf_CreateElm("span",["id",this.prfxBtnFirstSpan+this.id]);if(this.hasPagingBtns){if(this.btnNextPageHtml==null){var f=tf_CreateElm(this.fltTypeInp,["id",this.prfxBtnNext+this.id],["type","button"],["value",this.btnNextPageText],["title","Next"]);f.className=this.btnPageCssClass,f.onclick=this.Evt._Paging.next,s.appendChild(f)}else s.innerHTML=this.btnNextPageHtml,s.onclick=this.Evt._Paging.next;if(this.btnPrevPageHtml==null){var l=tf_CreateElm(this.fltTypeInp,["id",this.prfxBtnPrev+this.id],["type","button"],["value",this.btnPrevPageText],["title","Previous"]);l.className=this.btnPageCssClass,l.onclick=this.Evt._Paging.prev,o.appendChild(l)}else o.innerHTML=this.btnPrevPageHtml,o.onclick=this.Evt._Paging.prev;if(this.btnLastPageHtml==null){var c=tf_CreateElm(this.fltTypeInp,["id",this.prfxBtnLast+this.id],["type","button"],["value",this.btnLastPageText],["title","Last"]);c.className=this.btnPageCssClass,c.onclick=this.Evt._Paging.last,u.appendChild(c)}else u.innerHTML=this.btnLastPageHtml,u.onclick=this.Evt._Paging.last;if(this.btnFirstPageHtml==null){var h=tf_CreateElm(this.fltTypeInp,["id",this.prfxBtnFirst+this.id],["type","button"],["value",this.btnFirstPageText],["title","First"]);h.className=this.btnPageCssClass,h.onclick=this.Evt._Paging.first,a.appendChild(h)}else a.innerHTML=this.btnFirstPageHtml,a.onclick=this.Evt._Paging.first}this.pagingTgtId==null&&this.SetTopDiv();var p=this.pagingTgtId==null?this.mDiv:tf_Id(this.pagingTgtId);p.innerHTML!=""&&(p.innerHTML=""),p.appendChild(a),p.appendChild(o);var d=tf_CreateElm("span",["id",this.prfxPgBeforeSpan+this.id]);d.appendChild(tf_CreateText(this.pageText)),d.className=this.nbPgSpanCssClass,p.appendChild(d),p.appendChild(i);var v=tf_CreateElm("span",["id",this.prfxPgAfterSpan+this.id]);v.appendChild(tf_CreateText(this.ofText)),v.className=this.nbPgSpanCssClass,p.appendChild(v);var m=tf_CreateElm("span",["id",this.prfxPgSpan+this.id]);m.className=this.nbPgSpanCssClass,m.appendChild(tf_CreateText(" "+this.nbPages+" ")),p.appendChild(m),p.appendChild(s),p.appendChild(u),this.pagingSlc=tf_Id(this.prfxSlcPages+this.id),(!this.rememberGridValues||this.isPagingRemoved)&&this.SetPagingInfo(),this.fltGrid||(this.ValidateAllRows(),this.SetPagingInfo(this.validRowsIndex)),this.pagingBtnEvents=this.Evt._Paging,this.isPagingRemoved=!1},TF.prototype.RemovePaging=function(){if(!this.hasGrid)return;if(this.pagingSlc==null)return;var e,t,n,r,i,s,o;e=tf_Id(this.prfxBtnNextSpan+this.id),t=tf_Id(this.prfxBtnPrevSpan+this.id),n=tf_Id(this.prfxBtnLastSpan+this.id),r=tf_Id(this.prfxBtnFirstSpan+this.id),i=tf_Id(this.prfxPgBeforeSpan+this.id),s=tf_Id(this.prfxPgAfterSpan+this.id),o=tf_Id(this.prfxPgSpan+this.id),this.pagingSlc.parentNode.removeChild(this.pagingSlc),e!=null&&e.parentNode.removeChild(e),t!=null&&t.parentNode.removeChild(t),n!=null&&n.parentNode.removeChild(n),r!=null&&r.parentNode.removeChild(r),i!=null&&i.parentNode.removeChild(i),s!=null&&s.parentNode.removeChild(s),o!=null&&o.parentNode.removeChild(o),this.pagingBtnEvents=null,this.pagingSlc=null,this.isPagingRemoved=!0},TF.prototype.SetPagingInfo=function(e){var t=this.tbl.rows,n=this.pagingTgtId==null?this.mDiv:tf_Id(this.pagingTgtId),r=tf_Id(this.prfxPgSpan+this.id);if(e!=undefined)this.validRowsIndex=e;else{this.validRowsIndex=[];for(var i=this.refRow;i<this.nbRows;i++){if(!t[i])continue;var s=t[i].getAttribute("validRow");(s=="true"||s==null)&&this.validRowsIndex.push(i)}}this.nbPages=Math.ceil(this.validRowsIndex.length/this.pagingLength),r.innerHTML=this.nbPages,this.pageSelectorType==this.fltTypeSlc&&(this.pagingSlc.innerHTML="");if(this.nbPages>0){n.style.visibility="visible";if(this.pageSelectorType==this.fltTypeSlc)for(var o=0;o<this.nbPages;o++){var u=new Option(o+1,o*this.pagingLength,!1,!1);this.pagingSlc.options[o]=u}else this.pagingSlc.value=this.currentPageNb}else n.style.visibility="hidden";this.GroupByPage(this.validRowsIndex)},TF.prototype.GroupByPage=function(e){var t=this.tbl.rows,n=parseInt(this.startPagingRow)+parseInt(this.pagingLength);e!=undefined&&(this.validRowsIndex=e);for(h=0;h<this.validRowsIndex.length;h++)if(h>=this.startPagingRow&&h<n){var r=t[this.validRowsIndex[h]];if(r.getAttribute("validRow")=="true"||r.getAttribute("validRow")==undefined)r.style.display="";this.alternateBgs&&this.SetRowBg(this.validRowsIndex[h],h)}else t[this.validRowsIndex[h]].style.display="none",this.alternateBgs&&this.RemoveRowBg(this.validRowsIndex[h]);this.nbVisibleRows=this.validRowsIndex.length,this.isStartBgAlternate=!1,this.ApplyGridProps()},TF.prototype.SetPage=function(e){if(this.hasGrid&&this.paging){var t=this.pagingBtnEvents,n=typeof e;if(n=="string")switch(e.tf_LCase()){case"next":t.next();break;case"previous":t.prev();break;case"last":t.last();break;case"first":t.first();break;default:t.next()}n=="number"&&this.ChangePage(e-1)}},TF.prototype.SetResultsPerPage=function(){if(!this.hasGrid&&!this.isFirstLoad)return;if(this.resultsPerPageSlc!=null||this.resultsPerPage==null)return;if(!this.Evt._OnSlcResultsChange){var e=this;this.Evt._OnSlcResultsChange=function(){e.ChangeResultsPerPage(),this.blur(),this.parentNode&&tf_isIE&&this.parentNode.focus()}}var t=tf_CreateElm(this.fltTypeSlc,["id",this.prfxSlcResults+this.id]);t.className=this.resultsSlcCssClass;var n=this.resultsPerPage[0],r=this.resultsPerPage[1],i=tf_CreateElm("span",["id",this.prfxSlcResultsTxt+this.id]);i.className=this.resultsSpanCssClass,this.resultsPerPageTgtId==null&&this.SetTopDiv();var s=this.resultsPerPageTgtId==null?this.rDiv:tf_Id(this.resultsPerPageTgtId);i.appendChild(tf_CreateText(n)),s.appendChild(i),s.appendChild(t),this.resultsPerPageSlc=tf_Id(this.prfxSlcResults+this.id);for(var o=0;o<r.length;o++){var u=new Option(r[o],r[o],!1,!1);this.resultsPerPageSlc.options[o]=u}t.onchange=this.Evt._OnSlcResultsChange},TF.prototype.RemoveResultsPerPage=function(){if(!this.hasGrid)return;if(this.resultsPerPageSlc==null||this.resultsPerPage==null)return;var e,t;e=this.resultsPerPageSlc,t=tf_Id(this.prfxSlcResultsTxt+this.id),e!=null&&e.parentNode.removeChild(e),t!=null&&t.parentNode.removeChild(t),this.resultsPerPageSlc=null},TF.prototype.ChangePage=function(e){this.EvtManager(this.Evt.name.changepage,{pgIndex:e})},TF.prototype._ChangePage=function(e){if(!this.paging)return;e==undefined&&(e=this.pageSelectorType==this.fltTypeSlc?this.pagingSlc.options.selectedIndex:this.pagingSlc.value-1),e>=0&&e<=this.nbPages-1&&(this.onBeforeChangePage&&this.onBeforeChangePage.call(null,this,e),this.currentPageNb=parseInt(e)+1,this.pageSelectorType==this.fltTypeSlc?this.pagingSlc.options[e].selected=!0:this.pagingSlc.value=this.currentPageNb,this.rememberPageNb&&this.RememberPageNb(this.pgNbCookie),this.startPagingRow=this.pageSelectorType==this.fltTypeSlc?this.pagingSlc.value:e*this.pagingLength,this.GroupByPage(),this.onAfterChangePage&&this.onAfterChangePage.call(null,this,e))},TF.prototype.ChangeResultsPerPage=function(){this.EvtManager(this.Evt.name.changeresultsperpage)},TF.prototype._ChangeResultsPerPage=function(){if(!this.paging)return;var e=this.resultsPerPageSlc,t=this.pageSelectorType==this.fltTypeSlc?this.pagingSlc.selectedIndex:parseInt(this.pagingSlc.value-1);this.pagingLength=parseInt(e.options[e.selectedIndex].value),this.startPagingRow=this.pagingLength*t;if(!isNaN(this.pagingLength)){this.startPagingRow>=this.nbFilterableRows&&(this.startPagingRow=this.nbFilterableRows-this.pagingLength),this.SetPagingInfo();if(this.pageSelectorType==this.fltTypeSlc){var n=this.pagingSlc.options.length-1<=t?this.pagingSlc.options.length-1:t;this.pagingSlc.options[n].selected=!0}this.rememberPageLen&&this.RememberPageLength(this.pgLenCookie)}},TF.prototype.ResetPage=function(e){this.EvtManager(this.Evt.name.resetpage)},TF.prototype._ResetPage=function(e){var t=tf_ReadCookie(e);t!=""&&this.ChangePage(t-1)},TF.prototype.ResetPageLength=function(e){this.EvtManager(this.Evt.name.resetpagelength)},TF.prototype._ResetPageLength=function(e){if(!this.paging)return;var t=tf_ReadCookie(e);t!=""&&(this.resultsPerPageSlc.options[t].selected=!0,this.ChangeResultsPerPage())},TF.prototype.AddPaging=function(e){if(!this.hasGrid||this.paging)return;this.paging=!0,this.isPagingRemoved=!0,this.SetPaging(),this.ResetValues(),e&&this.Filter()};