Writing back the data

As we make changes into the dataset in the grid, Power Grid will output an string that can be found on onUpdatedItems property.

For instance if we change (on the grid) Category field to Planning (items 6 and 7) the output is going to be:

6</>Category</>Planning</Row>7</>Category</>Planning</Row>

The next step is to transform this string into a collection that can be used to write back to our data source.

Using onUpdatedItems to change the data

To do so let's use the formula below. This will split the string into a table using the tag </row> as separator.

Clear(updatedRowsCol);
Clear(updatedItemsCol);


// 1.  Parsing changed rows into a collection

ForAll(
    Split(
        LowcoderaDataGrid1.onUpdatedItems,
        "</Row>"
    ),
    Collect(
        updatedRowsCol,
        Result
    )
);

The result is a collection like this:

Now let's get the field, ID and value organized in a tabular way. For it we are going to use the Power FX formula as below:

ForAll(
    updatedRowsCol,
    Collect(
        updatedItemsCol,
        {
            id: First(
                Split(
                    ThisRecord.Value,
                    "</>"
                )
            ).Result,
            fieldName: First(
                LastN(
                    Split(
                        ThisRecord.Value,
                        "</>"
                    ),
                    2
                )
            ).Result,
            value: Last(
                LastN(
                    Split(
                        ThisRecord.Value,
                        "</>"
                    ),
                    2
                )
            ).Result
        }
    )
);

Finally we can loop through the data while changing our original data source.

ForAll(
    updatedItemsCol As UpdatedItems,
    Switch(
        UpdatedItems.fieldName,
        "Title",
        Patch(
            colWorkProgressTracker,
            LookUp(
                colWorkProgressTracker,
                ID = Value(UpdatedItems.id)
            ),
            {Title: UpdatedItems.value}
        ),
        "Description",
        Patch(
            colWorkProgressTracker,
            LookUp(
                colWorkProgressTracker,
                ID = Value(UpdatedItems.id)
            ),
            {Description: UpdatedItems.value}
        ),
        "Category",
        Patch(
            colWorkProgressTracker,
            LookUp(
                colWorkProgressTracker,
                ID = Value(UpdatedItems.id)
            ),
            {Category: UpdatedItems.value}
        ),
        "Progress",
        Patch(
            colWorkProgressTracker,
            LookUp(
                colWorkProgressTracker,
                ID = Value(UpdatedItems.id)
            ),
            {Progress: UpdatedItems.value}
        ),
        "Priority",
        Patch(
            colWorkProgressTracker,
            LookUp(
                colWorkProgressTracker,
                ID = Value(UpdatedItems.id)
            ),
            {Priority: UpdatedItems.value}
        ),
         "StartDate",
        Patch(
            colWorkProgressTracker,
            LookUp(
                colWorkProgressTracker,
                ID = Value(UpdatedItems.id)
            ),
            {StartDate: DateValue(UpdatedItems.value)}
        ),
             "DueDate",
        Patch(
            colWorkProgressTracker,
            LookUp(
                colWorkProgressTracker,
                ID = Value(UpdatedItems.id)
            ),
            {DueDate: DateValue(UpdatedItems.value)}
        )
    )
)

Note that we can define which fields will receive changes by placing or removing on the switch statement

Now, create a button for saving back the data

Patch(
    WorkProgressTracker,
    ShowColumns(
        colWorkProgressTracker,
        "ID",
        "Title",
        "Description",
        "Category",
        "Progress",
        "Priority",
        "StartDate",
        "DueDate"
    )
)

Last updated