# DataGrid - rows disappears after sequence - newItem, deleteItem, sort

**URL:** <https://discourse.dojo.io/t/datagrid-rows-disappears-after-sequence-newitem-deleteitem-sort/111>\
**Category:** Dojo 1.x users\
**Created:** [January 2, 2018, 9:15pm UTC](https://discourse.dojo.io/t/datagrid-rows-disappears-after-sequence-newitem-deleteitem-sort/111 "2018-01-02T21:15:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![KristoffSC](https://avatars.discourse-cdn.com/v4/letter/k/fbc32d/32.png) [@KristoffSC](https://discourse.dojo.io/u/KristoffSC)\
**Post date:** [January 2, 2018, 9:15pm UTC](https://discourse.dojo.io/t/datagrid-rows-disappears-after-sequence-newitem-deleteitem-sort/111/1 "2018-01-02T21:15:28Z")

</div>

HI all,  
I’ve come across an issue with DataGrid when sorting the grid.

With this isseue, all rows disappears when we execute this sequence of actions in one call flow:

1. add one Item to the grid’s store.
2. delete one item from the grid’s store
3. save store
4. call grid.sort.

After calling the grid.sort all rows from grid diapers (although they are still in the store). They are back if we repeat the same procedure.

I have a reproducer code for this, my custom Grid component based on DataGrid is here:  
[MyGrid.js](https://github.com/kristoffSC/MyGridDojo/blob/master/src/main/webapp/resources/MyGrid.js)

If you call the addOneDelOne of MyGrid like I’m doing in line 38 of [Scenarios.js](https://github.com/kristoffSC/MyGridDojo/blob/master/src/main/webapp/resources/Scenarios.js) You will be able to reproduce the issue.

In this code, items are removed from the store if there are more than 4 items.

Also the issue does not appear if we sort the grid if the number of added items are grater than deleted items.

I’ve tested it on Dojo 1.9 and 1.13

---

<div class="post-metadata">

**Author:** ![KristoffSC](https://avatars.discourse-cdn.com/v4/letter/k/fbc32d/32.png) [@KristoffSC](https://discourse.dojo.io/u/KristoffSC)\
**Post date:** [January 18, 2018, 9:40pm UTC](https://discourse.dojo.io/t/datagrid-rows-disappears-after-sequence-newitem-deleteitem-sort/111/2 "2018-01-18T21:40:23Z")

</div>

I believe we found the solution for this problem.  
Issue was with \_onFetchBegin method of Dojo’s DataGrid.

When we call sort on DataGrid it actually clears some counters by calling updateRowCount(0).

One of the counters that is cleared is **this.invalidated.rowCount** which is actually used by DataGrid and dojo to print the records. The number of printed records is equal to this.invalidated.rowCount value.

Later, during the grid.sort() call processing, dojo goes to the **\_fetch(0, isRender);** method. This method eventually calls this.store.fetch({…}) which updates the grid based on items returned from store. It is done by callback methods \_onFetchBegin and \_onFetchComplete. The problem was in **\_onFetchBegin**

In our case the number of items returned from store was equal to the number row in grid. _if(this.rowCount != size) \<–_ **this is false**. Mind that we dont use this.invalidated.rowCount here but this.rowCount. this.invalidated.rowCount was set to 0 in updateRowCount(0) previously. Normally, when this condition would be true we would call this.updateRowCount(size); where size is the number of items in the store. And dojo would print the grid with rows. But in our case, **this.rowCount** and **size** was the same (we add on record, and remove one record) so we ended up couple lines further in DataGrid

```
if(!size){
		this.views.render();
		this._resize();
		this.showMessage(this.noDataMessage);
		this.focus.initFocusView();
	}else{
		this.showMessage(); <- we ended up here. And we we do not update the RowCount :(
	}

```

If we add **this.updateRowCount(size);** before **this.showMessage();**

like this

```
else{
		this.updateRowCount(size);
		this.showMessage();
	}

```

It will be ok.

---

<div class="post-metadata">

**Author:** ![dylan](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dojo.io/dylan/32/5_2.png) [@dylan](https://discourse.dojo.io/u/dylan)\
**Post date:** [February 7, 2018, 5:03pm UTC](https://discourse.dojo.io/t/datagrid-rows-disappears-after-sequence-newitem-deleteitem-sort/111/3 "2018-02-07T17:03:15Z")

</div>

The DataGrid does not get a lot of love as most Dojo 1 users use dgrid today. If you want to raise a PR at [https://github.com/dojo/dojox/](https://github.com/dojo/dojox/) per the contributing guidelines at [https://github.com/dojo/dojox/blob/master/CONTRIBUTING.md](https://github.com/dojo/dojox/blob/master/CONTRIBUTING.md) , then I will consider landing it for a future release.
