Implementation of Javascript logic in power fx - canvas app

98 Views Asked by At

I am pretty new to Canvas app and I was actually looking for implementation of JS logic in power fx. Power fx is a low code language but its very complex while applying the logic to it when the filters are nested. Is there any way I could implement the following JS code in Power fx.

let Vehicles = [
  {name:'foo bar', vehicleFeatures: ["Bow", "Feature2"]},
  {name:'hello world', vehicleFeatures: ["Bow", "Row"]},
  {name:'mind games', vehicleFeatures: ["Test"]},
  {name:'new world', vehicleFeatures: ["Bow", "Row", "Feature3"]},

];

let selectedFeaturesToFilter = ["Bow", "Row"];


let filteredVehicles = Vehicles.filter(vehicle=>{
    if(vehicle.vehicleFeatures.every(feature=>selectedFeaturesToFilter.includes(feature))){
        return vehicle;
    }
});

console.log(filteredVehicles);

In case of canvas app I have selectedFeaturesToFilter equivalent to the Combobox.SelectedItems and 'Vehicle' and 'Vehicle Features' are two tables with many to many relationship. I need to get the filteredVehicles.

1

There are 1 best solutions below

4
On

Is there any way I could implement the following JS code in Power fx.

Yes, you can do this with PowerFx. On your App OnStart, create the data:

Vehicles collection

ClearCollect(
    Vehicles,
    {
        name: "foo bar",
        vehicleFeatures: ["Bow", "Feature2"]
    },
    {
        name: "hello world",
        vehicleFeatures: ["Bow", "Row"]
    },
    {
        name: "mind games",
        vehicleFeatures: ["Test"]
    },
    {
        name: "new world",
        vehicleFeatures: ["Bow", "Row", "Feature3" ]
    }
);

Features filter

Set(selectedFeaturesToFilter, ["Bow", "Row"]);

Filter vehicles with the desired features

ClearCollect(FilteredVehiclesCollection,
    ForAll(selectedFeaturesToFilter As featureToSelect,
        Filter(Vehicles, featureToSelect in vehicleFeatures)
    )
);

Merge results

Now, as you have two features to filter, the collection FilteredVehiclesCollection has two rows. If you want to have a single row with the filtered vehicles, you need to merge the rows. This code is tricky, but it's working:

ClearCollect(FilteredVehiclesCollectionMerged, First(FilteredVehiclesCollection));

Collect(FilteredVehiclesCollectionMerged, ForAll(FilteredVehiclesCollection, Patch(FilteredVehiclesCollectionMerged, ThisRecord)));

Set(FilteredVehicles, First(FilteredVehiclesCollectionMerged).Value);

Screenshots

App OnStart Vehicles table Features to select Results Merged result