joeharrison714 / MVCGrid.Net

http://mvcgrid.net
MIT License
75 stars 56 forks source link

Using withPreloadData=false causes Script Error #113

Open perret123 opened 7 years ago

perret123 commented 7 years ago

Hi there! Thanks for providing this tool and supporting it :-)

I think I have found a bug, but let's see what you think:

I query quite a lot of data, so i use the following settings when setting up the grid: .WithQueryOnPageLoad(true).WithPreloadData(false).WithFiltering(true)

Inside the .cshtml, I initialize the Grid like this: @Html.MVCGrid("CustomerSearch")

And of course I also use a few Custom Filters. Now already when first loading the page, I run into this error:

error

I checked it out and I saw why:

In MVCGrid.init you do this:

for (var i = 0; i < currentGrids.length; i++) {
            var obj = currentGrids[i];

            if (!obj.preloaded) {
                MVCGrid.reloadGrid(obj.name);
            }
        }

        bindToolbarEvents();

And in bindToolbarEventsyou do loadBoundFiltersand then getClientDatawhich checks $('#' + 'MVCGrid_' + mvcGridName + '_ContextJsonData').html()

As MVCGrid.reloadGrid is not yet finished with building the grid, $('#' + 'MVCGrid_' + mvcGridName + '_ContextJsonData').html() is still empty when executed which leads to the above error.

To fix it I think you would need to do "bindToolbarEvents" on reloadGrid complete function.

But yeah maybe I missed something and do something wrong, please advice :-) Thank you!

zairja commented 6 years ago

@perret123 Were you ever able to implement a fix or workaround for this issue? I found that I can avoid it if I remove the data-mvcgrid-type="filter" and data-mvcgrid-option attributes from my inputs. Then on the click event of my button that applies the filter(s), I call MVCGrid.setFilters('MyGrid', { Name: $('#NameInput').val() }) (one would add whatever relevant input(s) to filter).

This also removes the need to ever call MVCGrid.reloadGrid('MyGrid') since if you weren't preloading or querying on page load then you wanted to avoid loading the entire grid in the first place. The first time you set the filter(s), the grid will load with those filters applied. If you apply the filter(s) with all the inputs cleared, you will return all the results. (Once again, reloadGrid is useless here because calling it when filters are set will only return the filtered grid, not all unfiltered results)

perret123 commented 6 years ago

Hey zairja

Thank you for your answer - also seems a legit way to work around the problem :-)

Actually I made a dirty quick fix for myself in MVCGrid but was too lazy to make a pull request for this project as I didn't really want to invest time to check if this fix works in all cases:

In the init function, I removed the bindToolbarEvents(); Line.

Instead I added it in the reloadGrid function in the "success" part of the ajax method:

            success: function (result) {
                $('#' + tableHolderHtmlId).html(result);
                bindToolbarEvents();
            },

This works well for me without removing the data-mvcgrid attributes.