-
Notifications
You must be signed in to change notification settings - Fork 0
/
hideableColumns.php
131 lines (118 loc) · 3.53 KB
/
hideableColumns.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
<?php
/**
* Make Columns in Data Table hideable
* ===================================
* This plugin helps working with tables with a lot of colums. Clicking the header of a column while holding the ALT-key hides the column.
* Columns are still hidden when using the form above the table (search or order) or flipping the page.
*
* You can define columns that should not be hideable when enabling the plugin.
* $plugins = array(
* new hideableColumns(array('ID','userName')) //define columns that may not be hidden.
* );
*
* Written in vanilla JavaScript, no jquery needed.
*
* Tested with Adminer 4.8.1 in FireFox 104.
*
* NOTE: This is not a security feature! The full table is loaded in any case, hiding is done in Javascript.
*
* @author Stephan Herrmann, https://github.com/derStephan/AdminerPlugins
* @license MIT
*
*/
class hideableColumns
{
//will hold a list of all columns that will be hidden
private $columnsToHide=array();
//filter columns that may not be hidden.
public function __construct ($unHideableColums=array())
{
if(isset($_GET["hide"]))
{
//filter URL for columns to hide.
foreach($_GET["hide"] as $columnToHide)
{
if(!in_array($columnToHide, $unHideableColums))
$this->columnsToHide[]=$columnToHide;
}
}
}
public function head()
{
//all of this is only applicable in the simple table view
if(!isset($_GET["select"]))
return;
?>
<script <?php echo nonce()?> type='text/javascript'>
//make columns hideable
function makeColumnsHideable()
{
//register click-events on Links above the columns
for (let span of document.querySelectorAll('#table thead th span' ) )
{
span.addEventListener('click', headerSpanClick);
}
<?php
//do actual hiding for each column seperately
foreach($this->columnsToHide as $columnToHide)
{
?>
//hide content
for (let cell of document.querySelectorAll('#table tbody td[id$="[<?php echo $columnToHide ?>]"' ) )
{
cell.style.display="none";
}
//hide header
for (let span of document.querySelectorAll('#table thead th span' ) )
{
if(span.innerText=="<?php echo $columnToHide ?>")
{
span.parentElement.parentElement.style.display="none";
}
}
//add column to the form above the table so this will be kept while searching or ordering
var newHideField=document.getElementById("form")[0].cloneNode();
newHideField.name="hide[]";
newHideField.value="<?php echo $columnToHide ?>";
document.getElementById("form").appendChild(newHideField);
<?php
}
?>
//hide content of rows that are appended later
//whenever something is added to the table
document.getElementById("table").addEventListener('DOMNodeInserted', function(e)
{
//walk through all new rows
for (let rows of e.target.childNodes )
{
//walk through all cells in all new row
for (let cell of rows.childNodes )
{
<?php
//do actual hiding for each column seperately
foreach($this->columnsToHide as $columnToHide)
{
?>
if(cell.id.indexOf("[<?php echo $columnToHide ?>]")>0)
cell.style.display="none";
<?php
}
?>
}
}
});
}
document.addEventListener('DOMContentLoaded', makeColumnsHideable);
//react to click on Heading while holding Alt-Key.
function headerSpanClick(e)
{
if(e.altKey)
{
e.preventDefault();
window.location.href = '?<?php echo $_SERVER["QUERY_STRING"]?>&hide[]='+event.target.innerText;
}
}
</script>
<?php
}
}