How to initialize options label/value with server reponse in Editor

How to initialize options label/value with server reponse in Editor

armando@streamloan.ioarmando@streamloan.io Posts: 4Questions: 2Answers: 0
edited May 2016 in DataTables 1.10

Hi Allan,

I've been trying to find for a clean solution on how to initialize options fields in the editor with values returned from the database. In my mind when I specify the name locations I should be able to return for this field from the server an array with all the available labels and values from the server. In all the examples the only option is to hardcode/write the labels and values within the editor initialization as shown below:

editor.add( {
    type:  "select",
    label: "Locations:",
    name:  "locations",
    options: [
        { label: "Edinburgh", value: 51 },
        { label: "London",    value: 76 }
    ]
} );

Could you share the cleanest lines of code required to achieve this in the most current version of the editor? I hope it wont require tons of coding. In my mind this is a pretty common use cases and I have to believe the solution is quite straight forward.

Thanks

Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    There are basically two options. If you are Ajax loading data into your DataTable then you can set an options property in the returned object which Editor will automatically look for and populate the field options with (documentation). You can see that in practice in this example.

    Alternatively, if you want to update the options after initialisation and the table's data has been loaded, use the update() method of the select type.

    Allan

This discussion has been closed.