Persisting Filter conditions in EP Grid


Persisting Filter condition in EP Grid


In AX 2009, EP grid control comes with an advanced filter. Here is a code to persist the filter condition the user last entered in a page for that user, so that later when the user comes back to the same page the last filter is applied and user didn’t have to reenter then.


This code sample assumes a dataset with the name “EPSalesTableList” which contains the datasource “SalesTable”


 (1) First override the pack method of the dataset ( EPSalesTableList in this example) in AOT


public container pack()
{


container ret;
SysLastValue sysLastValue;


formdatasource filterDataSource;
;


filterDataSource = SalesTable_ds; // Put your datasource name


    ttsbegin;
       // Delete last saved query for the current dataset
    delete_from sysLastValue
        where sysLastValue.Company      == curext()
           && sysLastValue.UserId       == curuserid()
           && sysLastValue.RecordType   == UtilElementType::DataSet
           && sysLastValue.ElementName  == filterDataSource.name()
           && sysLastValue.DesignName   == filterDataSource.name();


    // If there is a new queryRun() object then serialize and save it
    // in the sys last value table
    // Put your datasource name
   
    if (filterDataSource.queryRun())
   {
   
        sysLastValue.RecId = 0;
        sysLastValue.Company      = curext();
        sysLastValue.UserId       = curuserid();
        sysLastValue.RecordType   = UtilElementType::DataSet;
        sysLastValue.ElementName  = filterDataSource.name();
        sysLastValue.DesignName   = filterDataSource.name();
        sysLastValue.value = SysQuery::packRangeAndSortorder(filterDataSource.queryRun().query());   
        sysLastValue.insert();
    }
    ttscommit;


 


    ret = super();


 


 


    return ret;


}


 


 


(2) Second override the init method of the datasource ( SalesTable in this example) within that dataset in AOT


public void init()
{
SysLastValue    sysLastValue;
    Query savedQuery;
    ;
    super();


// get the last value from the  sys last value table
    select firstonly sysLastValue
        where sysLastValue.Company      == curext()
           && sysLastValue.UserId       == curuserid()
           && sysLastValue.RecordType   == UtilElementType::DataSet
           && sysLastValue.ElementName  == this.name()
           && sysLastValue.DesignName   == this.name();


    if (sysLastValue && sysLastValue.Value)
    {
              // If there is an unpack error delete the saved query


       if (!SysQuery::unpackRangeAndSortorder(this.query(), sysLastValue.value))
        {


            ttsbegin;
            delete_from sysLastValue
                where sysLastValue.Company      == curext()
                   && sysLastValue.UserId       == curuserid()
                   && sysLastValue.RecordType   == UtilElementType::DataSet
                   && sysLastValue.ElementName  == this.name()
                   && sysLastValue.DesignName   == this.name();
            ttscommit;
        }


    }
               


 


 Make sure you replace the datasourcename variable filterDataSource in the pack method with your datasource name.Save the changes, refresh AOD and now go to the page that is using this dataset and enter some filter condition & apply. Close the browser and reopen the page. Now you should see that the filter that you last applied before the browser was closed is still applied in this page.


Arif Kureshy, who is the dev manager for EP came out with this code snippet. So thanks to him for providing this sample code.


 

Comments (5)

  1. Fradim9 says:

    Meysun, hello!  

    Many thanks for your articles, we've fixed a lot of issues in our EP 🙂

    Just one question about current topic. It works fine if user close and reopen browser. But let's imagine a little bit different situation. User applied some filters, had some entries/documents in Grid and went through one of the documents from Grid (just clicked on the link). After looking the document he just pushed browser's Back button and… returned to previous page without applied filters 🙁  Is there the way to fix it? I mean, apply previous filters even the user just pushed browser's Back button, not reopen Browser?

    We've found Michael Nilsson' site http://ajaxhistory.com/ with general suggestion, should we go this way, or there is different solution especially for EP?

  2. Fradim9 says:

    Meysun, hello!  

    Many thanks for your articles, we've fixed a lot of issues in our EP 🙂

    Just one question about current topic. It works fine if user close and reopen browser. But let's imagine a little bit different situation. User applied some filters, had some entries/documents in Grid and went through one of the documents from Grid (just clicked on the link). After looking the document he just pushed browser's Back button and… returned to previous page without applied filters 🙁  Is there the way to fix it? I mean, apply previous filters even the user just pushed browser's Back button, not reopen Browser?

    We've found Michael Nilsson' site http://ajaxhistory.com/ with general suggestion, should we go this way, or there is different solution especially for EP?

  3. Einar Lárusson says:

    Make sure that the PersistState property on the data source is set to True

  4. Einar Lárusson says:

    It is also good to look at this one and use it at the same time

    Always keep the Filter section expanded in AxGridView

    community.dynamics.com/…/always-keep-the-filter-section-expanded-in-axgridview.aspx

  5. Don Shields says:

    Is there any trick to get this to work on Gridview with multiple datasources on the DataSet?  I have one particular page where it refuses to work.  PersistState=true, so that's not the issue.