Login Register

QueryReadStore, sorting and paging

Hi,

I am using the grid with QueryReadStore using a Java servlet backing the store. As I am not too educated in Javascript (which is a bit of an overstatement given that I have never worked with Javascript before :-), it took me some time to make QueryReadStore work with my servlet and do proper paging (I have a relatively big database behind the servlet and the grid shows the database content).
However, I was unable to make paging work, once I sorted the grid. The first bunch of rows were properly fetched from the servlet, but after scrolling past the last item of the bunch, nothing happened, my servlet was not contacted for the next piece of the data set.

After having browsed model.js, I came to the conclusion that the reason for the paging not working was due to this piece of code in dojox.grid.data.Dynamic:

    requestPage: function(inPageIndex){
        var row = this.pageToRow(inPageIndex);
        var count = Math.min(this.rowsPerPage, this.count - row);
        if(count > 0){
            this.requests++;
            this.requestsPending(true);
            setTimeout(dojo.hitch(this, "requestRows", row, count), 1);
            //this.requestRows(row, count);
        }
    },

That is, the value of this.count was somehow not equal to the value returned by my getRowCount method but to the size of the first bunch retrieved after sorting.

So I tried to find a way to test my idea and set the size of the dataset in my sort method in my dojox.grid.data.DojoData model:

          <script type="dojo/method" event="sort" args="colIndex">
              // clears old data to force loading of new, then requests new rows
              this.sortColumn = colIndex;
              this.clearData();
              // Without this the grid does not do paging after the sort!!!
              this.setRowCount(this.getRowCount());
              this.requestRows();
          </script>

And, voilá! the grid now properly did the paging after sorting!!
Now, I have no idea if this is the right way to do the sort or I am simply using some side effect of the grid?

What I found, however, was that after a sort my servlet gets three requests for the row count! I understand one
 of them, it is the result of the this.getRowCount() call in my sort method.
But the other two I don't understand.
Again, looking at model.js I found that there are several getRowCount methods in it (in dojox.grid.data.Table and
in dojox.grid.data.Dynamic). Is it possible that as I overwrite the getRowCount method it gets called even for cases
when the intention was to simply return the value of this.count (in dojox.grid.data.Dynamic)?

Sorry if I couldn't properly express my issue, but if anyone needs more information, I am happy to provide it.
I can live with the actual status, but would like to make sure I am not doing something totally crazy :-)

And hope the code excerpts will appear properly!

Since I posted this issue, I recognized one more:

Paging was only working properly after sorting if the grid hasn't been paged before doing sorting. If I paged down the grid before sorting, then only the first page was requested. Paging further down after sorting suddenly resulted in a proper page request, so I ended up with a grid having the first and the Nth page displayed properly with empty (full of '?') rows in between.
I guess something was not properly reset still.
The only way I could force the grid to behave properly was to change the sort method of the grid to the following:

<script type="dojo/method" event="sort">
                this.render();
                this.edit.apply();
                this.model.sort(this.getSortField());
            </script>

With this change my grid is forced back to the beginning after the sort, but paging seems to be working properly.
Is this OK or there is a better way to solve this issue?

Cheers and thanks in advance,

Laca

How did you make

How did you make QueryReadStore work with your servlet? I could not get the onComplete method to set the items parameter. Can you share some code?

My requestRows method

Hi,

My requestRows method in my DojoData model looks like:

<script type="dojo/method" event="requestRows" args="inRowIndex, inCount">
              // console.error("inRowIndex: ", inRowIndex);
              // console.error("inCount: ", inCount);
              var row = inRowIndex || 0;
              var params = {
                    start: row,
                    count: inCount || this.rowsPerPage,
                    serverQuery: dojo.mixin(
                        { start: row,
                          count: inCount || this.rowsPerPage,
                          sort: (this.sortColumn || '')
                        },
                        this.query
                    ),
                    query: this.query,
            // sort: this.sortFields,
            // queryOptions: this.queryOptions,
            // onBegin: dojo.hitch(this, "beginReturn"),
                    onComplete: dojo.hitch(this, "processRows"),
                    onError: dojo.hitch(this, "processError")
              }
              this.store.fetch(params);
          </script>

So onComplete simply calls the processRows method.

Hope this helps!

Cheers,

Laca

Great work, laca!

This really saved my brain today.