1 // This is for displaying brapi paginated responses in a table format. The brapi response should be paginated over response.result.data and pagination info should be in response.metadata.pagination
3 // Pass a js object with the headers and data as key:values, such as:
5 // {"col1":"col1row1value", "height":"12", "age":"900"},
6 // {"col1":"col1row2value", "height":"1", "age":"9"},
7 // {"col1":"col1row3value", "height":"2", "age":"99"}
10 // as well as a pagination js object, such as:
11 // {"currentPage": "2", "pageSize": "200", "totalCount": "999", "totalPages":"5"}
13 // as well as the id for the div that you want the table to appear on the web page, e.g. "brapi_map_list_table"
14 // as well as a return url for processing changing currentPage and pageSize
15 // as well as optionally, a link js object, which will be used to construct links in the table. e.g. { "name": ["mapId", "/maps/protocols/"] }
18 function brapi_create_table(data, pagination, div_id, return_url, link, params) {
20 console.log(pagination);
22 console.log(return_url);
25 var current_page = pagination.currentPage;
26 var next_page = current_page + 1;
27 var previous_page = current_page - 1;
28 var page_size = pagination.pageSize;
29 var total_count = pagination.totalCount;
30 var total_pages = pagination.totalPages;
31 var html = "<input id='table_div_id_input' type='hidden' value='"+div_id+"'/>";
32 var html = html+"<input id='table_return_url_input' type='hidden' value='"+return_url+"'/>";
33 if (checkDefined(link) == 1) {
34 var html = html+"<input id='table_links_input' type='hidden' value='"+JSON.stringify(link)+"'/>";
36 var html = html+"<input id='table_links_input' type='hidden' value='"+link+"'/>";
38 var html = html+"<input id='table_page_size_input' type='hidden' value='"+page_size+"'/>";
39 var html = html+"<input id='table_next_page_input' type='hidden' value='"+next_page+"'/>";
40 var html = html+"<input id='table_previous_page_input' type='hidden' value='"+previous_page+"'/>";
41 if (data.length == 0) {
42 html = html+"<center><h3>No data available!</h3></center>";
44 html = html+"<table class='table table-hover table-bordered'><thead><tr>";
46 for(var h in data[0]) {
47 if (data[0].hasOwnProperty(h)) {
49 if (checkDefined(link) == 0) {
52 for(var link_header in link) {
53 if(h != link[link_header][0]) {
61 for(var col=0; col<header.length; col++){
62 html = html+"<th>"+capitalizeEachWord(header[col])+"</th>";
64 html = html+"</tr></thead><tbody>";
65 for(var row=0; row<data.length; row++) {
67 for(var col=0; col<header.length; col++){
68 if (checkDefined(link) == 0) {
69 html = html+"<td>"+data[row][header[col]]+"</td>";
71 for(var link_header in link) {
72 if (header[col] == link_header) {
73 html = html+"<td><a href='"+link[link_header][1]+data[row][link[link_header][0]]+"'>"+data[row][link_header]+"</a></td>";
75 if (data[row][header[col]].length == 0) {
76 html = html+"<td>"+data[row][header[col]]+"</td>";
84 html = html+"</tbody></table>";
86 html = html+"<div class='well well-sm'><div class='row'>";
87 html = html+"<div class='col-sm-6'><div class='row'><div class='col-sm-7'><div class='btn-group' role='group'>";
89 if (total_pages > 1) {
90 if (current_page > 1) {
91 html = html+"<button id='table_previous_page_button' class='btn btn-sm btn-default glyphicon glyphicon-arrow-left'></button><button class='btn btn-sm btn-default' style='margin-top:1px'>Page "+current_page+" of "+total_pages+"</button><button id='table_next_page_button' class='btn btn-sm btn-default glyphicon glyphicon-arrow-right'></button>";
93 html = html+"<button class='disabled btn btn-sm btn-default glyphicon glyphicon-arrow-left'></button><button class='btn btn-sm btn-default' style='margin-top:1px'>Page "+current_page+" of "+total_pages+"</button><button id='table_next_page_button' class='btn btn-sm btn-default glyphicon glyphicon-arrow-right'></button>";
96 html = html+"<button class='disabled btn btn-sm btn-default glyphicon glyphicon-arrow-left'></button><button class='btn btn-sm btn-default' style='margin-top:1px'>Page "+current_page+" of "+total_pages+"</button><button id='table_next_page_button' class='disabled btn btn-sm btn-default glyphicon glyphicon-arrow-right'></button>";
98 html = html+"</div></div><div class='col-sm-5'><div class='input-group input-group-sm'><span class='input-group-addon' id='basic-addon1'>Page:</span><input type='text' id='table_change_current_page_input' class='form-control' placeholder='"+current_page+"' aria-describedby='basic-addon1'></div></div></div></div>";
99 html = html+"<div class='col-sm-6'><div class='row'><div class='col-sm-7'><div class='input-group input-group-sm'><span class='input-group-addon' id='basic-addon2'>Page Size:</span><input type='text' id='table_change_page_size_input' class='form-control' placeholder='"+page_size+"' aria-describedby='basic-addon2'></div></div><div class='col-sm-5'><button class='btn btn-sm btn-default'>Total: "+total_count+"</button></div></div></div></div>";
100 html = html+"</div>";
102 jQuery("#"+div_id).html(html);
105 function capitalizeEachWord(string) {
106 return string.charAt(0).toUpperCase() + string.slice(1);
109 function checkDefined(o) {
110 if (typeof o === 'undefined' || o === null || o === 'undefined') {
117 jQuery(document).ready(function () {
119 jQuery(document).on( 'click', "#table_next_page_button", function() {
121 url: jQuery("#table_return_url_input").val(),
125 'currentPage':jQuery("#table_next_page_input").val(),
126 'pageSize':jQuery("#table_page_size_input").val(),
128 beforeSend: function() {
129 jQuery("#working_modal").modal("show");
131 success: function(response) {
132 jQuery("#working_modal").modal("hide");
133 //console.log(response);
134 var div_id = jQuery("#table_div_id_input").val();
135 var return_url = jQuery("#table_return_url_input").val();
136 if (checkDefined(jQuery("#table_links_input").val()) == 1) {
137 var links = jQuery.parseJSON( jQuery("#table_links_input").val() );
139 var links = jQuery("#table_links_input").val();
141 jQuery("#"+jQuery("#table_div_id_input").val()).empty();
142 brapi_create_table(response.result.data, response.metadata.pagination, div_id, return_url, links);
144 error: function(response) {
145 jQuery("#working_modal").modal("hide");
146 alert('An error occurred constructing table after moving to the next page.');
151 jQuery(document).on( 'click', "#table_previous_page_button", function() {
153 url: jQuery("#table_return_url_input").val(),
157 'currentPage':jQuery("#table_previous_page_input").val(),
158 'pageSize':jQuery("#table_page_size_input").val(),
160 beforeSend: function() {
161 jQuery("#working_modal").modal("show");
163 success: function(response) {
164 jQuery("#working_modal").modal("hide");
165 //console.log(response);
166 var div_id = jQuery("#table_div_id_input").val();
167 var return_url = jQuery("#table_return_url_input").val();
168 if (checkDefined(jQuery("#table_links_input").val()) == 1) {
169 var links = jQuery.parseJSON( jQuery("#table_links_input").val() );
171 var links = jQuery("#table_links_input").val();
173 jQuery("#"+jQuery("#table_div_id_input").val()).empty();
174 brapi_create_table(response.result.data, response.metadata.pagination, div_id, return_url, links);
176 error: function(response) {
177 jQuery("#working_modal").modal("hide");
178 alert('An error occurred constructing table after moving to the previous page.');
183 jQuery(document).on( 'keyup', "#table_change_page_size_input", function() {
186 url: jQuery("#table_return_url_input").val()+"?currentPage="+jQuery("#table_previous_page_input").val()+"&pageSize="+jQuery("#table_change_page_size_input").val(),
188 beforeSend: function() {
189 jQuery("#working_modal").modal("show");
191 success: function(response) {
192 jQuery("#working_modal").modal("hide");
193 //console.log(response);
194 var div_id = jQuery("#table_div_id_input").val();
195 var return_url = jQuery("#table_return_url_input").val();
196 if (checkDefined(jQuery("#table_links_input").val()) == 1) {
197 var links = jQuery.parseJSON( jQuery("#table_links_input").val() );
199 var links = jQuery("#table_links_input").val();
201 jQuery("#"+jQuery("#table_div_id_input").val()).empty();
202 brapi_create_table(response.result.data, response.metadata.pagination, div_id, return_url, links);
204 error: function(response) {
205 jQuery("#working_modal").modal("hide");
206 alert('An error occurred constructing table after moving to the previous page.');
212 jQuery(document).on( 'keyup', "#table_change_current_page_input", function() {
215 url: jQuery("#table_return_url_input").val()+"?currentPage="+jQuery("#table_change_current_page_input").val()+"&pageSize="+jQuery("#table_page_size_input").val(),
217 beforeSend: function() {
218 jQuery("#working_modal").modal("show");
220 success: function(response) {
221 jQuery("#working_modal").modal("hide");
222 //console.log(response);
223 var div_id = jQuery("#table_div_id_input").val();
224 var return_url = jQuery("#table_return_url_input").val();
225 if (checkDefined(jQuery("#table_links_input").val()) == 1) {
226 var links = jQuery.parseJSON( jQuery("#table_links_input").val() );
228 var links = jQuery("#table_links_input").val();
230 jQuery("#"+jQuery("#table_div_id_input").val()).empty();
231 brapi_create_table(response.result.data, response.metadata.pagination, div_id, return_url, links);
233 error: function(response) {
234 jQuery("#working_modal").modal("hide");
235 alert('An error occurred constructing table after moving to the previous page.');
241 var delay = (function(){
243 return function(callback, ms){
244 clearTimeout (timer);
245 timer = setTimeout(callback, ms);