Difference between revisions of "Dev/Results"
(tweaked child row) |
(New data source. One row per team*award. everything's different now) |
||
Line 18: | Line 18: | ||
<script> | <script> | ||
+ | //Request the awards data | ||
+ | var resultsRequest = jQuery.ajax({ | ||
+ | url: 'https://igem.org/awards/json_dump.cgi', | ||
+ | type: 'GET', | ||
+ | timeout: 30000, | ||
+ | dataType: "json", | ||
+ | error: function(jqxhr, textStatus, errorThrown) { | ||
+ | alert("AJAX error; look at var resultsRequest"); | ||
+ | }, | ||
+ | success: function(data, textStatus, jqxhr) { | ||
+ | console.log("successfully got team award data, now parsing"); | ||
+ | var arrayOfTeamAwards = parseTeamsToAwards(data); | ||
+ | } | ||
+ | }); | ||
+ | |||
+ | |||
+ | //Helper function | ||
String.prototype.capitalize = function() { | String.prototype.capitalize = function() { | ||
return this.charAt(0).toUpperCase() + this.slice(1); | return this.charAt(0).toUpperCase() + this.slice(1); | ||
Line 38: | Line 55: | ||
} | } | ||
+ | //Accepts an array of team objects and reorganizes it into an array of team*awards (incl no-awards) | ||
+ | function parseTeamsToAwards(arrayOfTeams) { | ||
+ | var arrayOfTeamAwards = []; | ||
+ | arrayOfTeams.forEach(function(teamObj, index, array) { | ||
+ | //Take care of any teams that didn't win any awards | ||
+ | if (teamObj.awards.length == 0) { | ||
+ | teamObj.awards.push(null); | ||
+ | } | ||
+ | awards.forEach(function(award) { | ||
+ | var teamAwardObj = jQuery.extend({}, teamObj); //copy the team object | ||
+ | delete teamAwardObj.awards; //delete the array of awards | ||
+ | teamAwardObj.award = award; //add the one award back | ||
+ | arrayOfTeamAwards.push(teamAwardObj); //and put the team*award on the list to return | ||
+ | }); | ||
+ | }); | ||
+ | } | ||
+ | |||
+ | |||
+ | //Initialize dataTable and set up event bindings. **Move this to after the data request comes back and document is ready. | ||
jQuery(document).ready(function() { | jQuery(document).ready(function() { | ||
− | var t = jQuery('#table_team').DataTable( { | + | // var t = jQuery('#table_team').DataTable( { |
− | + | // 'lengthMenu': [[10, 25, 50, 100, -1],[10, 25, 50, 100, 'All']], | |
− | + | // 'pageLength': -1, | |
− | + | // 'deferRender': true, //improves performance | |
− | + | // // 'ajax': { | |
− | + | // // 'url': 'https://igem.org/awards/json_dump.cgi', | |
− | + | // // 'dataSrc': '' | |
− | + | // // }, | |
− | + | // //CHANGE DATA SOURCE | |
− | + | // 'columns': [ | |
− | + | // { //Child-table control element | |
− | + | // 'className': 'details-control', | |
− | + | // 'orderable': false, | |
− | + | // 'data': null, | |
− | + | // 'defaultContent': '' | |
− | + | // }, | |
− | + | // {'data': 'team_name'}, | |
− | + | // {'data': 'region'}, | |
− | + | // {'data': 'medal',} | |
− | + | // ], | |
− | } ); | + | // 'order': [[1, 'asc']] //I think this sorts by team-name by default |
+ | // } ); | ||
− | // Add event listener for opening and closing details | + | // // Add event listener for opening and closing details |
− | $('#table_team tbody').on('click', 'td.details-control', function () { | + | // $('#table_team tbody').on('click', 'td.details-control', function () { |
− | + | // var tr = $(this).closest('tr'); | |
− | + | // var row = t.row( tr ); | |
− | + | // if ( row.child.isShown() ) { | |
− | + | // // This row is already open - close it | |
− | + | // row.child.hide(); | |
− | + | // tr.removeClass('shown'); | |
− | + | // } | |
− | + | // else { | |
− | + | // // Open this row | |
− | + | // row.child( childRowFormat(row.data()) ).show(); | |
− | + | // tr.addClass('shown'); | |
− | + | // } | |
− | } ); | + | // } ); |
− | jQuery('#table_team').addClass('hover'); | + | // jQuery('#table_team').addClass('hover'); |
}); | }); |
Revision as of 18:28, 23 September 2015