Server side initialization of selected field in a multiple select input field
Server side initialization of selected field in a multiple select input field
Hello,
I'm using datatables to display a list of usergroups. For every group I'm listing the users that are part of it. I get the data using the JSON procedure.
I want to use the Editor to let the user change the Group name and also the users that are in the group.
Through the JSON, using the "options" element, I was able to initialize a select input field with the list of all available users. I added the option "multiple" to let the user select multiple users belonging to that group.
But I also want to preselect the list of users that have already been associated to that group, in order for the user to edit that list. Except that I don't know how to do it by using the JSON.
This is an example of my JSON:
{
"data": [{
"DT_RowId": "row_1",
"description": {
"ID": "1",
"NOME": "Group 1",
"N_UTENTI": "3"
}
}, {
"DT_RowId": "row_2",
"description": {
"ID": "2",
"NOME": "Group 2",
"N_UTENTI": "1"
}
}, {
"DT_RowId": "row_7",
"description": {
"ID": "7",
"NOME": "Group Everybody",
"N_UTENTI": "10"
}
}],
"options": {
"description.UTENTI": [{
"value": "0",
"label": "-"
}, {
"value": "1",
"label": "User Test"
},{
"value": "5",
"label": "User Demo"
},{
"value": "9",
"label": "User Dummy"
}, {
"value": "10",
"label": "User Office"
}]
}
}
This is how I initialized the Editor
editor = new $.fn.dataTable.Editor( {
ajax: 'php/json_users_groups.php',
table: '#list-usersGroups-table',
fields: [{
label: "Group Name:",
name: "description.NOME",
fieldInfo: "Obbligatorio"
},{
label: "Users:",
name: "description.UTENTI",
type: "select2",
opts: {
multiple: true
}
}],
i18n: {
create: {
button: "Insert", ..............
}
}
});
Can anybody help me with that?
Thanks in advance!
This question has an accepted answers - jump to answer
Answers
Hi,
You would have the value (for the field
description.UTENTI
) as an array of values -[ "1", "5" ]
for example.There is an example of that being used here.
Allan
Such a simple answer for what it seemed (to me) a big problem! Thank you very much!