Skip to content

Commit

Permalink
Add export to grid.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdriscoll committed Jun 20, 2019
1 parent 68c91dd commit f405740
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
31 changes: 29 additions & 2 deletions src/UniversalDashboard.Materialize/Components/ud-grid.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,27 @@ export default class UdGrid extends React.Component {

onExportData() {

var csv = '';
this.state.data.forEach(x => {
for(var propertyName in x) {
var property = x[propertyName];
if (property == null) continue;

csv += property + ",";
}
csv = csv.substr(0, csv.length - 1) + "\r\n";
})

var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(csv));
element.setAttribute('download', 'export.csv');

element.style.display = 'none';
document.body.appendChild(element);

element.click();

document.body.removeChild(element);
}

render() {
Expand Down Expand Up @@ -303,7 +324,13 @@ export default class UdGrid extends React.Component {
>
{rowDefinition}
</Griddle>,
<GridToolbar activePage={this.state.currentPage} totalPages={Math.ceil(this.state.recordCount / this.state.pageSize)} onPageChanged={this.onPageChanged.bind(this)}/>]
<GridToolbar
activePage={this.state.currentPage}
totalPages={Math.ceil(this.state.recordCount / this.state.pageSize)}
onPageChanged={this.onPageChanged.bind(this)}
onExportData={this.onExportData.bind(this)}
noExport={this.props.noExport}
/>]
: <div>No results found</div>}
</div>

Expand Down Expand Up @@ -335,7 +362,7 @@ class GridToolbar extends React.Component {
}

return (
<Row><Button icon={<UdIcon icon="Download" />} style={{display: 'inline-block', float: 'right', marginTop: '15x'}} onClick={this.onExportData.bind(this)} />{pagination}</Row>
<Row><Button icon={<UdIcon icon="Download" />} style={{display: this.props.noExport ? 'none' : 'inline-block', float: 'right', marginTop: '15px'}} onClick={this.props.onExportData} />{pagination}</Row>
)
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/UniversalDashboard.Materialize/Scripts/grid.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ function New-UDGrid {
[Parameter()]
[Switch]$AutoRefresh,
[Parameter()]
[int]$RefreshInterval = 5
[int]$RefreshInterval = 5,
[Parameter()]
[Switch]$NoExport
)

End {
Expand Down Expand Up @@ -73,6 +75,7 @@ function New-UDGrid {
noFilter = $NoFilter.IsPresent
autoRefresh = $AutoRefresh.IsPresent
refreshInterval = $RefreshInterval
noExport = $NoExport.IsPresent
}
}
}
2 changes: 0 additions & 2 deletions src/UniversalDashboard.Materialize/Tests/grid.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,6 @@ Describe "Grid" {

Set-TestDashboard -Dashboard $dashboard

Wait-Debugger

It "should not page when NoPaging set" {
$Element = Find-SeElement -Id "NoPagingGrid" -Driver $Driver
$Element = Find-SeElement -ClassName "griddle-row" -Driver $Element[0]
Expand Down

0 comments on commit f405740

Please sign in to comment.