JavaBlog.fr / Java.lu DEVELOPMENT,Sencha,WEB Sencha/ExtJs: How to clear the store and update a paging toolbar?

Sencha/ExtJs: How to clear the store and update a paging toolbar?

A small post on a problem encountred with EXTJS 4.0.2. I have a GridPanel with a paging toolbar. This grid is loaded with store (ajax, server; json…etc). And the paging toolbar allows the navigation in all pages with each time, a automatical ajax request is done from client’s store to server. During these exchanges, (at each request) the parameters ‘page’, ‘start’ and ‘limit’ are also automaticaly sent in order to delimit the scope of data to get for the grid. More, there is a simple search form which must reload the store, grid and paging toolbar with a new request to server containing the new params filled by the user.

Here the code of the “search” button’s handler which merges the previous params with the new params filled by the user and load the store (ajax request from store to server):

01{
02    xtype: 'button',
03    id: 'documentListSearchPanelViewSearchButtonID',
04    text: 'Search'
05    ,handler: function() {
06            var form = this.up('form').getForm();
07     
08            [...]  
09            <!-- GridPanel -->
10            var gridPanelInCurrentTabTemp = [...];
11 
12            <!-- Loading STORE   -->
13            var storeGridPanelInCurrentTabTemp = Ext.getCmp(gridPanelInCurrentTabTemp.id).getStore();
14 
15            <!-- Merge the previous extraparams with the new params filled by the user   -->
16            var extraParamsPrevious = storeGridPanelInCurrentTabTemp.proxy.extraParams;
17            storeGridPanelInCurrentTabTemp.proxy.extraParams = Ext.Object.merge(extraParamsPrevious, form.getValues());
18 
19            storeGridPanelInCurrentTabTemp.load(
20                {
21                    callback: function(documents, options, success){
22                        if (success == false) { // Timeout
23                            Ext.MessageBox.alert('alert', 'timeout');
24                        }
25                    }
26                }
27            );
28    }
29}

A piece of Store codes:

01Ext.define(huo.store.myHuoStore', {
02    extend: 'Ext.data.Store',
03     
04    <!-- autoLoad : true,-->
05    autoLoad : false,
06    autoSave : false,
07    remoteSort: true,
08    pageSize:50
09     
10    // Store to reference the Model name instead of providing fields inline,
11    ,model: 'huo.model.MyHuoModel'
12    ,proxy: {
13        type: 'ajax',
14 
15        api: {
16            // get data from server
17            read: 'myRemoteAjaxService.do?action=getMeAll'
18        },
19        params:{
20            start:0,
21            limit:50
22        },
23[...]
24}

This is a normal case, nothing is special.

In server side, we print in console all parameters received with the simple below code (java code):

01Map params = request.getParameterMap();
02System.out.println("----- NEW REQUEST -------");
03for (Iterator<String> iterator = params.keySet().iterator(); iterator.hasNext();) {
04    String key = (String) iterator.next();
05    //
06    String str = " ";
07    for (int i = 0; i < request.getParameterValues(key).length; i++) {
08        str = str + request.getParameterValues(key)[i];
09    }
10    System.out.println("key="+key+" - value="+str);
11    //
12}

Scenario of problem:
1. First the request to load the grid, from the store (OK):

1----- NEW REQUEST -------
2key=limit - value= 50
3key=page - value= 1
4key=start - value= 0
5...

2. We change the page of grid so a new request is sent to load the grid, from the store (OK):
The ‘start’ param has the value 100 because we want the data from the page 3 => (3-1)x50 =100; (page 1=> start=0, page 2=> start=’50’..etc).

1----- NEW REQUEST -------
2key=limit - value= 50
3key=page - value= 3
4key=start - value= 100

3. There is a problem if the user uses the search form a new resquest is sent from store to server (NOK):

1----- NEW REQUEST -------
2key=limit - value= 50
3key=page - value= 3
4key=start - value= 100

The store/grid is loaded however the params are not initialized, and the previous values are sent to server.
More the paging toolbar is not updated too.

The code of store’s loading was:

1storeGridPanelInCurrentTabTemp.load(
2                {
3                    callback: function(documents, options, success){
4                        if (success == false) { // Timeout
5                            Ext.MessageBox.alert('alert', 'timeout');
6                        }
7                    }
8                }
9            );

Conclusion
My investigations/tests have led to that:

  • the ‘overriding’ of params during the load doesn’t work:
    01store.load({
    02    params:{
    03        page: 1,
    04        start: 0,
    05        limit: 50
    06    },
    07    extraParams={
    08        param1:value,
    09        param2:value
    10    },
    11    callback: function(documents, options, success){
    12        if (success == false) { // Timeout
    13            Ext.MessageBox.alert('alert', 'timeout');
    14        }
    15    }
    16});
  • there is a solution which works by replacing the call of ‘load’ method by a call to ‘loadPage(1)’ method of store:
    1store.proxy.extraParams={
    2    param1:value,
    3    param2:value
    4}
    5store.loadPage(1);

    … however, this solution in my case dosen’t work totally, because I need a callback on the store’s loading in order to display a timeout or confirmation message. More, as described in the Sencha forum site the Store loadPage doen’t accept a 2nd argument as stated in documentation. This bug seems be fixed in version 4.0.6!

  • The FINAL solution is to set the page nuumber manually before the store loading like:

    1<!--  Set the page nuumber manually -->
    2storeGridPanelInCurrentTabTemp.currentPage = 1;
    3storeGridPanelInCurrentTabTemp.load({
    4    callback: function(documents, options, success){
    5        if (success == false) { // Timeout
    6            Ext.MessageBox.alert('alert', 'timeout');
    7        }
    8    }
    9});

    …and the outputs are:

    01----- NEW REQUEST -------
    02key=limit - value= 500
    03key=page - value= 1
    04key=start - value= 0
    05 
    06 
    07----- NEW REQUEST -------
    08key=limit - value= 50
    09key=page - value= 3
    10key=start - value= 100
    11 
    12 
    13----- NEW REQUEST -------
    14key=limit - value= 50
    15key=page - value= 1
    16key=start - value= 0

That’s all! 🙂

Huseyin OZVEREN

3 thoughts on “Sencha/ExtJs: How to clear the store and update a paging toolbar?”

Leave a Reply to Anna Cancel reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

Related Post