Bootstrap Toggle plugin
Bootstrap Toggle plugin
kthorngren
Posts: 21,305Questions: 26Answers: 4,947
I've gotten the Bootstrap Toggle plugin to work with inline editing with the following:
http://www.bootstraptoggle.com/
columnDefs: [
{
createdCell: function (td, cellData, rowData, row, col) {
$(td).children()
.bootstrapToggle({size: 'mini'})
.addClass('toggle' + row);
console.log(row);
},
targets: 6
}
],
rowCallback: function ( row, data ) {
// Set the checked state of the checkbox in the table
//.change() is bootstrapToggle plugin
$('input.editor-active', row).prop( 'checked', data.coll.enabled == 1 ).change();
}
} );
$('#edit-table').on( 'change', 'input.editor-active', function () {
editor
.edit( $(this).closest('tr'), false )
.set( 'coll.enabled', $(this).prop( 'checked' ) ? 1 : 0 )
.submit();
$(this).bootstrapToggle({size: 'mini'});
} );
Are there any options (maybe the field().def() api
) that can be used to change the checkbox on the edit from to use the toggles provided by this API?
Kevin
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
The way to use libraries such as this one (thanks for introducing me to it btw - I've not seen this one before - it looks great!) is to create a field type plug-in for Editor. That basically acts as a bridge between Editor and the external library.
Using a field type plug-in means you can use the field just like any other in Editor.
Allan
Great, get to learn something new!
Below is my working code. Please let me know if you see anything that should be changed or improved. My HTML and JS skills make me a dangerous dveloper :-) Basically just made checkboxes work with the plugin then added the Bootstrap Toggle init function to the
set
method.Kevin
Very nice - thanks for sharing your code with us!
Allan
Just thought I would provide an update. I found the above implementation caused the Editor to submit changes when filtering or doing anything that caused the
rowCallaback
to execute. The.change()
in the rowCallback cause the bootstrap-toggle plugin to invoke a change causing the unexpected Editor updates.The following changes fixed the issue and actually made the code a bit simpler. Here are the code snippets:
The Editor Field same config as a checkbox but type is
toggle
:I swapped the two lines in the
set: function
and added the.change()
to the checkbox update so the Editor will show the toggle in the proper state:I removed the
createdCell
incolumnDefs
and changed therowCallback
to this:And removed the
$(this).bootstrapToggle({size: 'mini'});
from thechange
event handler which now looks like this:Everything seems to work properly now. Essentially its the same code as the Always Shown Checkbox example with the addition of the toggle plugin, the
.bootstrapToggle({size: 'mini'})
on the end of the line inrowCallback
and changing the field type totoggle
in the Editor.Kevin
Did you ever get this to work? Can you show us the final code? My BootstrapToggle btns don't show up. They stay as checkboxes. Also, do I need the editor licence to get this to work?
Here is an example with just Datatables.
http://live.datatables.net/pesacoxe/1/edit
The key is to use
rowCallback
to apply the Bootstrap Toggle.The Field Type plugin code above is only needed if you are using Editor.
If you are unable to get it to work please post a link to your page or a test case replicating the issue.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
Worked like a charm! The Datatable community is lucky to have you. Thank you kthorngren!