Setting value for Select from dynamic sources

Setting value for Select from dynamic sources

dknifedknife Posts: 14Questions: 4Answers: 1

Trying to get an Editor Select working for a particular field (office). The row data is all ajax and the options for the select is an array which I update before the data is retrieved. When I initiate the reload() to retrieve and render the data, I then update the field options once it's complete with editor.field('office').update(optionsarray).

This is all working great with the label/value pair all correct. However, it doesn't know how to select the existing value since currently I'm only displaying the office's name. With other fields that have a different "value" than the displayed data, I use a custom render with a display and either a sort or filter defined in the column definition and the source JSON corresponding. So I thought I could use the same orthogonal method for this purpose however it doesn't seem that this component of Editor is linked with this capability and there is no "value" component that the select field could read and ensure the correct option is selected.

I guess the set() function could be used however I'm worried about a significant performance hit if I'm using something like createdRow to enact a cycle of the options for that particular field, check for a match based on custom data and use set() when matched. If I have 20,000 rows and possibly 100 options, that doesn't sound like an efficient method.

Any suggestions on how to go about ensuring the correct value is already selected?

This question has an accepted answers - jump to answer

Answers

  • dknifedknife Posts: 14Questions: 4Answers: 1
    edited July 2018 Answer ✓

    Nevermind, figured it out eventually!

    Breaking up the data source into an object
    "office":{"label":"office name","value":"123456"}

    Setting field definition to office.value when defining Editor
    { label: 'Office:', name: 'office.value', type: 'select', options: officeoptions }

    Setting nested label as data adding editField to column definition

    {data:'office.label',editField:'office.value',className:'editable'},

    All is working now and office value is correctly selected after each reload when selecting a new office and updating row data.

  • dknifedknife Posts: 14Questions: 4Answers: 1

    Updated Editor definition to separate name and data to submit correct value to server on submit

    {
                label: 'Office:',
                name: 'office',
                data: 'office.value',
                type: 'select',
                options: officeoptions
            }
    
    {data:'office.label',editField:'office',className:'editable'}
    
    agenteditor.field('office').update(officeoptions);
    

    Figured out markdown for code blocks now, sorry about above post.

  • allanallan Posts: 61,609Questions: 1Answers: 10,089 Site admin

    Thanks for posting back - good to hear you've got it working.

    Allan

This discussion has been closed.