2 // This is for displaying brapi non-paginated responses in a table format.
4 // Pass a js object with the headers and data as key:values, such as:
6 // "col1":"col1row1value",
11 // 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"
12 // as well as optionally, a link js object, which will be used to construct links in the table. e.g. { "name": ["mapId", "/maps/protocols/"] }
14 function brapi_create_table(data, div_id, link) {
18 var html = "<table class='table table-hover table-bordered'><thead><tr>";
21 if (data.hasOwnProperty(h)) {
22 if (checkDefined(link) == 0) {
25 for(var link_header in link) {
26 if(h != link[link_header][0]) {
33 //console.log(header);
35 for(var col=0; col<header.length; col++){
36 html = html+"<th>"+capitalizeEachWord(header[col])+"</th>";
38 html = html+"</tr></thead><tbody>";
40 for(var col=0; col<header.length; col++){
41 if (checkDefined(link) == 0) {
42 html = html+"<td>"+data[header[col]]+"</td>";
44 for(var link_header in link) {
45 if (header[col] == link_header) {
46 if (link[link_header][1] == 'stock') {
47 html = html+"<td><a href='/"+link[link_header][1]+"/"+data[link[link_header][0]]+"/view'>"+data[link_header]+"</a></td>";
49 html = html+"<td><a href='/"+link[link_header][1]+"/"+data[link[link_header][0]]+"'>"+data[link_header]+"</a></td>";
52 html = html+"<td>"+data[header[col]]+"</td>";
58 html = html+"</tbody></table>";
59 jQuery("#"+div_id).html(html);
63 // 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
65 // Pass a js object with the headers and data as key:values, such as:
67 // {"col1":"col1row1value", "height":"12", "age":"900"},
68 // {"col1":"col1row2value", "height":"1", "age":"9"},
69 // {"col1":"col1row3value", "height":"2", "age":"99"}
72 // as well as a pagination js object, such as:
73 // {"currentPage": "2", "pageSize": "200", "totalCount": "999", "totalPages":"5"}
75 // 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"
76 // as well as a return url for processing changing currentPage and pageSize
77 // as well as optionally, a link js object, which will be used to construct links in the table. e.g. { "name": ["mapId", "/maps/protocols/"] }
78 // as well as the search query data so that the query can be run on next page, prev page, etc
79 // as well as a list of column names you want to display_columns
80 // as well as the column name that should be the value of the select BreedersToolbox
81 // as well as a list of column names that can be added as data-attributes to the select box
83 function brapi_create_paginated_table(data, pagination, div_id, return_url, link, search_query_data, display_columns, select_column, select_data) {
85 console.log(pagination);
86 //console.log(div_id);
87 //console.log(return_url);
89 //console.log(display_columns);
90 var current_page = pagination.currentPage;
91 var next_page = Number(current_page) + 1;
92 var previous_page = Number(current_page) - 1;
93 var page_size = pagination.pageSize;
94 var total_count = pagination.totalCount;
95 var total_pages = pagination.totalPages;
96 var url = jQuery("#table_return_url_input").val() ? jQuery("#table_return_url_input").val() : 'This Database';
97 var html = "<input id='table_div_id_input' type='hidden' value='"+div_id+"'/>";
98 var html = html+"<input id='table_return_url_input' type='hidden' value='"+return_url+"'/>";
99 if (checkDefined(link) == 1) {
100 var html = html+"<input id='table_links_input' type='hidden' value='"+JSON.stringify(link)+"'/>";
102 var html = html+"<input id='table_links_input' type='hidden' value='"+link+"'/>";
104 var html = html+"<input id='table_page_size_input' type='hidden' value='"+page_size+"'/>";
105 var html = html+"<input id='table_next_page_input' type='hidden' value='"+next_page+"'/>";
106 var html = html+"<input id='table_previous_page_input' type='hidden' value='"+previous_page+"'/>";
107 var html = html+"<input id='table_checkbox_select_column' type='hidden' value='"+select_column+"'/>";
108 var html = html+"<input id='table_search_query_params' type='hidden' value='"+JSON.stringify(search_query_data)+"'/>";
109 var html = html+"<input id='table_display_columns_params' type='hidden' value='"+JSON.stringify(display_columns)+"'/>";
110 var html = html+"<input id='table_display_select_data_param' type='hidden' value='"+JSON.stringify(select_data)+"'/>";
111 if (data.length == 0) {
112 html = html+"<center><h3>No data available!</h3></center>";
114 html = html+"<h3>Results From "+url+"</h3><table class='table table-hover table-bordered'><thead><tr><th>Select</th>";
116 for(var h in data[0]) {
117 if (data[0].hasOwnProperty(h)) {
119 if (jQuery.inArray(h, display_columns) != -1){
120 if (checkDefined(link) == 0) {
123 for(var link_header in link) {
124 if(h != link[link_header][0]) {
133 for(var col=0; col<header.length; col++){
134 html = html+"<th>"+capitalizeEachWord(header[col])+"</th>";
136 html = html+"</tr></thead><tbody>";
137 for(var row=0; row<data.length; row++) {
138 html = html+"<tr><td><input type='checkbox' name='brapi_table_select_"+return_url+"' value='"+data[row][select_column]+"'";
139 for (var s_data=0; s_data<select_data.length; s_data++){
140 html = html + " data-"+select_data[s_data]+"='"+data[row][select_data[s_data]]+"'";
142 html = html + "/></td>";
143 for(var col=0; col<header.length; col++){
144 if (checkDefined(link) == 0) {
145 var v = data[row][header[col]];
146 if (typeof v === 'object'){
147 v = JSON.stringify(v);
149 html = html+"<td>"+v+"</td>";
151 for(var link_header in link) {
152 if (header[col] == link_header) {
153 if (link[link_header][1] == 'stock') {
154 html = html+"<td><a href='"+link[link_header][1]+"/"+data[row][link[link_header][0]]+"/view'>"+data[row][link_header]+"</a></td>";
156 html = html+"<td><a href='"+data[row][link[link_header][0]]+"'>"+data[row][link_header]+"</a></td>";
159 var v = data[row][header[col]];
160 if (typeof v === 'object'){
161 v = JSON.stringify(v);
163 html = html+"<td>"+v+"</td>";
170 html = html+"</tbody></table>";
172 html = html+"<div class='well well-sm'><div class='row'>";
173 html = html+"<div class='col-sm-7'><div class='row'><div class='col-sm-2'><button class='btn btn-primary' id='brapi_table_select_submit_"+div_id+"' >Select</button></div><div class='col-sm-7 col-md-5'><div class='btn-group' role='group'>";
175 if (total_pages > 1) {
176 if (current_page > 0) {
177 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>";
179 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>";
182 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>";
184 html = html+"</div></div><div class='col-sm-3 col-md-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>";
185 html = html+"<div class='col-sm-5'><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>";
186 html = html+"</div>";
188 jQuery("#"+div_id).html(html);
191 function capitalizeEachWord(string) {
192 return string.charAt(0).toUpperCase() + string.slice(1);
195 function checkDefined(o) {
196 if (typeof o === 'undefined' || o === null || o === 'undefined') {
203 function brapi_recreate_paginated_table(search_query_data, display_columns, select_column, select_data){
204 var delay = (function(){
206 return function(callback, ms){
207 clearTimeout (timer);
208 timer = setTimeout(callback, ms);
214 url: jQuery("#table_return_url_input").val(),
216 data: search_query_data,
218 beforeSend: function() {
219 jQuery("#working_modal").modal("show");
221 success: function(response) {
222 jQuery("#working_modal").modal("hide");
223 //console.log(response);
224 var div_id = jQuery("#table_div_id_input").val();
225 var return_url = jQuery("#table_return_url_input").val();
226 if (checkDefined(jQuery("#table_links_input").val()) == 1) {
227 var links = jQuery.parseJSON( jQuery("#table_links_input").val() );
229 var links = jQuery("#table_links_input").val();
231 jQuery("#"+jQuery("#table_div_id_input").val()).empty();
232 console.log(search_query_data);
233 brapi_create_paginated_table(response.result.data, response.metadata.pagination, div_id, return_url, links, search_query_data, display_columns, select_column, select_data);
235 error: function(response) {
236 jQuery("#working_modal").modal("hide");
237 alert('An error occurred constructing table after moving to the previous page.');
243 jQuery(document).ready(function () {
245 jQuery(document).on( 'click', "#table_next_page_button", function() {
246 var search_query_data = JSON.parse(jQuery('#table_search_query_params').val());
247 var display_columns = JSON.parse(jQuery('#table_display_columns_params').val());
248 var select_data = JSON.parse(jQuery('#table_display_select_data_param').val());
249 var select_column = jQuery('#table_checkbox_select_column').val();
250 search_query_data['page'] = jQuery("#table_next_page_input").val();
251 search_query_data['pageSize'] = jQuery("#table_page_size_input").val();
252 brapi_recreate_paginated_table(search_query_data, display_columns, select_column, select_data);
255 jQuery(document).on( 'click', "#table_previous_page_button", function() {
256 var search_query_data = JSON.parse(jQuery('#table_search_query_params').val());
257 var display_columns = JSON.parse(jQuery('#table_display_columns_params').val());
258 var select_data = JSON.parse(jQuery('#table_display_select_data_param').val());
259 var select_column = jQuery('#table_checkbox_select_column').val();
260 search_query_data['page'] = jQuery("#table_previous_page_input").val();
261 search_query_data['pageSize'] = jQuery("#table_page_size_input").val();
262 brapi_recreate_paginated_table(search_query_data, display_columns, select_column, select_data);
265 jQuery(document).on( 'keyup', "#table_change_page_size_input", function() {
266 var search_query_data = JSON.parse(jQuery('#table_search_query_params').val());
267 var display_columns = JSON.parse(jQuery('#table_display_columns_params').val());
268 var select_data = JSON.parse(jQuery('#table_display_select_data_param').val());
269 var select_column = jQuery('#table_checkbox_select_column').val();
270 search_query_data['page'] = 0;
271 search_query_data['pageSize'] = jQuery("#table_change_page_size_input").val();
272 brapi_recreate_paginated_table(search_query_data, display_columns, select_column, select_data);
275 jQuery(document).on( 'keyup', "#table_change_current_page_input", function() {
276 var search_query_data = JSON.parse(jQuery('#table_search_query_params').val());
277 var display_columns = JSON.parse(jQuery('#table_display_columns_params').val());
278 var select_data = JSON.parse(jQuery('#table_display_select_data_param').val());
279 var select_column = jQuery('#table_checkbox_select_column').val();
280 search_query_data['page'] = jQuery("#table_change_current_page_input").val();
281 brapi_recreate_paginated_table(search_query_data, display_columns, select_column, select_data);