-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsortableTable.php
executable file
·145 lines (115 loc) · 3.14 KB
/
sortableTable.php
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
<?php
require_once('helper.php');
require_once('header.php');
class SortableTable {
private $headers;
private $row_count;
function __construct() {
$this->headers = array();
$this->row_count = 0;
}
public function add_header($name) {
if(!$this->header_exists($name)) {
array_push($this->headers,new Header($this->string_encode($name)));
}
}
public function add_row($row) {
foreach($row as $head => $value) {
$this->add_header($head);
$h = $this->get_header($head);
$h->add_data($this->string_encode($value), $this->row_count);
}
$this->row_count++;
}
private function header_exists($name) {
if(is_null($this->get_header($name))) {
return false;
}
return true;
}
private function get_header($name) {
foreach($this->headers as $head) {
if($head->get_name() == $name) {
return $head;
}
}
return null;
}
private function string_encode($string) {
return str_replace("\\\\\"", "\\\"", str_replace("\\\"", "\"", $string));
}
/****** Generate Data Set ******/
private function generate_data() {
$s = "";
$s .= "var data = [\n";
$array = array();
for($i = 0; $i < $this->row_count; $i++) {
array_push($array, $this->generate_data_row($i));
}
$s .= comma_seperate($array, true);
$s .= "\n];";
return $s;
}
private function generate_data_row($id) {
$array = array();
foreach($this->headers as $h) {
array_push($array, "\"".$h->get_name()."\"".": \"".$h->get_data($id)."\"");
}
$s = "{\n".comma_seperate($array, true)."\n}";
return $s;
}
/****** Generate Columns ******/
private function generate_cols() {
$array = array();
foreach($this->headers as $h) {
$string = "{key: '".$h->get_name()."', label: '".$h->get_name()."', sortable:true, allowHTML:true}";
array_push($array, $string);
}
$s = "";
$s .= "var cols = [\n";
$s .= comma_seperate($array, true);
$s .= "\n];\n";
return $s;
}
/****** Add Included Scripts ******/
private function add_scripts() {
$s = "<script src='http://yui.yahooapis.com/3.13.0/build/yui/yui-min.js'></script>";
return $s;
}
/****** Generate Table Script ******/
private function generate_table_script($table_id) {
$s = "";
$s .= "var dt = new Y.DataTable({\n";
$s .= " data: data,\n";
$s .= " columns: cols,\n";
//$s .= " scrollable: 'x',\n";
//$s .= " scrollable: true,\n";
$s .= " width: '100%',\n";
//$s .= " height: '250px'\n";
$s .= "});\n\n";
$s .= "dt.render('#$table_id');\n";
return $s;
}
/****** Generate Load Script ******/
private function generate_load_script($table_id) {
$s = "";
$s .= "YUI().use('datatable', 'datatable-scroll', 'datatype-number-format', function (Y) {\n";
$s .= $this->generate_data() . "\n";
$s .= $this->generate_cols() . "\n";
$s .= $this->generate_table_script($table_id);
$s .= "});";
return $s;
}
/****** Print Table ******/
public function print_table($table_id = "dtable") {
echo $this->get_table($table_id);
}
public function get_table($table_id = "dtable") {
$s = "";
//$s .= $this->add_scripts() . "\n";
$s .= "<script>\n";
$s .= $this->generate_load_script($table_id);
$s .= "</script>";
return $s;
}
}