vtfuture / BForms

Bootstrap Forms for ASP.NET MVC
MIT License
62 stars 33 forks source link

Best Way to "Refresh" Grid and Keep open Rows visible #263

Closed meister-d closed 8 years ago

meister-d commented 8 years ago

Hi BForms Team What is the Best way to keep the open Rows open (with Details) when I trigger a Refresh of the Grid.

After pressing the ResetButton (in the BulkActions Buttons), all rows are closed again. Is there a possibility to just refresh the Grid instead of Reset it?

I want to implement some sort of Refresh Timer, which refreshes the current Grid (View) after a certain amount of Time as the Serverside Data changes. After some code mining I came up with something like this:

jobIndex.prototype.init = function () {
        this.$grid = $('#grid');
        this.$toolbar = $('#toolbar');
        this.initGrid();
        this.initToolbar();
        //#region refreshTimer
        window.setInterval($.proxy(function () {
            this.$grid.bsGrid('refresh');
        }, this), 30000);
        //#endregion
    };

But unfortunately it seems to have the same behavior like the Reset Button.

Thanx for your Help

Regards Dumitru

cristipufu commented 8 years ago

Hi Dumitru,

You should parse the grid html and gather info about the rows (id, is opened) like this:

var items = [];

$.each(this.$grid.find('.grid_row'), $.proxy(function (idx, val) {

     items.push({
        Id: $(val).data('objid'),
        GetDetails: $(val).hasClass('open')
     });

}, this));

Then, you should make an ajax call for the new rows and update the grid's html:

var ajaxOptions = {
         name: '|refreshgrid|',
         url: this.options.refreshUrl,
         data: {
             items: items
         },
         context: this,
         success: $.proxy(function (response) {
              // update grid html
              if (response.RowsHtml) {
                   this.$grid.bsGrid('updateRows', response.RowsHtml);
              }
         }, this),
 };

$.bforms.ajax(ajaxOptions);

The controller action (refreshUrl) should look something like this:

public BsJsonResult Refresh(List<BsGridRowData<int>> items)
{    
       rowsHtml = GetRowsHtml(items);

       return new BsJsonResult(new
       {
             RowsHtml = rowsHtml
       }, status, msg);
}

Let me know if you managed to do it

meister-d commented 8 years ago

Hi Cristi, this works for me as expected. Thanx ;-)

Regards Dumitru