【问题标题】:Filter gallery to show all items of a category and all sub categories筛选库以显示一个类别的所有项目和所有子类别
【发布时间】:2018-02-14 19:38:52
【问题描述】:

我正在尝试使用 PowerApps 创建一个库存应用程序,目前每个项目都属于一个主要类别,并且至少属于一个子类别。一个小例子是这样的:

Concessions
|    Food
|        Chips
|            Brand 1
|            Brand 2
|            Brand 3
|        Candy
|           Chocolate
|               Brand 1
|               Brand 2
|           Hard
|           Gum
|        Grill
|    Drinks
Travel Goods
|    TravelGoodsSubCat

这保存在一个谷歌表格中(连同同一本书中另一张表格中的库存数据),如下所示:

CatID    CatName            ParentCat
1        Concessions        0
2        Travel Goods       0
3        Food               1
4        Chips              3
5        Candy              3
6        TravelGoodsSubCat  2
.... And So on....

所以我设想的是两个画廊,一个在屏幕顶部水平显示,仅显示类别 0 的子类别。第二个是垂直画廊,显示当前类别下的每个项目.因此,如果用户在水平图库中选择让步,则垂直图库应填充分配给类别 1、3、4、5 等的所有项目。

在我看来,如何实现递归搜索以查找所有这些可能的子类别?

【问题讨论】:

    标签: google-sheets powerapps


    【解决方案1】:

    这是可以实现的,但为了不显着降低性能,您需要首先将类别表缓存在集合中。如果您使用 Google 表格作为数据源,情况尤其如此。

    然后您可以创建一个包含两列的集合:CatID 和 UltimateParentCat,如下所示:

    CatID  UltimateParentCat
    1      1
    2      2
    3      1
    4      1
    5      1
    6      2
    

    这是您将使用递归的地方,但对每一步都进行编码,因此您需要在您期望的最大深度处停止。

    因此,您需要将应用的 OnStart 属性或某些按钮的 OnSelect 属性设置为类似这样,以便只填充一次集合。

    ClearCollect(CachedCategories, GoogleSheetsTable);
    
    ClearCollect(Step_1,
        AddColumns(
            ShowColumns(
                Filter(CachedCategories, ParentCat=0),
                "CatID"
                ),
            "UltimateParentCat", CatID
            )
        );
    
    Clear(Step_2);
    
    ForAll(Step_1,
        Collect(Step_2,
            AddColumns(
                ShowColumns(
                    Filter(CachedCategories, ParentCat=Step_1[@CatID]),
                    "CatID"
                    ),
            "UltimateParentCat", Step_1[@UltimateParentCat])
            ));
    
    Clear(Step_3);
    
    ForAll(Step_2,
        Collect(Step_3,
            AddColumns(
                ShowColumns(
                    Filter(CachedCategories, ParentCat=Step_2[@CatID]),
                    "CatID"
                    ),
            "UltimateParentCat", Step_2[@UltimateParentCat])
            ));
    
    ClearCollect(CatsWithUltimate, Step_1, Step_2, Step_3)
    

    一旦你这样做了,那么水平画廊的 Items 属性应该是

    Filter(CachedCategories, ParentCat=0)
    

    那么垂直图库的 Items 属性应该是

    Filter(Products,
        ProductCategory in Filter(CatsWithUltimate,
            UltimateParentCat=GalleryHorizontal.Selected.CatID).CatID)
    

    请让我知道上面的任何错别字,以便我更正。

    作为旁注,我建议迁移到另一个数据源,如 Azure SQL 数据库,并将 CatsWithUltimate 生成为视图。

    【讨论】:

    • 唯一的注意事项(这可能是我可以在 google 表格方面更改的内容)是来自 google 表格的所有值似乎都是字符串,而不是数字。结果,我不得不将 Step1 中的 Filter 命令更新为 Filter(CachedCategories, Value(ParentCat)=0),
    • 另外,是否有一致的方法来触发 OnStart 属性?我最终在主屏幕上使用了一个按钮来缓存数据。它实际上似乎在数据可用之前触发,因此集合是空的......但有时它会起作用,提供混合结果。
    • 关于 OnStart 的不可靠性,希望随着 PowerApps 的最终成熟而有所改善。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-02
    • 1970-01-01
    • 1970-01-01
    • 2015-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多