add select2 in all cells of a given column : how can I initialize them ?
add select2 in all cells of a given column : how can I initialize them ?
Hello,
I use the render() function with pleasure to add a form in each cell of the 2d column of my datatable. It works perfectly. But now, I would like to add also a select2 component but I cannot initialize it.
I tried to call $(".select2").select2(); after the $("#myTable").Datatable(...) initialization, with no success.
I tried to use the "initComplete" parameter of Datatable() like this :
"initComplete": function(settings, json) {
$(".select2").select2();
}
but it does not work neither.
The select2 components has the following HTML code :
<select class="form-control select2" multiple="multiple" data-placeholder="txt">
<option>...</option>
<option>...</option>
<option>...</option>
</select>
How can I initialize all the select2 fields which have been added to the cell content using the render function ?
Many thanks for your help !
T.
Answers
That looks like it should work to me. Can you link to a page showing the issue please.
Allan
I think that the problem could be linked to CSS selector. I tried to change $(".select2").select2(); to $('.select2').addClass('red'); but it does not work. But $('#myTable tbody').addClass('red'); works perfectly, adding the 'red' style to all cells.
I tried many times to get the correct selector to access all .select2 objects in myTable, without any luck. How can I determine the correct selector to use ?
Try:
However, as I say, I would really need a test case to know for sure.
Allan
It does not work... I can send you the url but it's a login protected website. Can I send you the login & pwd information using PM ?
Please do - click my name above and then "Send message".
Allan
Actually - I've just seen your message. I'll get back to you shortly.
The problem is that you aren't using DataTables' own
ajax
options to load the data into the table, but rather your own$.ajax
call withrow.add()
. You need to initialise select2 after you have added the rows. At the moment the select2 initialisation is happening before the data has been loaded into the table, thus it is finding nothing.Allan
Thank you... I tried to switch to ajax loading inside Datatables, but there is something that I do wrong...
I have this in HTML :
I have in JS > $(document).ready(function() { ... this :
myscript.php contains :
and it outputs this (got from Safari > Resources > XHR) :
When I load the page, nothing is shown in the DataTable (no entry) and I get the following error : TypeError: undefined is not an object (evaluating 'c.length') in jquery.dataTables.min.js:36:421.
I don't understand. Could you help me please ?
Many thanks again...
You have enabled the
serverSide
option, but your server-side script doesn't fully implement server-side processing.Do you need server-side processing?
Allan
Ok !! I modified the script to create json result and deleted serverSide option. Now it works and It's much pretty than my previous code... Thanks thanks !!! :-)