【开发示例】单据体控件分页加载第三方API接口请求的数据原创
金蝶云社区-唐佐平
唐佐平
3人赞赏了该文章 640次浏览 未经作者许可,禁止转载编辑于2024年02月22日 19:40:11

一、需求背景

       在实际业务中,经常碰到,需要将外部第三方API接口返回的数据,在单据体控件中展示,如果数据量少,不需分页,在表单插件中,直接将数据填充到单据体控件中就行,无需特别处理

       本文章要实现的效果是:第三方API接口有分页参数,每次在单据体控件中展示当前页数据,点击下一页操作时,实时调用第三方API接口,获取数据,在单据体展示,这种场景实现方式请参考本文的步骤实现。


二、实现方案

1.  重写EntryGrid控件

       重写EntryGrid控件,实现在切换页码的时候,加载自定义的数据,主要是重写翻页事件onFetchPageData,在此事件中,调用外部第三方API接口,获取数据,填充到单据体控件中。

/**
 * 重写EntryGrid控件,实现在切换页码的时候,加载自定义的数据
 *
 */
public class CustGridControl extends EntryGrid {

    /**
     * 重写翻页取数方法
     * 调用外部第三方API接口,获取数据,填充到单据体进行展示
     *
     * @param pageNo    当前页码,从1开始
     * @param pageSize  每页记录行数
     */
    @Override
    protected boolean onFetchPageData(int pageNo, int pageSize) {
        //1、先清空单据体控件的数据
        DynamicObject dataEntity = this.getModel().getDataEntity(true);
        DynamicObjectCollection entryEntities = dataEntity.getDynamicObjectCollection(this.getKey());
        entryEntities.clear();

        //2、调用第三方API接口获取当前页数据
        //TODO
		
		//3、填充数据到单据体 TODO
        for(Row row : queryDataSet) {
            DynamicObject entryEntity = entryEntities.addNew();
            entryEntity.set("fieldKey1", row.getString("fieldKey1"));
            entryEntity.set("fieldKey2", row.getString("fieldKey2"));
            entryEntity.set("fieldKey3", row.getString("fieldKey3"));
			//......
        }

        //数据起始行索引更新:
		//① 单据体序号显示
		//② setValue赋值时,序号要一致,比如:5行/页,展示第二页数据时,第一行显示序号是6(RowIndex是5)
		//赋值示例:this.getModel.setValue("key","value",5); 是对第二页第一行字段赋值
		//③ 如果你想第二页第一行序号页从1开始,则设置起始序号从0开始即可:setEntryStartRowIndex(this.getKey(), 0);
        int startRowIndex = (pageNo - 1) * pageSize;
        dataEntity.getDataEntityState().setEntryStartRowIndex(this.getKey(), startRowIndex);
        dataEntity.getDataEntityState().setEntryPageSize(this.getKey(), pageSize);
		//设置分录记录总行数
        dataEntity.getDataEntityState().setEntryRowCount(this.getKey(), rowCount);

        return true;
    }
}


完整代码示例参考文章后面的附件:CustGridControl.java


2.  设置单据体表格为自定义控件


       在表单插件的初始化事件initialize中设置,将单据体控件设置为自定义控件:

@Override
public void initialize() {
   super.initialize();
   // 设置单据体表格为自定义控件,ENTRY_KEY是单据体控件的标识
   this.getView().addCustomControls(ENTRY_KEY);
}


3. 重写onGetControl方法,创建自定义表格控件

       重写表单插件的onGetControl方法,在这个方法里面,创建自定义表格控件,代码如下:

@Override
public void onGetControl(OnGetControlArgs e) {
   super.onGetControl(e);
   String entryKey = e.getKey();
   //创建自定义的表格控件实例
   if (entryKey.equalsIgnoreCase(ENTRY_KEY)){
      Control oldGrid = e.getControl();
      if (oldGrid == null){
         return;
      }
      CustGridControl newCustGrid;
      if (oldGrid instanceof CustGridControl){
         newCustGrid = (CustGridControl)oldGrid;
      }else{
         newCustGrid = new CustGridControl();
      }
      newCustGrid.setView(this.getView());
      newCustGrid.setKey(entryKey);
      newCustGrid.setEntryKey(entryKey);
      newCustGrid.setSplitPage(true);
      if (oldGrid instanceof EntryGrid){
         //将设计器设置的字段赋值到新的控件上
         newCustGrid.getItems().addAll(((EntryGrid) oldGrid).getItems());
         //将设计器设置的分页条数赋值到新的控件上
         newCustGrid.setPageRow(((EntryGrid) oldGrid).getPageRow());
      }
      //把新创建的表格控件,传回给平台(使新创建的单据体控件生效)
      e.setControl(newCustGrid);
   }  //end if
}


4. 在afterBindData事件中,加载第一页数据

@Override
public void afterBindData(EventObject e) {
   super.afterBindData(e);
   //调用setPageRows,会触发onFetchPageData事件,获取第一页数据
   //建议在表单的afterBindData事件中调用,afterCreateNewData事件中,获取到的entry.getPageRow()不是设计器中设计的值
   EntryGrid entry = this.getView().getControl(ENTRY_KEY);
   entry.setPageRows(entry.getPageRow());
}


5. 启用单据体控件分页

       在开发平台,进入表单,打开单据体控件的分页属性:

image.png


经过上述5个步骤已经完成在单据体中分页加载外部数据的功能,运行效果如截图:


image.png


三、注意事项


  • 在苍穹V6.0调试通过

  • 单据体表头过滤不支持跨页过滤

  • 不支持平台的引出功能


图标赞 3
3人点赞
还没有人点赞,快来当第一个点赞的人吧!
图标打赏
0人打赏
还没有人打赏,快来当第一个打赏的人吧!

您的鼓励与嘉奖将成为创作者们前进的动力,如果觉得本文还不错,可以给予作者创作打赏哦!

请选择打赏金币数 *

10金币20金币30金币40金币50金币60金币
可用金币: 0