-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
157 lines (146 loc) · 5.96 KB
/
Copy pathscript.js
File metadata and controls
157 lines (146 loc) · 5.96 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
let tableSelect;
let columnSelect;
let columnInput;
let columnSortSelect;
let sortDirectionSelect;
//inits the program with variables and eventListeners
jQuery( document ).ready(function() {
tableSelect = document.getElementById("tableSelect");
if(tableSelect){
let getDataButton = document.getElementById("getDataButton");
let downloadButton = document.getElementById("downloadButton");
columnInput = document.getElementById("columnInput");
columnSelect = document.getElementById("columnSelect");
columnSortSelect = document.getElementById("columnSortSelect");
sortDirectionSelect = document.getElementById("sortDirectionSelect");
downloadButton.addEventListener('click', function(){
//only runs if a table is selected
if(tableSelect.value != -1){
downloadTableAjax(tableSelect.value, columnSelect.value, columnInput.value, columnSortSelect.value, sortDirectionSelect.value);
}
});
tableSelect.addEventListener('change', function(){
getTableAjax(tableSelect.value);
});
getDataButton.addEventListener('click', function(){
//only runs if a table is selected
if(tableSelect.value != -1){
filterByColumnAjax(tableSelect.value, columnSelect.value, columnInput.value, columnSortSelect.value, sortDirectionSelect.value, 1);
}
});
}
});
//gets database data depening on various user selected metrics
function filterByColumnAjax(tableName, columnName, columnValue, sortColumn, sortDirection, page) {
jQuery.post(
ajaxurl,
{
'action': 'filterTable',
'columnName': columnName,
'columnValue': columnValue,
'tableName': tableName,
'sortColumn': sortColumn,
'sortDirection': sortDirection,
'page': page,
'nonce': your_plugin_ajax_object.nonce
},
function (response) {
if (response.success) {
let tableRows = response.data.rows;
let pageAmount = response.data.totalPages;
createTableFromData(tableRows, pageAmount, page);
} else {
alert('Error: ' + response.data.message);
}
}
).fail(function (jqXHR, textStatus, errorThrown) {
alert('AJAX request failed: ' + textStatus + ', ' + errorThrown);
});
}
//creates a list of a tables columns, also populates html selects with the column names
function getTableAjax(pageName){
jQuery.post(
ajaxurl,
{
'action': 'getTable',
'pageName': pageName
},
function(response) {
//resets the html elements before adding new data to them
document.querySelector('.pagination').innerHTML = "";
columnSelect.innerHTML = '<select id="columnSelect"><option value="-1">No Filter</option>';
columnSortSelect.innerHTML = '<select id="columnSortSelect"><option value="-1">No Sort</option>';
let dataDiv = document.getElementById('dataDiv');
dataDiv.innerHTML = '<ul id="myUL">';
//adding the data
for(let i = 0; i < response.data.length; i++){
dataDiv.innerHTML += '<li>' + response.data[i] + '</li>';
columnSelect.innerHTML += `<option value="${response.data[i]}">${response.data[i]}</option>`;
columnSortSelect.innerHTML += `<option value="${response.data[i]}">${response.data[i]}</option>`;
}
dataDiv.innerHTML += '<ul>';
columnSelect.innerHTML += '</select>';
columnSortSelect.innerHTML += '</select>';
}
);
}
//sends an ajax request to download the html table as a csv file
function downloadTableAjax(tableName, columnName, columnValue, sortColumn, sortDirection){
jQuery.post(
ajaxurl,
{
'action': 'downloadTable',
'columnName': columnName,
'columnValue': columnValue,
'tableName' : tableName,
'sortColumn': sortColumn,
'sortDirection': sortDirection
},
function(response) {
location.reload();
}
);
}
function createTableFromData(tableData, pageAmount, page){
let dataDiv = document.getElementById('dataDiv');
//creating the html table from variable tableData
var tableHtml = '<table> <tr>';
//if tableData is empty, there is no data for that table
if (tableData.length === 0) {
tableHtml = '<p>No Data in Table</p>';
} else {
//the first row of tableData will be the names of the columns
Object.keys(tableData[0]).forEach(function(key) {
tableHtml += '<th>' + key + '</th>';
});
tableHtml += '</tr>';
//creates the main table rows
tableData.forEach(function(row) {
tableHtml += '<tr>';
Object.values(row).forEach(function(value) {
tableHtml += '<td><div style="max-height:50px; overflow:hidden">' + value + '</div></td>';
});
tableHtml += '</tr>';
});
tableHtml += '</table>';
}//end of table
//creating the pagination buttons
document.querySelector('.pagination').innerHTML = '';
for(let i = 1; i < pageAmount+1; i++){
var button = document.createElement('button');
button.addEventListener('click', function(e){
if(e.currentTarget.className !== 'pagination-button active'){
filterByColumnAjax(tableSelect.value, columnSelect.value, columnInput.value, columnSortSelect.value, sortDirectionSelect.value, i);
}
});
//the active page button gets the class 'active'
if(i === page){
button.className = 'pagination-button active';
}else{
button.className = 'pagination-button';
}
button.textContent = i.toString();
document.querySelector('.pagination').append(button);
}
dataDiv.innerHTML = tableHtml;
}