Hi all,
I'm going through the Editor examples and trying to figure out how the "one-to-many join" works. I have removed the options "permission[].id" in the server script and replaced them with options in the JavaScript. So my Server script and my AJAX Load look like this:
Server script:
$(document).ready(function () {
$(document).ready(function () {
editor = new $.fn.dataTable.Editor({
ajax: "/api/users",
table: "#example",
fields: [{
label: "First name:",
name: "users.first_name"
},
{
label: "Last name:",
name: "users.last_name"
},
{
label: "Site:",
name: "users.site",
type: "select"
},
{
label: "Permissions:",
name: "?",
type: "checkbox",
options: [{
name: "Mainframe",
id: 7
},
{
name: "Laptop",
id: 8
}
],
optionsPair: {
label: 'name',
value: 'id'
}
}
]
});
table = $("#example").DataTable({
dom: "Bfrtip",
ajax: {
url: "/api/users",
type: "POST"
},
columns: [{
data: "users.first_name"
},
{
data: "users.last_name"
},
{
data: "users.city"
},
{
data: "sites.name"
},
{
data: "permissions",
render: "[, ].name"
}
],
select: true,
buttons: [{
extend: "create",
editor: editor
},
{
extend: "edit",
editor: editor
},
{
extend: "remove",
editor: editor
}
]
});
});
});
AJAX Load (excerpt):
{
"DT_RowId": "row_27",
"users": {
"first_name": "Ifeoma",
"last_name": "Mays",
"city": "Parkersburg",
"site": 3
},
"sites": {
"name": "Paris"
},
"permissions": []
},
{
"DT_RowId": "row_28",
"users": {
"first_name": "Basia",
"last_name": "Harrell",
"city": "Cody",
"site": 4
},
"sites": {
"name": "New York"
},
"permissions": [
{
"id": 6,
"name": "Accounts"
},
{
"id": 3,
"name": "Desktop"
},
{
"id": 1,
"name": "Printer"
},
{
"id": 2,
"name": "Servers"
},
{
"id": 4,
"name": "VMs"
},
{
"id": 5,
"name": "Web-site"
}
]
},
- If I put
name: "permissions[].id"
like in the example there is no error but nothing happens. Why it doesn't work in my case?
- The fields in the server script and the option
name
in the JavaScript name: "users.first_name"
, name: "users.last_name"
and name: "users.site"
are the same. But looking at the server script where does name: "permissions[].id"
come from?
If I want to get the options from a AJAX call, then I suppose I should use this:
editor.on( 'initEdit', function ( e, node, data, items, type ) {
$.getJSON( '/myURL', data, function ( json ) {
editor.field('myField').update( json );
} );
} );
In this case the AJAX load should be just an array of objects, the option options
should be removed and the option optionsPair
should be left like it is now?