joeharrison714 / MVCGrid.Net

http://mvcgrid.net
MIT License
74 stars 55 forks source link

Export not including page parameters #27

Closed smillerk2 closed 8 years ago

smillerk2 commented 8 years ago

If I have already filtered my grid data using page parameters (as per below) then how can I have the getExportUrl use these settings and include them in the call to the AXD? It looks as though the export Url function needs to be updated with all of the current page parameters and their values.

         @Html.MVCGrid(ViewData["GridName"].ToString(), new { 
               Region = Model.Name,
               Province = Model.City,
               City = Model.Zip
            }
        )
        $(function () {
            $('#exportButton').click(function () {
                location.href = MVCGrid.getExportUrl('GlobalExportGrid');
            });
        });
smillerk2 commented 8 years ago

Hi. Is there any chance this could be corrected?

joeharrison714 commented 8 years ago

Hi smillerk2, I see the problem now. I am going to need to think of the best way to handle this. There may be a workaround where you could use AdditionalQueryOptions instead. Can you tell me more about your situation? Thanks, Joe

smillerk2 commented 8 years ago

Hi,

The issue is that if I filter the grid using a FORM POST and then try and export the grid's content, the pp_ parameters (as represented in pageParameters) are not passed to the handler. I believe the fix required would be something like:

// public
    this.getExportUrl = function (mvcGridName) {
        var gridDef = findGridDef(mvcGridName);

        var exportUrl = handlerPath + location.search;
        exportUrl = updateURLParameter(exportUrl, 'engine', 'export');
        exportUrl = updateURLParameter(exportUrl, 'Name', mvcGridName);

        //POSSIBLE CHANGE - added to ensure export includes filtered parameters
        $.each(gridDef.pageParameters, function (k, v) {
            var thisPP = "_pp_" + gridDef.qsPrefix + k;
            exportUrl = updateURLParameter(exportUrl, thisPP, v);
        });

        return exportUrl;
    };
joeharrison714 commented 8 years ago

Hi, So this actually was the intent of page parameters. Their purpose was to be able to pass static data one time on page load. The use case for this is if you are displaying data for a parent ID and need to get the parent ID into the grid at page load. That's not something that would change as the user interacts with the page.

I added some additional info about AdditionalQueryOptions. Can you read this and see if it would accomplish what you are trying to do? http://mvcgrid.net/demo/AdditionalQueryOptions

Thanks, Joe

smillerk2 commented 8 years ago

Hi Joe,

I don't think that will work because then I am simply duplicating my page parameter names as part of AdditionalQueryOptions. Let me give you an example that hopefully describes the scenario:

First, the MVCGrid is initialized in the view with explicit page parameters. The values in Model.Criteria come from dropdowns within a FORM so are available to the MVCGrid upon postback:

@Html.MVCGrid(ViewData["GridName"].ToString(), new { 
                                    Region = Model.Criteria.Region.Coalesce(""),
                                    Province = Model.Criteria.Province.Coalesce(""),
                                    City = Model.Criteria.City.Coalesce("")
})

My MVCGrid definition looks like this:

.WithPageParameterNames(new string[] { "Region", "Province", "City" })
.WithAdditionalQueryOptionNames("Search")

My WithRetrieveDataMethod is:

SearchCriteria crit = new SearchCriteria
            {
                Region = options.GetPageParameterString("Region"),
                Province = options.GetPageParameterString("Province"),
                City = options.GetPageParameterString("City")
            };

var options = ctx.QueryOptions;
string globalSearch = options.GetAdditionalQueryOptionString("Search");
var items = repo.GetData(crit);
items = items.Where(a => a.Name.StartsWith(globalSearch.Coalesce(a.Name), StringComparison.InvariantCultureIgnoreCase));

return new QueryResult<SearchRecord>()
            {
                Items = options.SortDirection== SortDirection.Dsc ? items.Skip(limit).Take(limitRow).Reverse() : items.Skip(limit).Take(limitRow)
            };

Using the export link causes all the pageParameters to be lost:

<script type="text/javascript">
        $(function () {
            $('#exportButton').click(function () {
                location.href = MVCGrid.getExportUrl('GlobalSearchGrid');
            });
        });
    </script>

I'm not sure how best to change the code to accommodate AdditionalQueryOptions without duplicating the PageParameterNames. All I'm trying to achieve is an export but preserving the values that were initially sent into the MVCGrid.

Thanks for your assistance.

joeharrison714 commented 8 years ago

smillerk2, This has been fixed. Nuget package updated. Thanks, Joe

smillerk2 commented 8 years ago

Confirmed in 1.0.0.60. Thanks Joe.