【问题标题】:UI grid export to CSV with the result after filter?UI网格导出到CSV并过滤后的结果?
【发布时间】:2017-03-10 18:25:12
【问题描述】:

我正在使用 ui-grid,我想将过滤后的所有记录导出到 csv,有人可以帮忙吗? 例如过滤电子邮件后是 gmail.com 我有 3 页记录,如何将 3 页中的所有记录导出到 csv?

【问题讨论】:

  • 你能把问题说得更具体一点吗?是尝试手动导出数据还是使用 API?如果是 API,能否向我们展示您正在使用的代码?
  • 我使用 ui-grid 这里是代码 $scope.gridOptions = { columnDefs: columnDefs, /*plugins: [new ngGridCsvExportPlugin(csvOpts)],*/ exporterMenuCsv: true, enableGridMenu: true, enableColumnResizing : true, enableFiltering: true, // pagingOptions - paginationPageSizes: [10, 20, 30, 50, 100], paginationPageSize: 10, enablePaginationControls: true, exporterFieldCallback: function(grid, row, col, input) { if( col. name == 'user_name') { return $scope.getUserName(input); } else { 返回输入; } },

标签: csv export filtering ng-grid


【解决方案1】:

如果您已将过滤器应用于 columnDefs,那么您只需编写一个回调方法,该方法将在导出数据时执行相同的过滤器。

以下是我提出的解决方案。此方法适用于具有多个参数的多个过滤器。

列定义:

var rptColumnDefs = [ {
        field: 'field1',
        cellFilter: "filter1:'param1' | filter2 | filter3 : 4 | filter4 : 4 : 'param2'",
        displayName: "Field 1"  
    }, field: 'field2',
        cellFilter: "filter1:'param1' | filter2 | filter3 : 4 | filter4 : 4 : 'param2'",
        displayName: "Field 2"  
    }]

导出器回调函数:

$scope.reportGridOptions = {
    enableFiltering: true,
    enableHorizontalScrollbar: uiGridConstants.scrollbars.NEVER,
    enableVerticalScrollbar: uiGridConstants.scrollbars.WHEN_NEEDED,
    exporterCsvFilename: '',
    columnDefs: rptColumnDefs,    
    exporterFieldCallback: function (grid, row, col, input) {
            if (col.cellFilter) { // check if any filter is applied on the column
                var filters = col.cellFilter.split('|'); // get all the filters applied
                angular.forEach(filters, function (filter) {
                    var filterName = filter.split(':')[0]; // fetch filter name
                    var filterParams = filter.split(':').splice(1); //fetch all the filter parameters
                    filterParams.unshift(input); // insert the input element to the filter parameters list
                    var filterFn = $filter(filterName); // filter function
                    // call the filter, with multiple filter parameters. 
                    //'Apply' will call the function and pass the array elements as individual parameters to that function.
                    input = filterFn.apply(this, filterParams); 
                })
                return input;
            }
            else
                return input;
        }
}

这个通用函数将帮助导出在 UI Grid 中显示的数据(在应用过滤器之后)。

【讨论】:

  • 碰巧在 Plunker 中设置了这个?
【解决方案2】:
public class CSVExporter
{
    public static void WriteToCSV(List<Person> personList)
    {
        string attachment = "attachment; filename=PersonList.csv";
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ClearHeaders();
        HttpContext.Current.Response.ClearContent();
        HttpContext.Current.Response.AddHeader("content-disposition", attachment);
        HttpContext.Current.Response.ContentType = "text/csv";
        HttpContext.Current.Response.AddHeader("Pragma", "public");
        WriteColumnName();
        foreach (Person person in personList)
        {
            WriteUserInfo(person);
        }
        HttpContext.Current.Response.End();
    }

    private static void WriteUserInfo(Person person)
    {
        StringBuilder stringBuilder = new StringBuilder();
        AddComma(person.Name, stringBuilder);
        AddComma(person.Family, stringBuilder);
        AddComma(person.Age.ToString(), stringBuilder);
        AddComma(string.Format("{0:C2}", person.Salary), stringBuilder);
        HttpContext.Current.Response.Write(stringBuilder.ToString());
        HttpContext.Current.Response.Write(Environment.NewLine);
    }

    private static void AddComma(string value, StringBuilder stringBuilder)
    {
        stringBuilder.Append(value.Replace(',', ' '));
        stringBuilder.Append(", ");
    }

    private static void WriteColumnName()
    {
        string columnNames = "Name, Family, Age, Salary";
        HttpContext.Current.Response.Write(columnNames);
        HttpContext.Current.Response.Write(Environment.NewLine);
    }
}

【讨论】:

  • 谢谢,但我想在 ui-gird 中而不是在服务器端执行此操作 下面是我在 ui-grid $scope.exportCsv = function(){ $scope.gridApi.selection.selectAllRows 中的代码(); console.log(uiGridExporterConstants.SELECTED); //$scope.gridApi.exporter.csvExport(uiGridExporterConstants.SELECTED, uiGridExporterConstants.ALL); uiGridExporterService.csvExport($scope.gridApi.grid,uiGridExporterConstants.SELECTED, uiGridExporterConstants.ALL); //$scope.gridApi.selection.clearSelectedRows(); };但它不起作用,你知道吗?
  • 顺便说一句,我不知道如何格式化代码,你是怎么做到的?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-18
  • 2020-01-29
  • 1970-01-01
  • 2018-04-28
  • 1970-01-01
相关资源
最近更新 更多