Editor Datatables: Bootstrap modal window not populating dynamic select options
Editor Datatables: Bootstrap modal window not populating dynamic select options
I'm trying to implement Bootstrap for this kind of form with nested JSON data source where select drop-down is present with standalone collection editor.
At the moment I can see modal window is opened, however there are no values in drop-down select. When I manually add some test options for select field, those are populated. I suspect this is happening, because Bootstrap modal window is not playing together with Datatables Editor and particularly select field. Here is my other issue with Bootstrap implementation.
My JSON data structure is exactly what Editor is expecting:
{
"data": [{
"id": 2,
"media": {
"name": "something here",
"company": 1
},
"companies": {
"name": "company No1"
}
}, {
"id": 3,
"media": {
"name": "test name",
"company": 2
},
"companies": {
"name": "company No2"
}
}],
"options": {
"media.company": [{
"value": 2,
"name": "company No2"
}, {
"value": 1,
"name": "company No1"
}]
}
}
This row '<span data-editor-field="media.company">' + data.companies.name + '</span> '
in my JS populates Company name in panel, so it seems to me data is passed correctly. However values are not shown in drop-down select on adding new record or editing existing one.
I'm a bit confused why I don't see my select drop-down values? My JS code is pretty much the same as in standalone collection editor example, my Bootstrap JS and CSS are in place.
I'll be happy for any hint of how to solve this.
Answers
Its happening because the JSON that is used to populate the DataTable includes the options to show in the
select
. Since you are using a standalone Editor there is no JSON that Editor can automatically scan through and get that information from.Instead, what you would need to do is either use the
options
parameter of theselect
type, or use theupload()
field method with information that you get via Ajax or any other method.Regards,
Allan
@allan Thank you for explaining. As I understand at the moment I can pass my JSON to standalone collection editor - please, see my example above. In my JSON I'm sending "options", however I can't make them to work. My fields look like this:
So far I was expecting to see in my drop-down select list of
media.company
values.Do you think I can do something with my JSON, to make it work or I have to go for that
upload()
anyway?I'm not clear on how the options get from your JSON above into the form though. If it were linked to DataTables, that would work because Editor listens for the
xhr
event from DataTables and uses the JSON to population the options. But in standalone mode it can't do that - there is nothing to listen to. You need to populate the options using theupdate()
method of theselect
field.Allan
@allan
I create JSON above with Jbuilder and get all together with this:
I can clearly see in my console.log
options
are there. So far I've been thinking, if I pass options in my JSON, it should be possible to access them in my form as in some of examples you have.As I understand at the moment my
editor = new $.fn.dataTable.Editor
fields access onlyjson.data
, so I cannot get anything fromoptions
. For examplename: 'media.company'
will return error at the moment as it can't retrieveoptions.media.company
.Do you think there is a way to get it working or I still have to do that
update()
thing?Update
I can get options from my JSON and update options like this:
however it will not show selected option for
edit
action.I tried to use fields.data however no luck so far - can't get out options data from my JSON into select drop-down.
That suggests that the value in the list of options does not match the value being edited. What is the result of
JSON.stringify( options );
on line 14 in the above?Allan
@allan
JSON.stringify( options );
gives me this:[{"value":2,"label":"company No2"},{"value":1,"label":"company No1"}]
My current JSON looks like this:
In my JS
createPanel
function and Edit action look like this:I can get my
company
ID with this:Then for
company id=1
I should seecompany No1
in my drop-down, however I seecompany No2
as it is sorted first in list.I've sent you a PM, but for completeness here - I've fairly certain the issue is related to the fact that the data is being read from the DOM, which means its a string.
If the updated script doesn't help, I've just had another thought - try making your numbers in the select options strings (simply add
""
around them) and I think that will solve it.Allan