Form Parts: Creating form method callbacks

来源:互联网 发布:低周波按摩仪 知乎 编辑:程序博客网 时间:2024/05/20 07:15

http://www.exploreax.com/blog/blog/2014/12/01/form-parts-creating-form-method-callbacks/

  1. Create your form and form parts.
  2. Add your form part to your main form
  3. On your main form create a reference to your form part in the class declaration e.g.Object _part;
  4. Create a method on your main form e.g. “registerForChange(Object _part); with the following code:
    public void registerForChange(Object _part)
    {
        part = _part;
    }
    This method will allow your form part to provide a reference of itself to the main form.
  5. On your form part’s init method. Call this method register call the above method passing itself as a reference. Note: you will need to perform step #4 on all forms that will use this form part.
    public void init()
    {
        Object caller;
        caller= args.caller();
        if(caller)
        {
            caller.registerForChange(this)
        }
    }
  6. On your form part create an doRefresh method, your main form will call this method whenever a form part refresh is needed. This method can have an parameter that your would like the main form to pass through. In this example we will pass the active record from the main form  E.G.
    publicvoid doRefresh(Common _record)
    {
    //Do custom form part refresh.
    }
  7. Finally, on your main form, call the the method in #6 at the appropriate time. In this example calling “part.doRefresh(myTable);” in the myTable datasource’s active method works well. You could also do your call from a button on the main form or on any other trigger.

ListPage variation

If you are planning on using the form part as part of a list page. Youneed to make the following adjustments.

  1. Create a reference to your part in the list page’s ListPageInteractionClass’ ClassDeclaration e.g. Object part;
  2. For step #4 add the “registerForChange” to your list page’s ListPageInteractionClass
  3. For step #5 detect whether your part is being called from a ListPage or a normal form: E.G.
    public void init()
    {

        Object caller;
        SysSetupFormRun formRun;
          super();
        caller
    = this.args().caller();
        if(caller)
        {
                formRun = caller;
                if (formRun.pageInteraction())
                {
                      caller = formRun.pageInteraction();
                      caller
    .registerForChange(this);
                }

else
{
      caller.registerForChange(this);
}

}
}

  1. On the relevant method in your list page interaction class call the part’s doRefresh method. For this example use the selectionChanged method
    public void selectionChanged()
    {
        ListPage listPage = this.listPage();
        if (listPage.activeRecord(“MyTable”))
        {
            part.doRefresh(listPage.activeRecord(“MyTable”));
        }
    }

 

Refreshing form parts

When using form parts in AX 2012, you sometimes need to explicitly refreshtheir data based on an event in the main form. It may not be completely obvioushow to do it, but it’s not too complicated in the end.

Form parts are actually forms by themselves and if you know how tomanipulate forms at runtime, you know how to work with parts too. The trickypart it getting a reference to a form part.

One of possible solutions is adding the following method toSysSetupFormRun class (so it’s available to all forms):

public FormRun getFormPartByName(str _name)

{

   PartListpartList = newPartList(this);

   FormRun part;

   int i;

 

   for(i= 1; i <= partList.partCount();i++)

   {

       part = partList.getPartById(i);

 

       if (part.name() == _name)

       {

           return part;

       }

   }

   return null;

}

As you see, it iterates all parts in the form and finds a part with thegiven name.

Then you can call it from your form to get a reference to a particularpart and do anything you like with it, such as refreshing the data:

publicvoid refreshMyFactBox()

{

   SysSetupFormRunformRun = this as SysSetupFormRun;

   FormRun factBox= formRun.getFormPartByName('MyInfoPart');

 

   if (factBox)

   {

       factBox.dataSource().research();

   }

}
原创粉丝点击