如何根据表绑定值构造过滤器?

时间:2017-04-27 12:48:37

标签: sapui5

我有这样的表:

<Table
    id="table"
    items="{path: invoices}/>
    <headerToolbar>
        <Toolbar>
            <Title id="tableHeader" text="table"/>
            <ToolbarSpacer />
            <Button tooltip="View Settings" icon="sap-icon://drop-down-list" press="handleFilterPressed" />
        </Toolbar>
    </headerToolbar>
</Table>

发票如下:

{
  invoiceid: 1,
  business area: 1
}
{
  invoiceid: 2,
  business area: 2
}
{
  invoiceid: 3,
  business area: 1
}

handleFilterPressed看起来像这样:

handleFilterPressed : function(oEvent) {
    if (!this._oDialog) {
        this._oDialog = sap.ui.xmlfragment("vfb.view.Filter", this);
    }

    // toggle compact style
    jQuery.sap.syncStyleClass("sapUiSizeCompact", this.getView(), this._oDialog);               
    this._oDialog.open();
},

,过滤器的片段如下所示:

<core:FragmentDefinition
    xmlns="sap.m"
    xmlns:core="sap.ui.core">
    <ViewSettingsDialog
        confirm="onSetFilter">
        <filterItems>
            <ViewSettingsFilterItem
                text="Business area"
                key="BusArea"
                multiSelect="true"
                id="filterList">
                <items>
                </items>
            </ViewSettingsFilterItem>
        </filterItems>
    </ViewSettingsDialog>
</core:FragmentDefinition>

所以现在我的问题是:如何根据数据构建过滤列表项,过滤器将如下所示:

<items>
    <ViewSettingsItem text="1" key="1" />
    <ViewSettingsItem text="2" key="2" />
</items>

1 个答案:

答案 0 :(得分:2)

你应该在片段中进行聚合绑定:

invoices:[
  {
    invoiceid: 1,
    businessArea: 1
  },
  {
    invoiceid: 2,
    businessArea: 2
  },
  {
    invoiceid: 3,
    businessArea: 1
  }
]


<core:FragmentDefinition
    xmlns="sap.m"
    xmlns:core="sap.ui.core">
    <ViewSettingsDialog
        confirm="onSetFilter"
        filterItems="{/invoices}">
        <filterItems>
            <ViewSettingsFilterItem
                text="{businessArea}"
                key="{businessArea}">
            </ViewSettingsFilterItem>
        </filterItems>
    </ViewSettingsDialog>
</core:FragmentDefinition>

但是,正确的方法是在JSON模型(或OData模型中的entitySet)中创建一个列出businessAreas的节点。否则,如果您使用当前的JSON,则在执行聚合绑定时将获得重复项。 所以用这样的东西改进它:

invoices:[
  {
    invoiceid: 1,
    businessArea: 1
  },
  {
    invoiceid: 2,
    businessArea: 2
  },
  {
    invoiceid: 3,
    businessArea: 1
  }
],
businessAreas:[
  {
    businessAreaName: "Business 1"
    businessArea: 1
  },
  {
    businessAreaName: "Business 2"
    businessArea: 2
  }
]


<core:FragmentDefinition
    xmlns="sap.m"
    xmlns:core="sap.ui.core">
    <ViewSettingsDialog
        confirm="onSetFilter"
        filterItems="{/businessAreas}">
        <filterItems>
            <ViewSettingsFilterItem
                text="{businessAreaName}"
                key="{businessArea}">
            </ViewSettingsFilterItem>
        </filterItems>
    </ViewSettingsDialog>
</core:FragmentDefinition>

我认为这些小改动对你有所帮助。