Monday, December 20, 2010

Filter datasources of same table on Tab Pages

I had a requirement to show the filtered data of one table on different tabs within a form. The standard filter functionality in AX forms is a powerful feature. Using this filter functionality in your code is something you'll definitely use at some point in time as a programmer.
Let me explain it with an example; we have a currency table with multiple currencies e.g. EUR and USD. I have two different tabs named ‘EUR’ and ‘USD’ and these will show the respective filtered data from the currency table i.e. EUR tab will show the EUR currency and USD will show the USD currency data.
Add two datasources (CurrencyEUR and CurrencyUSD) on the form of the same table (Currency), after assigning these datasources to the tabpages. You just need to follow the following 3 steps.
Step 1: Declare a class variable
In the ClassDeclaration method of the form, define a range.
QueryBuildRange CurrencyQBREUR;
QueryBuildRange CurrencyQBRUSD;



Step 2: Instantiate the new range.
In the init() method on the datasource of the form, you assign the range to a specific field (after the super call).

DataSource: CurrencyEUR
public void init()
{
super();

CurrencyQBREUR = this.query().dataSourceName('Currency').addRange(fieldnum(Currency, CurrencyCode));
}
DataSource: CurrencyUSD
public void init()
{
super();

CurrencyQBRUSD = this.query().dataSourceName('Currency').addRange(fieldnum(Currency, CurrencyCode));
}

Step 3: In the last step, you assign a value to the range.
This is done in the executeQuery method on the same datasource of the form. Before the super call. Like this:

DataSource: CurrencyEURpublic void executeQuery()
{ ;

CurrencyQBREUR.value(queryvalue('EUR'));

super();
}
DataSource: CurrencyUSDpublic void executeQuery()
{ ;

CurrencyQBRUSD.value(queryvalue('USD'));

super();
}
This can be done in one line of code as well. In the init() method of the form datasource, after the super call, place this code:
this.query().dataSourceName(‘Currency’).addRange(fieldnum(‘Currency’,CurrencyCode)).value(queryvalue('USD'));

But this way, it's fixed. If you choose the 3 step method, you could for example use a variable in the range value. The way to go would be to place an input field on your form. Get the value from it and supply it in the executeQuery() method.
For example like this:
public void executeQuery()
{ ;
CurrencyQBRUSD.value(queryvalue(MyInputField.text()));
super();
}

Just make sure the executeQuery method is executed, thus applying the desired filter (maybe be using a button on your form to activate it).

No comments:

Post a Comment

I will appreciate your comments !

How to enable new Microsoft teams - Public Preview!

New Microsoft Teams is just AWESOME, quick but useful post below shows how you have this preview feature to make your life EASY!  Open Micr...