3 /* when a table gets paginated, displays context info */
4 function plc_table_paginator (opts,tablename) {
6 if(!("currentPage" in opts)) { return; }
8 var p = document.createElement('p');
9 var t = document.getElementById(tablename+'-fdtablePaginaterWrapTop');
10 var b = document.getElementById(tablename+'-fdtablePaginaterWrapBottom');
12 /* when there's no visible entry, the pagination code removes the wrappers */
13 if ( (!t) || (!b) ) return;
15 /* get how many entries are matching:
16 opts.visibleRows only holds the contents of the current page
17 so we store the number of matching entries in the tbody's classname
18 see plc_table_tbody_matching
20 var totalMatches = opts.totalRows;
21 var tbody=document.getElementById(tablename).getElementsByTagName("tbody")[0];
22 var cn=tbody.className;
23 if (cn.match (/matching-\d+/)) {
24 totalMatches=cn.match(/matching-\d+/)[0].replace("matching-","");
30 if (totalMatches != opts.totalRows) {
31 matches_text = totalMatches + "/" + opts.totalRows;
33 matches_text = opts.totalRows;
35 var first = ((opts.currentPage-1) * opts.rowsPerPage) +1;
36 var last = Math.min((opts.currentPage * opts.rowsPerPage),totalMatches);
37 var items_text = "Items [" + first + " - " + last + "] of " + matches_text;
38 var page_text = "Page " + opts.currentPage + " of " + Math.ceil(totalMatches / opts.rowsPerPage);
39 label = items_text + " -- " + page_text;
41 p.className = "paginationText";
42 p.appendChild(document.createTextNode(label));
44 /* t.insertBefore(p.cloneNode(true), t.firstChild); */
49 /* locates a table from its id and alters the classname to reflect new table size */
50 function plc_pagesize_set (table_id,size_id,def_size) {
51 var table=document.getElementById(table_id);
52 var size_area=document.getElementById(size_id);
53 if ( ! size_area.value ) {
54 size_area.value=def_size;
56 var size=size_area.value;
57 table.className=table.className.replace(/paginate-\d+/,"paginate-"+size);
58 tablePaginater.init(table_id);
61 function plc_pagesize_reset(table_id, size_id, size) {
62 var table=document.getElementById(table_id);
63 var size_area=document.getElementById(size_id);
65 table.className=table.className.replace(/paginate-\d+/,"paginate-"+size);
66 tablePaginater.init(table_id);
69 /* set or clear the ' invisibleRow' in the tr's classname, according to visible */
70 function plc_table_row_visible (row,visible) {
73 cn=cn.replace(" invisibleRow","");
74 if (! visible) cn += " invisibleRow";
78 /* maintain the number of matching entries in the <tbody> element's classname */
79 function plc_table_tbody_matching (tbody, matching) {
80 var new_cn="matching-" + matching;
81 var cn=tbody.className;
82 if (cn.match("matching-")) {
83 cn=cn.replace(/matching-\d+/,new_cn);
87 cn=cn.replace(/^ +/,"");
91 /* scan the table, and mark as visible
92 the rows that match (either AND or OR the patterns) */
93 function plc_table_filter (table_id,pattern_id,and_id) {
94 var tbody = document.getElementById(table_id).getElementsByTagName("tbody")[0];
96 var pattern_area = document.getElementById(pattern_id);
97 var pattern_text = pattern_area.value;
98 var row_index, row, cells, cell_index, cell, visible;
100 var matching_entries=0;
101 var and_button=document.getElementById(and_id);
102 var and_if_true=and_button.checked;
104 // remove whitespaces at the beginning and end
105 pattern_text = pattern_text.replace(/[ \t]+$/,"");
106 pattern_text = pattern_text.replace(/^[ \t]+/,"");
108 if (pattern_text.indexOf ("&") != -1) {
109 pattern_text = pattern_text.replace(/&$/,"").replace(/&/," ");
110 pattern_area.value=pattern_text;
111 and_button.checked=true;
113 } else if (pattern_text.indexOf ("|") != -1 ) {
114 pattern_text = pattern_text.replace(/\|$/,"").replace(/\|/," ");
115 pattern_area.value=pattern_text;
116 and_button.checked=false;
120 var patterns = pattern_text.split(" ");
122 for (row_index = 0; row=rows[row_index]; row_index++) {
126 if (patterns.length == 0) {
128 } else if (and_if_true) {
129 /* AND mode: all patterns must match */
131 for (i in patterns) {
132 var pattern_matched=false;
134 for (cell_index = 0; cell=cells[cell_index]; cell_index++) {
135 if ( cell.innerHTML.match(pattern)) {
136 pattern_matched=true;
137 // alert ('AND matched! p='+pattern+' c='+cell.innerHTML);
141 if ( ! pattern_matched ) visible=false;
144 /* OR mode: any match is good enough */
146 for (cell_index = 0; cell=cells[cell_index]; cell_index++) {
147 for (i in patterns) {
149 if (cell.innerHTML.match(pattern)) {
151 // alert ('OR matched! p='+pattern+' c='+cell.innerHTML);
157 plc_table_row_visible(row,visible);
158 if (visible) matching_entries +=1;
160 plc_table_tbody_matching(tbody,matching_entries);
161 tablePaginater.init(table_id);
164 function plc_table_filter_reset (table_id, pattern_id,and_id) {
166 document.getElementById(pattern_id).value="";
167 plc_table_filter (table_id, pattern_id,and_id);