Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ng-grid export that calls cell filters

I'm working with an ng-grid and some columns have filters applied. However, when I export to CSV right now the values are displayed without filters: a record might like like this in json:

{"service_date":"2014-02-10T00:00:00.000Z",
 "service_code":"someJob3",
 "price":1234.56}

but in the grid it is displayed as:

Service Date |Service Desc | Price
-------------------------------------
Feb 10,2014  |Some Job 3   | $1,234.56

I'd like when I get an extract for it to appear as if the filter are applied but the only csv plugin does not seem to call cell filters, and simply returns the values in raw form. How can I invoke the filters?

like image 507
Michael B Avatar asked Dec 17 '25 15:12

Michael B


1 Answers

I discovered a solution that works with ui-grid (the replacement version of ng-grid), and it's much simpler than the plugin workaround that Matt Welch had to develop for ng-grid 2.0.

You can specify an exporterFieldCallback in your gridOptions and then do whatever you want based on the col.name. For my use case I had user IDs, which I needed to cross reference to an array index. I'd built a cellFilter for the table, but it obviously wasn't reflected in the exported CSV until I added the exporterFieldCallback like so:

$scope.gridOptions = {
    ...

    exporterFieldCallback: function( grid, row, col, input ) {
        if( col.name == 'account_executive_id' || col.name == 'account_manager_id' ) {
            return adminUsers.getUserName(input);
        } else {
            return input;
        }
    },
    ...
}

The callback requires the grid, row, col, and input variables, and then you can do anything inside it to return whatever values you need exported. It would be nice if there were a "use what's displayed in the grid" flag, but this is still a pretty simple solution, even if it appears to be undocumented.

like image 114
Mordred Avatar answered Dec 19 '25 06:12

Mordred



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!