Question about setting the value in Drop Down

Question about setting the value in Drop Down

oneillgoneillg Posts: 33Questions: 9Answers: 0

I have what I think is a simple question but can't seem to find an answer. I have almost everything working as I am using the inline editor, specifically a dropdown field. I am populating the dropdown successfully, but don't know how to select its value after it is populated to match what's in the database.

The dropdown is set up thusly.
{
name: "supplier",
type: "select",
options: retrieveDropdownData() // gets the list formatted appropriately
}
I need each row to have its select values set correctly before allowing the user to change the fields. Thanks for any insight.

-Gabe

Answers

  • allanallan Posts: 61,892Questions: 1Answers: 10,144 Site admin

    retrieveDropdownData() - is that an async function? If so, you'd need to use the update method of the select field type to update the options.

    Allan

  • oneillgoneillg Posts: 33Questions: 9Answers: 0

    No it is not. It simply pulls the values from storage.

  • oneillgoneillg Posts: 33Questions: 9Answers: 0
    edited April 2018

    In your example here all the values are different for 'site' (location) but all rely on same drop down options. When you click on the field the drop down appears and the correct value is pre-selected. That's what I want to emulate but don't see how you are doing it.

    https://editor.datatables.net/examples/inline-editing/join.html

  • allanallan Posts: 61,892Questions: 1Answers: 10,144 Site admin

    In that example the JSON return from the server contains the options for the select input and Editor will automatically detect that and populate it. It uses the update() method of the select input internally (which is the same as using the options property), so your way should work okay.

    Can you give me a link to your page so I can take a look and debug it?

    Allan

  • oneillgoneillg Posts: 33Questions: 9Answers: 0

    I found the issue. The function retrieveDropDownData returns a set of named pairs for the options in the select field. The keys are 'label' and 'value'. I assumed that the 'label' would be the text in the option and the value the value like so:
    <option value=<value> > <label> </option>. I often use the value for the database and the label for what's shown. These are different values. By making both of these the same value that matched the contents of the original field, it works.

    Is this correct?

  • allanallan Posts: 61,892Questions: 1Answers: 10,144 Site admin

    What you assume should be correct. The fact that it isn't happening suggests to me that the value to be edited is not the value, but rather the label. Might that be the case?

    Typically the value to be edited would be an integer (primary key column id).

    Allan

  • oneillgoneillg Posts: 33Questions: 9Answers: 0

    Yes, I want to edit the label but pass the value to the database. Is this possible?

  • allanallan Posts: 61,892Questions: 1Answers: 10,144 Site admin

    I'm afraid not. You need to have both the label and the value in the data source for the row (you can actually get away with just the value, but that's a topic for another time!).

    If you have a look at this example you will see the JSON looks like:

    {
        "DT_RowId": "row_1",
        "users": {
            "first_name": "Quynn",
            "last_name": "Contreras",
            "phone": "1-971-977-4681",
            "site": "1"
        },
        "sites": {
            "name": "Edinburgh"
        }
    }
    

    The DataTable displayed the label - sites.name. But Editor edits users.site (since that is what matches the value in the select list).

    Allan

  • oneillgoneillg Posts: 33Questions: 9Answers: 0

    Thanks for your help, Allan. I understand most of this but have yet to quickly grasp exactly how to use this example to achieve my goal. I have found another way. I have the preSubmit code adding the needed info in later.

  • oneillgoneillg Posts: 33Questions: 9Answers: 0

    . . . or at least I thought I did.

This discussion has been closed.