SlickGrid icon indicating copy to clipboard operation
SlickGrid copied to clipboard

formatter called twice for each row

Open DemersM opened this issue 10 years ago • 3 comments

if you are using an ajax loader such

$.ajax({
      type: "GET",
      url: "/basqui/layer/shapefile/attributesTable/loader/1/",
      success: function(r) {
                  dataView.beginUpdate();
                  dataView.setItems(r);
                  dataView.setFilter(filter);
                  dataView.endUpdate();
                },
});

with those standard dataview events subscribed:

dataView.onRowCountChanged.subscribe(function (e, args) {
  grid.updateRowCount();
  grid.render();
});

dataView.onRowsChanged.subscribe(function (e, args) {
  grid.invalidateRows(args.rows);
  grid.render();
});

each row are formatted twice ( You can see this by using a custom formatter for a column and console.log() in its function ).

Is this normal?

DemersM avatar Jan 07 '15 16:01 DemersM

You're calling .render() on each event and one of them has .invalidateRows() (as it should), so yes, this is expected behaviour.

Advanced / Suggestion how to fix/optimize this out

If you want to optimize the .render() call count, you can do by, for example, queueing the .render() in a delayed function (I use a 'debouncer' object for this which encodes such behaviour in a generic way, inspired by the 'debouncer circuits' used to 'debounce' physical buttons by experienced electronic engineers). The fundamental approach here is to set a timer (and kill and retrigger it in either one event) which fires off the .render() when the timeout expires.

The 'debouncer' approach is a good way of doing this as you cannot predict the number of events (1 or 2) here unless you do some deep analysis of the data you're feeding the SlickGrid animal and that would thwart the concept of 'black boxing' the component.

Met vriendelijke groeten / Best regards,

Ger Hobbelt


web: http://www.hobbelt.com/ http://www.hebbut.net/ mail: [email protected]

mobile: +31-6-11 120 978

On Wed, Jan 7, 2015 at 5:42 PM, DemarsM [email protected] wrote:

if you are using an ajax loader such

$.ajax({ type: "GET", url: "/basqui/layer/shapefile/attributesTable/loader/1/", success: function(r) { dataView.beginUpdate(); dataView.setItems(r); dataView.setFilter(filter); dataView.endUpdate(); }, });

with those standard dataview events subscribed:

dataView.onRowCountChanged.subscribe(function (e, args) { grid.updateRowCount(); grid.render(); });

dataView.onRowsChanged.subscribe(function (e, args) { grid.invalidateRows(args.rows); grid.render(); });

each row are formatted twice ( You can see this by using a custom formatter for a column and console.log() in its function ).

Is this normal?

— Reply to this email directly or view it on GitHub https://github.com/mleibman/SlickGrid/issues/1043.

GerHobbelt avatar Jan 08 '15 21:01 GerHobbelt

You debounce suggestion make sense. Do you suggest to add a debouncer on render() in the main branch or not?

I imagine there is many events (such resize) that may trigger render() repeatedly and could benifit of a debouncer

DemersM avatar Jan 09 '15 13:01 DemersM

...in the meantime here's a workaround:

let t = null;
function debouncedRender() {
	if (t) clearTimeout(t);
	t = setTimeout(grid.render, 50);
}

Use debouncedRender instead of grid.render.

haqk avatar Nov 02 '21 15:11 haqk