Eureka: Replace options with title for display only

Created on 10 Mar 2016  路  4Comments  路  Source: xmartlabs/Eureka

Hi, I have some MultipleSelectorRows that contains options with integers. I need to replace the integers when displaying but the values must remain integers when the form is submitted.
This is an example:

<<< MultipleSelectorRow<String>{ $0.title = "Reports" $0.tag = "reports" $0.options = [1,2,5,6,8,9] }

As you can see, when the form is loaded it will allow users to select 1, 2, 5 and so on but what i want is for the user to see the titles for those so 1 becomes Report Test, 2 = Report Test Name etc.

Thanks

appearance onPresent MultipleSelectorRow enhancement

Most helpful comment

You can accomplish this by setting up displayValueFor row property.

MultipleSelectorRow displayValueFor default value is ...

self.displayValueFor = {
            if let t = $0 {
                return t.map({ String($0) }).joinWithSeparator(", ")
            }
            return nil
        }

All 4 comments

@wstudios I just added public var displayValueFor : (T? -> String?)? to MultipleSelectorViewController<T:Hashable> which is the view controller presented by MultipleSelectorRow.

So now you can use the onPresent callback to set up dispayValueFor property..

row.onPresent { from, to in 
     to.dispayValueFor = { return "Hi \($0)" }
}

Thank you, this works for when the value is already selected but how would i change the value to display to users in the multiple select page? Currently it displays 1, 2, 5... Instead I want to display the titles for those integers but keep the integers as the values selected when the form is posted.

I want the users to see this $0.options = [1,2,5,6,8,9] as $0.options = ["Test One","Product Two","Another Option"...]

In another form class that was done like this:

    row.configuration[FormRowDescriptor.Configuration.Options] = ["1", "2", "3", "4"]
    row.configuration[FormRowDescriptor.Configuration.TitleFormatterClosure] = { value in
        switch( value ) {
        case "1":
            return "Normal Visit, no significant changes"
        case "2":
            return "Minor change in status"
        case "3":
            return "Follow up required"
        case "4":
            return "Urgent attention needed"
        default:
            return nil
        }
        } as TitleFormatterClosure

Thanks.

You can accomplish this by setting up displayValueFor row property.

MultipleSelectorRow displayValueFor default value is ...

self.displayValueFor = {
            if let t = $0 {
                return t.map({ String($0) }).joinWithSeparator(", ")
            }
            return nil
        }

I cannot understand how to do, here is my code:

let countries = [ "uk" : "United Kingdom", "fr" : "France", "us" : "United States"]
form +++ Section("Required fields".uppercaseString)
    <<< MultipleSelectorRow<String>("countries") { row in
    row.title = "Countries"
    row.options = countries.map { $0.0 }
    // Show the labels of selected values
    row.displayValueFor  = {
        if let t = $0 {
            return t.map({ countries[$0]! }).joinWithSeparator(", ")
        }
        return nil
    }
    row.onPresent({ from, to in
        // Add the label of the value on the right side of the cell after selection
        to.selectableRowCellUpdate = { cell, row in
            row.displayValueFor = { val in
                if let value = val {
                    return countries[value]
                }
                return nil
            }
        }
    })
}

screenshot 2016-06-07 12 45 13
screenshot 2016-06-07 12 45 19
screenshot 2016-06-07 12 45 25

Was this page helpful?
0 / 5 - 0 ratings