How to show multiple selected options?

How to show multiple selected options?

wblakencwblakenc Posts: 78Questions: 17Answers: 1

Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:
This seems like a fairly simple question but I cannot find the answer anywhere on DT.net. I have an editor form with a select field type. This field type allows users to select multiple options in the dropdown and submit them to the server. All of that works great. However, when I attempt to edit the record again, none of the options are selected. I suspect it is due to the way I am providing that data to editor.

My data looks like:

[
{ "DT_RowId":"1",
"name":"somename",
"options":"1,2"
}
]

In this case lets assume I have a multiple select field simply called options. How do I give editor the information about which option to select? I have also tried:

[
{ "DT_RowId":"1",
"name":"somename",
"options":["1,2"]
}
]

But that doesn't work. Again, this is a very basic question but I can't figure it out. Help!

Thanks!

This question has an accepted answers - jump to answer

Answers

  • JLegaultJLegault Posts: 31Questions: 6Answers: 2

    Here is I believe the reference documentation for what you're looking for. I haven't used Editor at all, but I believe your options needs to be an object with a label and value.

    Without a test case I cannot try it out, but I'd recommend:

    [ { "DT_RowId":"1", "name":"somename", "options":{ "1":1, "2":2 } } ]

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

    Can you show me how you define your field type for that? I would expect it to be something like:

    {
      name: 'options',
      label: 'Options:',
      options: [ '1', '2', .... ],
      multiple: true,
      separator: ','
    }
    

    Allan

  • JLegaultJLegault Posts: 31Questions: 6Answers: 2
    edited May 2021

    As Allan points out, your second example has an array but only one element: "1,2" you may also want to try an array with separate items, "1","2"

  • wblakencwblakenc Posts: 78Questions: 17Answers: 1

    Allen and JLegault. Thanks for the info.

    This is how my field is set up:

    { 
    label: "Possible Proof Type",
    name: "proofTypes",
    type: "select",
    multiple: true,
    options: [
    { "label": "Audio","value": "6"},                           
    { "label": "None","value": "4"},            
    { "label": "Paper Ballot","value": "5"}                 
    ]
    }
    

    I think JLegault hit the nail on the head, and I need to put quotes around the values instead of having it as a string. I will get it a try and report back.

  • wblakencwblakenc Posts: 78Questions: 17Answers: 1

    and just to update, the json that is being passed to Editor looks like:

    {
    "DT_RowId": "row_7",
    "hardware_name":"AutoMark ",
    "active":"Y",
    "updated_by" : "wblakenc",
    "updated_dt" : "2021-05-12 16:27:00.0",
    "proofTypes": "['1','2']"
    }
    

    Which does not work.

  • wblakencwblakenc Posts: 78Questions: 17Answers: 1

    One more update, in my comment above where the values are 4,5,6, that was a mistake on my part. The values (currently) are just 1,2,3.

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

    "proofTypes": "['1','2']"

    That isn't an array of values though - it is a string.

    If it were:

    "proofTypes": ['1','2']
    

    then I would expect it to work.

    How are you reading and formatting that information from the database at the moment?

    Allan

  • wblakencwblakenc Posts: 78Questions: 17Answers: 1

    Allan,
    Right now I am populating the datatable via ajax and json. All the data editor needs to update is passed to DT and made available in editor when the record is edited.

    I have tried to pass json back that looks like:

    [
    {"DT_RowId": "row_7", "hardware_name":"AutoMark ", "active":"Y", "updated_by" : "wblakenc", "updated_dt" : "2021-05-12 16:27:00.0", "proofTypes": ['1','2'] } 
    ]
    

    But that fails as it is not valid json. I dont know how to pass the proofType without putting quotes around what should be the array.

    I am happy to provide login information if you would like to see it in action.

    I really appreciate your help!

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

    It is only invalid JSON because of the single quotes - JSON uses double quotes for strings (unless Javascript which can use either - or backticks now).

    So this is valid JSON:

    [{
        "DT_RowId": "row_7",
        "hardware_name": "AutoMark ",
        "active": "Y",
        "updated_by": "wblakenc",
        "updated_dt": "2021-05-12 16:27:00.0",
        "proofTypes": ["1", "2"]
    }]
    

    Allan

  • wblakencwblakenc Posts: 78Questions: 17Answers: 1

    Thanks Allan. I should have been able to figure that out myself.

    All is well with the world.

This discussion has been closed.