-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathData_Table.php
More file actions
225 lines (177 loc) · 6.3 KB
/
Data_Table.php
File metadata and controls
225 lines (177 loc) · 6.3 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
####################################################
# Data Table Class #
# ------------------------------------------------ #
# This class creates a data table that can be #
# easily manipulated and sorted. I have intentions #
# of extending this class later to allow exporting #
# of any table to an excel (xls) document. #
# #
# This class makes use of my DOMe class. #
# #
# ------------------------------------------------ #
# CREATED BY: Thomas Eynon #
# www.thomaseynon.com #
# CREATED ON: 1/18/2012 #
####################################################
require "../DOMe/DOMe.php";
class Data_Table {
public $table, $thead, $tbody, $tr, $th, $td;
public $rows, $row, $columns, $column, $cells, $autoFill, $rowLimit, $id, $headerRepeat;
function __construct($id = NULL, $autoFill = true) {
$this->table = new DOMe("table");
// Initialize to an empty table.
$this->thead = NULL;
$this->tbody = NULL;
$this->tr = array();
$this->th = array();
$this->td = array();
// Track the total rows, columns, and cells in the table.
$this->row = 0;
$this->rows = 0;
$this->columns = 0;
$this->cells = 0;
// By default, header never repeats.
$this->headerRepeat = 0;
if ($id == NULL) {
// Create a random name.
$id = "datatable_";
$string = "abcdefghijklmnopqrstuvwxyz0123456789";
for ($i = 0; $i < 15; $i++) {
$id .= $string[rand(0, strlen($string) -1)];
}
}
$this->id = $id;
// When adding rows, should we create cells even if the columns weren't specified? Default is false.
// - The table column count will be defined by the first row added unless set by the user.
// So if the column count is 4 and we add a row that only specifies 3 columns, should we still
// build the table cell? (td)
$this->autoFill = $autoFill;
}
// Headers should always come before the tbody.
function addHeader($data) {
// Create the thead
$this->thead = new DOMe("thead");
if ($this->tbody != NULL) {
$this->table->insertBefore($this->tbody, $this->thead);
}
else {
$this->table->assign($this->thead);
}
// Define a header row.
$this->tr[$this->row] = &$this->thead->newChild("tr");
// Assign the data to a cell.
foreach ($data as $key => $value) {
$this->th[$this->row][$key] = &$this->tr[$this->row]->newChild("th", $value);
}
$this->columns = count($data);
++$this->rows;
$this->cells += $this->columns;
return $this->row++;
}
function addRow($data) {
// Create the tbody if it doesn't already exist.
if ($this->tbody == NULL) {
$this->tbody = &$this->table->newChild("tbody");
}
// Define a data row.
$this->tr[$this->row] = &$this->tbody->newChild("tr");
// Assign the data to a cell.
foreach ($data as $key => $value) {
$this->td[$this->row][$key] = &$this->tr[$this->row]->newChild("td", $value);
}
if (count($data) < $this->columns && $this->autoFill === true) {
// Create the rest of the cells.
for ($i = count($data); $i < $this->columns; $i++) {
$this->td[$this->row][$key] = $this->tr[$this->row]->newChild("td");
}
}
$this->rows++;
$this->cells += $this->columns;
return $this->row++;
}
function generate() {
// Copy the DOM Object so we can modify it for output.
$output = $this->table;
// See if the header repeats and a header exists.
if ($this->headerRepeat != 0 && is_int($this->headerRepeat) && (count($this->th) > 0)) {
// Get the row for the header.
$keys = array_keys($this->th);
// Find first tbody element
$tbody = $output->getElementByTagName("tbody");
// Loop through the DOM and insert headers every N rows.
for ($x = $this->headerRepeat; $x < count($tbody); $x = $x + $this->headerRepeat) {
$headerNode[$x] = $this->tr[$keys[0]]->copy();
$node = &$tbody->child($x);
$tbody->insertBefore($node, $headerNode[$x]);
$x++;
}
}
return $output->generate(false);
}
function sort($headerKey) {
// We can only sort if there is only one header and the specified key exists.
if (count($this->th) == 1) {
// Get the row number.
foreach ($this->th as $row => $cell) {
if (isset($this->th[$row][$headerKey])) {
// Now we can sort the table.
$sortValues = array();
// Pull the data from the specified column into an array.
foreach ($this->td as $row => $value) {
$sortValues[$row] = $this->td[$row][$headerKey]->text;
}
// Sort the data.
asort($sortValues);
// Build a new tbody
$tbody = new DOMe("tbody");
// Loop through the array and rebuild the tbody.
foreach ($sortValues as $row => $value) {
// Assign the row to the tbody
$tbody->assign($this->tr[$row]);
}
// Remove the current tbody.
//$this->table->remove($this->tbody);
// Reset the tbody to the new sorted tbody.
$this->tbody = $tbody;
// Add the tbody to the table.
//$this->table->assign($this->tbody);
}
}
}
}
function rsort($headerKey) {
// We can only sort if there is only one header and the specified key exists.
if (count($this->th) == 1) {
// Get the row number.
foreach ($this->th as $row => $cell) {
if (isset($this->th[$row][$headerKey])) {
// Now we can sort the table.
$sortValues = array();
// Pull the data from the specified column into an array.
foreach ($this->td as $row => $value) {
$sortValues[$row] = $this->td[$row][$headerKey]->text;
}
// Sort the data.
arsort($sortValues);
// Build a new tbody
$tbody = new DOMe("tbody");
// Loop through the array and rebuild the tbody.
foreach ($sortValues as $row => $value) {
// Assign the row to the tbody
$tbody->assign($this->tr[$row]);
}
// Remove the current tbody.
//$this->table->remove($this->tbody);
// Reset the tbody to the new sorted tbody.
$this->tbody = $tbody;
// Add the tbody to the table.
//$this->table->assign($this->tbody);
}
}
}
}
// Imports a valid DOM Table Node
function importDOM($table) {
}
}