How to integrate Select2 in table and edit menu?
How to integrate Select2 in table and edit menu?
Hi all,
I have hard time integrating Select2 both in the table as well as in the edit menu. I've read the manual and seen some examples but I'm either getting errors or there are errors but no dropdowns.
The response from server looks like this:
{
"data": [{
"DT_RowId": "row_name1",
"name": "name1",
"lines": null
},
{
"DT_RowId": "row_name2",
"name": "name2",
"lines": null
},
{
"DT_RowId": "name3",
"name": "name3",
"lines": {
"results": [{
"id": 0,
"text": "text1"
},
{
"id": 1,
"text": "text2"
}
]
}
}
]
}
and this my script:
var editor;
$(document).ready(function () {
editor = new $.fn.dataTable.Editor({
ajax: {
url: "/api/test1",
dataSrc: "data"
},
table: "#example",
fields: [{
label: "Name:",
name: "name",
"type": "text"
}, {
label: "Lines:",
name: "text",
type: "select2",
options: "data.lines",
}]
});
var table = $('#example').DataTable({
dom: "Bfrtip",
ajax: {
url: "/api/test1",
dataSrc: "data"
},
columns: [{
"data": 'name'
}],
buttons: [{
extend: "create",
editor: editor
},
{
extend: "edit",
editor: editor
},
{
extend: "remove",
editor: editor
}
]
});
});
What am I supposed to put in the "name" and "options"? I have tried different things but nothing has worked.
How can I check what data is coming from the ajax request?
Could someone please help with this one or provide a complete example?
Answers
That's not going to work. The
options
property for the Select2 field type should be an array of values (usually objects), not a string (it doesn't attempt to resolve that in the JSON).Typically, you need the server to define the options, so often you wouldn't use the
options
property at all. Instead, in the Ajax response define anoptions
object along with the field name they should apply to (allowing the options for more than one field to be sent) and the options themselves.Have a look at this example and click on the "Ajax load" tab below the table. If you scroll right down the JSON you will see the options defined for
users.site
. The same method is used for the Select2 field type.The reference documentation for that is available here.
Allan
@allan : Thanks for the quick response!
I've tried the example you mentioned:
and I was able to see the dropdown menu in the edit menu if I put:
as editor property.
Not sure If understand you correctly but in my case each table cell can have or not a dropdown menu and if it has it is different from other table rows. In the example the dropdown is fixed and does not vary from row to row.
So what can I do in my case?
Otherwise I have done some progress in the table but there too I have two issues:
1. The dropdown menus appear only on the first page. From the second page on appear only icons (down arrow) without content.
2. On the first page I have two dropdown menus which appear correctly but the values in both of them are joined.
This is how the updated script looks like:
BTW
Is not possible to edit / update posted posts?
Regarding this issue:
1. The dropdown menus appear only on the first page. From the second page on appear only icons (down arrow) without content.
It seems to be related to this:
https://datatables.net/faqs/index
Q. My events don't work on the second page
From there:
One of the best ways of dealing with this is through the use of delegated events with jQuery's on method
but how can I apply this method in the case of a Select2 dropdown?
You need to use
initEdit
to make an Ajax call to get the options from the database and then theupdate
method of the Select2 field type to set the options. e.g.Allan
I still can not make it work. I've used initEdit like this:
As I've mentioned before each dropdown menu could be different for each cell. The "field.name" seems to be accepting only fixed values and when I put json in place of x then I get a dropdown menu with the options:
data, fieldErrors, files, options
When I put optionArray, which is an array with options in the Select2 data format, I get a dropdown menu with 0 and 1.
I've modified the json, which is being fetched via AJAX like this:
I think your
initEdit
should look more like this:That assumes that
/api/test1
will check the data that was sent to it for the right row id, and get the list of options based on that id (since as you say, it varies by row).Then return an array of
id
/text
objects which will be passed to thefield().update()
function.Allan
When I use initEdit the way you mentioned I get as options in the dropdown: data, fieldErrors,files,options.
When I use for and if like this
I get as options in the dropdown the correct id's (e.g. 0,1,..) for each row, and when I swap the label and value in the optionsPair like this:
I get the correct text (e.g. text0, text1, ...), which is actually what I want but now other issues appear, namely when I choose one of them and click update
(when I select the first two)
and the modified json looks like this:
When I swap the label and value in the optionsPair again it has this form:
I might be misunderstanding your JSON format. Are you saying that you are loading all rows and all of their individual options? Not just the options for that specific row?
Is that information already available in the data you feed into the DataTable? If so, it would be available at
row().data()
so no need to make an Ajax call.Allan