RE-ordering items in a collection field - UI Model Help Needed!

I'm building a data form which contains a collection field, and I've added some UI Action buttons to move items up and down in the collection.


But this is where I'm stuck, I have no idea what to do in the UI Model. This is as far as I've gotten :)

Private Sub _btnmoveup_InvokeAction(sender As Object, e As InvokeActionEventArgs) Handles _btnmoveup.InvokeAction

End Sub
Private Sub _btnmovedown_InvokeAction(sender As Object, e As InvokeActionEventArgs) Handles _btnmovedown.InvokeAction

End Sub

Not having much luck finding example code.


Comments

  • Hi Rick,

    I saw this the other day, and I thought I'd throw together a quick demo. It turns out it's harder than it looks.

    What I have below doesn't quite work, but I don't know why yet.

    I thought it might give you ideas.

    If someone has a working example, I'd love to see it. Likewise, if I get it to work, I'll add to this post.

    Given this collection:

    <FormField FieldID="LOCATIONS" Caption="Locations" DataType="XML">
    <Collection>
    <Fields>
    <FormField FieldID="NAME" DataType="String" Caption="Name"/>
    <FormField FieldID="COUNTRYID" DataType="Guid" Caption="Country">
    <SimpleDataList SimpleDataListID="c9649672-353d-42e8-8c25-4d34bbabfbba"/>
    </FormField>
    <FormField FieldID="STATEID" DataType="Guid" Caption="State">
    <SimpleDataList SimpleDataListID="b46d36d1-d3ed-4f6e-91da-89b6c88ca0c6">
    <Params>
    <Param ID="COUNTRYID">
    <Value>Fields!COUNTRYID</Value>
    </Param>
    </Params>
    </SimpleDataList>
    </FormField>
    </Fields>
    </Collection>
    </FormField>

    And these UIActions


    <UIActions>
    <UIAction ActionID="MOVEUP" Caption="Move up"/>
    <UIAction ActionID="MOVDOWN" Caption="Move down"/>
    </UIActions>

    This code should move the selected row up (but it doesn't)


    Private Sub _moveup_InvokeAction(sender As Object, e As InvokeActionEventArgs) Handles _moveup.InvokeAction
    Dim selectedIndex = GetSelectedRowIndex()
    If 0 < selectedIndex Then '0 because you can't move the top row up any further
    Dim replacedRow = LOCATIONS.Value(selectedIndex - 1)
    LOCATIONS.Value(selectedIndex - 1) = LOCATIONS.Value(selectedIndex)
    LOCATIONS.Value(selectedIndex) = replacedRow
    End If
    End Sub

    Private Function GetSelectedRowIndex() As Integer
    For i As Integer = 0 To LOCATIONS.Value.Count
    If LOCATIONS.Value(i).Selected Then
    Return i
    End If
    Next
    Return -1
    End Function

    Joseph Styons

    https://www.styonssoftware.com

  • You gave me enough to get it working! Just had to use the methods.... THANK YOU!


    Private Sub _btnmoveup_InvokeAction(sender As Object, e As InvokeActionEventArgs) Handles _btnmoveup.InvokeAction
    Dim selectedIndex = GetSelectedRowIndex()
    If selectedIndex > 0 Then '0 because you can't move the top row up any further
    Dim rowToMove = FUNDS.Value(selectedIndex)
    FUNDS.Value.RemoveAt(selectedIndex) 'remove the selected item
    FUNDS.Value.Insert(selectedIndex - 1, rowToMove) ' then insert it one hgiher.
    End If
    End Sub

    Private Sub _btnmovedown_InvokeAction(sender As Object, e As InvokeActionEventArgs) Handles _btnmovedown.InvokeAction
    Dim selectedIndex = GetSelectedRowIndex()
    If selectedIndex < FUNDS.Value.Count - 1 Then
    Dim rowToMove = FUNDS.Value(selectedIndex)
    FUNDS.Value.RemoveAt(selectedIndex) 'remove the selected item
    FUNDS.Value.Insert(selectedIndex + 1, rowToMove) ' then insert it one hgiher.
    End If

    End Sub

    Private Function GetSelectedRowIndex() As Integer
    For i As Integer = 0 To FUNDS.Value.Count
    If FUNDS.Value(i).Selected Then
    Return i
    End If
    Next
    Return -1
    End Function

Categories