tablefilter_wev8.js
4.42 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Attach the filter to a table. filterRow specifies the rownumber at which the filter should be inserted.
function attachFilter(table, filterRow)
{
table.filterRow = filterRow;
// Check if the table has any rows. If not, do nothing
if(table.rows.length > 0)
{
// Insert the filterrow and add cells whith drowdowns.
var filterRow = table.insertRow(table.filterRow);
for(var i = 0; i < table.rows[table.filterRow + 1].cells.length; i++)
{
var c = document.createElement("TH");
table.rows[table.filterRow].appendChild(c);
var opt = document.createElement("select");
opt.style.width="100%"
opt.onchange = filter;
c.appendChild(opt);
}
// Set the functions
table.fillFilters = fillFilters;
table.inFilter = inFilter;
table.buildFilter = buildFilter;
table.showAll = showAll;
table.detachFilter = detachFilter;
table.filterElements = new Array();
// Fill the filters
table.fillFilters();
table.filterEnabled = true;
}
}
function detachFilter()
{
if(this.filterEnabled)
{
// Remove the filter
this.showAll();
this.deleteRow(this.filterRow);
this.filterEnabled = false;
}
}
// Checks if a column is filtered
function inFilter(col)
{
for(var i = 0; i < this.filterElements.length; i++)
{
if(this.filterElements[i].index == col)
return true;
}
return false;
}
// Fills the filters for columns which are not fiiltered
function fillFilters()
{
for(var col = 0; col < this.rows[this.filterRow].cells.length; col++)
{
if(!this.inFilter(col))
{
this.buildFilter(col, "(all)");
}
}
}
// Fills the columns dropdown box.
// setValue is the value which the dropdownbox should have one filled.
// If the value is not suplied, the first item is selected
function buildFilter(col, setValue)
{
// Get a reference to the dropdownbox.
var opt = this.rows[this.filterRow].cells[col].firstChild;
// remove all existing items
while(opt.length > 0)
opt.remove(0);
var values = new Array();
// put all relevant strings in the values array.
for(var i = this.filterRow + 1; i < this.rows.length; i++)
{
var row = this.rows[i];
if(row.style.display != "none" && row.className != "noFilter")
{
values.push(row.cells[col].innerHTML.toLowerCase());
}
}
values.sort();
//add each unique string to the dopdownbox
var value;
for(var i = 0; i < values.length; i++)
{
if(values[i].toLowerCase() != value)
{
value = values[i].toLowerCase();
opt.options.add(new Option(values[i], value));
}
}
opt.options.add(new Option("(全部)", "(all)"), 0);
if(setValue != undefined)
opt.value = setValue;
else
opt.options[0].selected = true;
}
// This function is called when a dropdown box changes
function filter()
{
var table = this; // 'this' is a reference to the dropdownbox which changed
while(table.tagName.toUpperCase() != "TABLE")
table = table.parentNode;
var filterIndex = this.parentNode.cellIndex; // The column number of the column which should be filtered
var filterText = table.rows[table.filterRow].cells[filterIndex].firstChild.value;
// First check if the column is allready in the filter.
var bFound = false;
for(var i = 0; i < table.filterElements.length; i++)
{
if(table.filterElements[i].index == filterIndex)
{
bFound = true;
// If the new value is '(all') this column is removed from the filter.
if(filterText == "(all)")
{
table.filterElements.splice(i, 1);
}
else
{
table.filterElements[i].filter = filterText;
}
break;
}
}
if(!bFound)
{
// the column is added to the filter
var obj = new Object();
obj.filter = filterText;
obj.index = filterIndex;
table.filterElements.push(obj);
}
// first set all rows to be displayed
table.showAll();
// the filter ou the right rows.
for(var i = 0; i < table.filterElements.length; i++)
{
// First fill the dropdown box for this column
table.buildFilter(table.filterElements[i].index, table.filterElements[i].filter);
// Apply the filter
for(var j = table.filterRow + 1; j < table.rows.length; j++)
{
var row = table.rows[j];
if(table.style.display != "none" && row.className != "noFilter")
{
if(table.filterElements[i].filter != row.cells[table.filterElements[i].index].innerHTML.toLowerCase())
{
row.style.display = "none";
}
}
}
}
// Fill the dropdownboxes for the remaining columns.
table.fillFilters();
}
function showAll()
{
for(var i = this.filterRow + 1; i < this.rows.length; i++)
{
this.rows[i].style.display = "";
}
}