Thursday, 10 December 2015

Adding filter on display fields in ax2012

Here is a sample example scenario where you add a display field on the form and make this field to be allowed to filter.

Create a table TEST



Create form TEST




Data source à TEST
Add fields Test_Id, Test_ActivityNumber, ActivityName from the datasource TEST
FormControl à Activity name à auto declaration YES
Data method à displayActivityName

Then override context method on the formcontrol and write the following code :

public void context()
{
    int             selectedMenu;
    formrun         fr;
    Args            ag;
    Name            strtext;
    querybuilddataSource qb1;
    queryrun    qr;
    query       q;
    PopupMenu menu = new PopupMenu(element.hWnd());
    int a = menu.insertItem('Filter By Field');
    int b = menu.insertItem('Filter By Selection');
    int c = menu.insertItem('Remove Filter');
    ;


    q   = Test_ds.query();
    qb1 = q.dataSourceTable(tablenum(Test));
    qb1 = qb1.addDataSource(TableNum(smmActivities));
    qb1.addLink(FieldNum(Test,ActivityNumber),FieldNum(smmActivities,ActivityNumber));

    selectedMenu = menu.draw();
    switch (selectedMenu)
    {
    case -1: //Filter by field
            break;
    case a:
            ag = new args('SysformSearch');
            fr = new formrun(ag);
            fr.run();
            fr.wait();
//Reading User entered value for filter process
            strtext = fr.design().controlName('FindEdit').valueStr();
            if(strtext)
            {
//Creating a query for filter

                qb1.addRange(FieldNum(smmActivities,Purpose)).value(strtext);
                Test_ds.query(q);
                Test_ds.executeQuery();
            }
            break;

    case b:                                      // Filter By Selection

            qb1.addRange(FieldNum(smmActivities,Purpose)).value(ActivityName.valueStr());
            Test_ds.query(q);
            Test_ds.executeQuery();
            break;

    case c :                                      // Remove Filter
            q   = new Query();
            qb1 = q.addDataSource(tablenum(Test));
            qb1.clearLinks();
            qb1.clearRanges();
            Test_ds.query(q);
            Test_ds.removeFilter();
            break;

    Default:
            break;

    }
}








No comments:

Post a Comment