Bootstrap Toggle plugin

Bootstrap Toggle plugin

kthorngrenkthorngren Posts: 21,305Questions: 26Answers: 4,947
edited January 2017 in Free community support

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

Answers

  • allanallan Posts: 63,471Questions: 1Answers: 10,467 Site admin
    Answer ✓

    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

  • kthorngrenkthorngren Posts: 21,305Questions: 26Answers: 4,947

    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.

    // toggle field type plug-in code
    (function ($, DataTable) {
     
    if ( ! DataTable.ext.editorFields ) {
        DataTable.ext.editorFields = {};
    }
     
    var Editor = DataTable.Editor;
    var _fieldTypes = DataTable.ext.editorFields;
     
    _fieldTypes.toggle = {
        create: function ( conf ) {
            var that = this;
     
            conf._enabled = true;
            // Create the elements to use for the input
            conf._input = $('<input/>').attr($.extend({
                type: 'checkbox',
                }, conf.attr || {}));
            return conf._input;
        },
     
        get: function ( conf ) {
            return $(conf._input).prop( 'checked' ) ? 1 : 0;
        },
     
        set: function ( conf, val ) {
            $(conf._input).prop( 'checked', val == 1 )
            $(conf._input).bootstrapToggle({size: 'mini'});
        },
     
        enable: function ( conf ) {
            conf._enabled = true;
            $(conf._input).removeClass( 'disabled' );
        },
     
        disable: function ( conf ) {
            conf._enabled = false;
            $(conf._input).addClass( 'disabled' );
        }
    };
     
    })(jQuery, jQuery.fn.dataTable);
    

    Kevin

  • allanallan Posts: 63,471Questions: 1Answers: 10,467 Site admin

    Very nice - thanks for sharing your code with us!

    Allan

  • kthorngrenkthorngren Posts: 21,305Questions: 26Answers: 4,947
    edited March 2017

    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:

              fields: [
                { label: 'Data Collection:',  name: 'coll.enabled',
                  type: 'toggle', separator: '|',
                  options: [
                          { label: '', value: 1 }
                  ]
                }
            ]
    

    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:

        set: function ( conf, val ) {
            $(conf._input).bootstrapToggle({size: 'mini'});
            $(conf._input).prop( 'checked', val == 1 ).change();
        },
    

    I removed the createdCell in columnDefs and changed the rowCallback to this:

            rowCallback: function ( row, data ) {
                $('input.editor-active', row).prop( 'checked', data.coll.enabled == 1 ).bootstrapToggle({size: 'mini'});
            },
    

    And removed the $(this).bootstrapToggle({size: 'mini'}); from the change event handler which now looks like this:

        $('#edit-table').on( 'change', 'input.editor-active', function () {
            editor
                .edit( $(this).closest('tr'), false )
                .set( 'coll.enabled', $(this).prop( 'checked' ) ? 1 : 0 )
                .submit();
        } );
    

    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 in rowCallback and changing the field type to toggle in the Editor.

    Kevin

  • Igorski88Igorski88 Posts: 25Questions: 8Answers: 0
    edited May 2019

    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?

  • kthorngrenkthorngren Posts: 21,305Questions: 26Answers: 4,947

    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

  • Igorski88Igorski88 Posts: 25Questions: 8Answers: 0

    Worked like a charm! The Datatable community is lucky to have you. Thank you kthorngren!

This discussion has been closed.