GUI -- Updated TableService to use d3 table creation methods.
Change-Id: I318ee4e81624118a82c52efe380213c661b743c5
Showing
3 changed files
with
34 additions
and
55 deletions
| ... | @@ -24,8 +24,8 @@ | ... | @@ -24,8 +24,8 @@ |
| 24 | var config = { | 24 | var config = { |
| 25 | colIds: ['_iconid_available', 'id', 'mfr', 'hw', 'sw', 'serial', | 25 | colIds: ['_iconid_available', 'id', 'mfr', 'hw', 'sw', 'serial', |
| 26 | 'annotations'], | 26 | 'annotations'], |
| 27 | - colText: ['Availability', 'URI', 'Vendor', 'Hardware Version', 'Software Version', | 27 | + colText: ['Availability', 'URI', 'Vendor', 'Hardware Version', |
| 28 | - 'Serial Number', 'Protocol'] | 28 | + 'Software Version', 'Serial Number', 'Protocol'] |
| 29 | }, | 29 | }, |
| 30 | deviceData = { | 30 | deviceData = { |
| 31 | "devices": [{ | 31 | "devices": [{ |
| ... | @@ -234,7 +234,7 @@ | ... | @@ -234,7 +234,7 @@ |
| 234 | 234 | ||
| 235 | .controller('showTableCtrl', ['$log', 'TableService', | 235 | .controller('showTableCtrl', ['$log', 'TableService', |
| 236 | function ($log, ts) { | 236 | function ($log, ts) { |
| 237 | - ts.renderAndLoadTable(d3.select('#tableDiv'), config, deviceData); | 237 | + ts.renderTable(d3.select('#tableDiv'), config, deviceData); |
| 238 | }]) | 238 | }]) |
| 239 | 239 | ||
| 240 | .directive('fixedHeader', ['$log', '$timeout', function ($log, $timeout) { | 240 | .directive('fixedHeader', ['$log', '$timeout', function ($log, $timeout) { | ... | ... |
| ... | @@ -22,52 +22,40 @@ | ... | @@ -22,52 +22,40 @@ |
| 22 | 22 | ||
| 23 | var $log; | 23 | var $log; |
| 24 | 24 | ||
| 25 | - function renderTable(div, config) { | 25 | + function renderTable(div, config, data) { |
| 26 | var table = div.append('table').attr('fixed-header', ''), | 26 | var table = div.append('table').attr('fixed-header', ''), |
| 27 | - thead, tr, numTableCols, i; | 27 | + colIds = config.colIds, |
| 28 | - table.append('thead'); | 28 | + colText = config.colText, |
| 29 | - table.append('tbody'); | 29 | + dataObjects = data[Object.keys(data)[0]], |
| 30 | + thead, tbody, tRows; | ||
| 30 | 31 | ||
| 31 | - thead = table.select('thead'); | 32 | + thead = table.append('thead'); |
| 32 | - tr = thead.append('tr'); | 33 | + tbody = table.append('tbody'); |
| 33 | - numTableCols = config.colIds.length; | ||
| 34 | 34 | ||
| 35 | - for(i = 0; i < numTableCols; i += 1) { | 35 | + thead.append('tr').selectAll('th') |
| 36 | - tr.append('th').html(config.colText[i]); | 36 | + .data(colText) |
| 37 | - } | 37 | + .enter() |
| 38 | + .append('th') | ||
| 39 | + .text(function(d) { return d }); | ||
| 38 | 40 | ||
| 39 | - return config.colIds; | 41 | + tRows = tbody.selectAll('tr') |
| 40 | - } | 42 | + .data(dataObjects) |
| 41 | - | 43 | + .enter() |
| 42 | - // I can delete these comments later... | 44 | + .append('tr'); |
| 43 | - // loadTableData needs to know which IDs are used to create the table... | ||
| 44 | - // Potentially, there will be some rows in the JSON that the server is | ||
| 45 | - // sending back that will be unused. We don't want to create unneeded rows. | ||
| 46 | - // For example, in device view, we aren't displaying "role" or | ||
| 47 | - // "available" properties, but they would be displayed if we took it | ||
| 48 | - // directly from the data being sent in. | ||
| 49 | - function loadTableData(data, div, colIds) { | ||
| 50 | - // let me know if you have suggestions for this function | ||
| 51 | 45 | ||
| 52 | - var numTableCols = colIds.length, | 46 | + tRows.selectAll('td') |
| 53 | - tbody, tr, i; | 47 | + .data(function(row) { |
| 54 | - tbody = div.select('tbody'); | 48 | + return colIds.map(function(headerId) { |
| 55 | - | 49 | + return { |
| 56 | - // get the array associated with the first object, such as "devices" | 50 | + column: headerId, value: row[headerId] |
| 57 | - // loop through every object in the array, and every colId in config | 51 | + }; |
| 58 | - // put the appropriate property in the td of the table | 52 | + }); |
| 59 | - (data[Object.keys(data)[0]]).forEach(function (item) { | 53 | + }) |
| 60 | - tr = tbody.append('tr'); | 54 | + .enter() |
| 61 | - for(i = 0; i < numTableCols; i += 1) { | 55 | + .append('td') |
| 62 | - if(item.hasOwnProperty(colIds[i])) { | 56 | + .html(function(d) { return d.value }); |
| 63 | - tr.append('td').html(item[colIds[i]]); | ||
| 64 | - } | ||
| 65 | - } | ||
| 66 | - }); | ||
| 67 | - } | ||
| 68 | 57 | ||
| 69 | - function renderAndLoadTable(div, config, data) { | 58 | + return table; |
| 70 | - loadTableData(data, div, (renderTable(div, config))); | ||
| 71 | } | 59 | } |
| 72 | 60 | ||
| 73 | angular.module('onosWidget') | 61 | angular.module('onosWidget') |
| ... | @@ -75,9 +63,7 @@ | ... | @@ -75,9 +63,7 @@ |
| 75 | $log = _$log_; | 63 | $log = _$log_; |
| 76 | 64 | ||
| 77 | return { | 65 | return { |
| 78 | - renderTable: renderTable, | 66 | + renderTable: renderTable |
| 79 | - loadTableData: loadTableData, | ||
| 80 | - renderAndLoadTable: renderAndLoadTable | ||
| 81 | }; | 67 | }; |
| 82 | }]); | 68 | }]); |
| 83 | 69 | ... | ... |
| ... | @@ -121,19 +121,12 @@ describe('factory: fw/widget/table.js', function() { | ... | @@ -121,19 +121,12 @@ describe('factory: fw/widget/table.js', function() { |
| 121 | } | 121 | } |
| 122 | 122 | ||
| 123 | it('should create table tags', function () { | 123 | it('should create table tags', function () { |
| 124 | - ts.renderTable(d3Elem, config); | 124 | + ts.renderTable(d3Elem, config, fakeData); |
| 125 | verifyTableTags(d3Elem); | 125 | verifyTableTags(d3Elem); |
| 126 | }); | 126 | }); |
| 127 | 127 | ||
| 128 | it('should load data into table', function () { | 128 | it('should load data into table', function () { |
| 129 | - var colIds = ts.renderTable(d3Elem, config); | 129 | + ts.renderTable(d3Elem, config, fakeData); |
| 130 | - ts.loadTableData(fakeData, d3Elem, colIds); | ||
| 131 | verifyData(d3Elem); | 130 | verifyData(d3Elem); |
| 132 | }); | 131 | }); |
| 133 | - | ||
| 134 | - it('should render table and load data', function () { | ||
| 135 | - ts.renderAndLoadTable(d3Elem, config, fakeData); | ||
| 136 | - verifyData(d3Elem); | ||
| 137 | - }); | ||
| 138 | - | ||
| 139 | }); | 132 | }); | ... | ... |
-
Please register or login to post a comment