ASP.net
Awesome
Learn
Forum
Buy
Demos
Sign In
☾
☀
Switch to
Dark
Light
Mode
this site works best with javascript enabled
Ask Question
generate Grid Export columns automatically based grid current columns
Title:
B
I
{code}
?
I want to get the grid's `Column[]` to map them to `ExpColumn[]` for the export, so I can generate the ExpColumn automatically for each grid where I need to export to excel or pdf. Each grid in my app can have different Columns, plus the user might show/hide columns, and that might need to be reflected in the exported result.
Save Changes
Cancel
Arash Ansari
asked 9 days ago
Answers
B
I
{code}
?
In the view you can modify the `getFile` function ( found in our demo view: https://demo.aspnetawesome.com/GridExportToExcelDemo ) to this: function getFile(url) { var $form = $('<form method="post"/>').attr('action', url).appendTo('body'); var grid = $('#@(grid)'); // request parameters (sorting, grouping, any parent parameters) var req = grid.data('api').getRequest(); // send visible columns info, binds to GridExpParams var expCols = getExpColumns(grid); awef.loop(expCols, function(col, i) { $form.append("<input type='hidden' name='expColumns["+i+"].Name' value='" + col.name + "'/>"); $form.append("<input type='hidden' name='expColumns["+i+"].Header' value='" + col.header + "'/>"); $form.append("<input type='hidden' name='expColumns["+i+"].Width' value='" + col.W || col.Mw || 140 + "'/>"); }); awef.loop(req, function(val) { $form.append("<input type='hidden' name='" + val.name + "' value='" + val.value + "'/>"); }); $form.append($('#allPages').clone()); $form.submit(); $form.remove(); } function getExpColumns(g) { var vcols = []; awef.loop(g.data('o').columns, function (col) { if (!col.Hid) { console.log(col); vcols.push({ name: col.T || col.P, header: col.H }); } }); return vcols; } this way in the controller export action you'll get the `expColumns` array, so you can make use of it like this: [HttpPost] public IActionResult ExportGridToPdf(GridParams g, ExpColumn[] expColumns, bool? allPages) { if (allPages.HasValue && allPages.Value) { g.Paging = false; } var gridModel = BuildGridModel(g); var builder = new GridPdfBuilder(expColumns) { ExpParams = new GridExpParams() { VisNames = expColumns.Select(o => o.Name).ToArray() } }; // the rest remains unchanged in `expColumn.Name` you'll get the the `Column` `Bind` or `ClientFormat` value, but you may still need to manually define `expColumn.ClientFormatFunc` in the controller for certain columns (in the view, `Column.ClientFormatFunc` is the name of a javascript function, so it can't be used in the controller when generating the pdf/xls etc. ).
Save Changes
Cancel
Omu
answered 8 days ago
Thank you for your kindness.
6 days ago
Arash Ansari
please
Sign In
to leave an answer
By accessing this site, you agree to store cookies on your device and disclose information in accordance with our
cookie policy
and
privacy policy
.
OK