How to display multiple selections in Editor Selectize from data source?

How to display multiple selections in Editor Selectize from data source?

mrsearing-allmrsearing-all Posts: 10Questions: 3Answers: 0
edited November 2023 in Free community support

I am using Editor with Selectize, which loads the options as expected and I can successfully select multiple options and save to the database. However, when I go to edit that record, specifically the field 'Brands' below, the Selecitze Editor field is blank. If it is just one, then it successfully shows that option as selected.

I've tried creating a custom GetFormatter and the ready-to-use Format methods for the field.

With the custom GetFormatter, I receive the error 'Unable to cast value to be String'

.Field(new Field("Brands")
    .GetFormatter((val, data) => val == null ? Format.NullEmpty() : Format.Explode(","))
    .SetFormatter(Format.NullEmpty()))

Using the ready-to-use Format methods does not seem to alter the data and there is no error, however Selectize does not recognize multiple selections and does not display if there is more than one option selected.

.Field(new Field("Brands")
    .GetFormatter(Format.Explode(","))
    .GetFormatter(Format.NullEmpty())
    .SetFormatter(Format.NullEmpty()))

What is returned with no Formatters or ready-to-use methods:

Brands: "152" //will displayed as selected, but not "152,232"; shows as empty. Can be null
Comments: null
ContactID: 1498
Email: "test@test.com"
FirstName: "Test"
LastName: "Contact"
Notify: false
Title: "Test Title"
Updated: "2023-11-29T18:13:51.703"

Unfortunately I cannot link to the page but can provide any clarification.

I have read this discussion https://datatables.net/forums/discussion/68294/how-to-show-multiple-selected-options#latest so I know it needs to be an array but cannot get the format in the server-side code to do so.

This question has an accepted answers - jump to answer

Answers

  • mrsearing-allmrsearing-all Posts: 10Questions: 3Answers: 0

    It looks like I may need to use Field.GetValue to set a delegate, but I am not quite sure how to use this. I have searched the forums and other sites for any examples, but have not found a decent example of how it used.

  • mrsearing-allmrsearing-all Posts: 10Questions: 3Answers: 0

    For anyone looking for the same solution, this is how I solved it:

    .Field(new Field("Brands")
                    .GetFormatter((val, data) => val.GetType().ToString() == "System.DBNull" ? null : ((string[])val.ToString().Split(",")))
                    .SetFormatter(Format.NullEmpty()))
    

    And change the property in my class from string to string[]

    For it to save properly, use

    editor.on('preSubmit', function (e, data, action) {
        
        if (editor.field('Brands').val() !== null && action !== 'remove') {
            let notFormatted = editor.field('Brands').val();
            let keys = Object.keys(data.data);
            let rowID = keys[0];
            data.data[rowID]['Brands'] = notFormatted.toString();
        }
    });
    
  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓

    Nice, thanks for posting the solution!

    Colin

Sign In or Register to comment.