<!DOCTYPE html>
<html>
  <head>
    
    
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <!-- <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.0/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdn.datatables.net/1.12.1/js/jquery.dataTables.js"></script>
    <script src="https://cdn.datatables.net/1.12.1/js/dataTables.bootstrap4.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.7.1/js/dataTables.buttons.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.7.1/js/buttons.bootstrap4.min.js"></script>
    <script src="https://cdn.datatables.net/select/1.3.3/js/dataTables.select.min.js"></script>
    <script src="https://editor.datatables.net/extensions/Editor/js/dataTables.editor.min.js"></script>
    <script src="https://editor.datatables.net/extensions/Editor/js/editor.bootstrap4.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.2/js/select2.min.js"></script>
  
    
    
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
    <link href="https://editor.datatables.net/extensions/Editor/css/editor.bootstrap4.min.css" rel="stylesheet" type="text/css" />
    <link href="https://cdn.datatables.net/1.12.1/css/jquery.dataTables.css" rel="stylesheet" type="text/css" />
    <link href="https://cdn.datatables.net/buttons/1.7.1/css/buttons.bootstrap4.min.css" rel="stylesheet" type="text/css" />
    <link href="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.2/css/select2.min.css" rel="stylesheet" type="text/css" />
    
    
    <meta charset=utf-8 />
    <title>DataTables - JS Bin</title>
    <body>
    <div class="container">
 
      <table id="example" class="table display" cellspacing="0" width="100%"></table>
      
    </div>
  </body>
</html>
 
body {
  font: 90%/1.45em "Helvetica Neue", HelveticaNeue, Verdana, Arial, Helvetica, sans-serif;
  margin: 0;
  padding: 0;
  color: #333;
  background-color: #fff;
}
 
(function( factory ){
    if ( typeof define === 'function' && define.amd ) {
        // AMD
        define( ['jquery', 'datatables.net', 'datatables.net-editor'], factory );
    }
    else if ( typeof exports === 'object' ) {
        // Node / CommonJS
        module.exports = function ($, dt) {
            if ( ! $ ) { $ = require('jquery'); }
            factory( $, dt || $.fn.dataTable || require('datatables') );
        };
    }
    else if ( jQuery ) {
        // Browser standard
        factory( jQuery, jQuery.fn.dataTable );
    }
}(function( $, DataTable ) {
'use strict';
 
 
if ( ! DataTable.ext.editorFields ) {
    DataTable.ext.editorFields = {};
}
  
var _fieldTypes = DataTable.Editor ?
    DataTable.Editor.fieldTypes :
    DataTable.ext.editorFields;
  
_fieldTypes.select2 = {
    _addOptions: function ( conf, opts ) {
        var elOpts = conf._input[0].options;
  
        elOpts.length = 0;
  
        if ( opts ) {
            DataTable.Editor.pairs( opts, conf.optionsPair, function ( val, label, i ) {
                elOpts[i] = new Option( label, val );
            } );
        }
    },
  
    create: function ( conf ) {
        conf._input = $('<select/>')
            .attr( $.extend( {
                id: DataTable.Editor.safeId( conf.id )
            }, conf.attr || {} ) );
  
        var options = $.extend( {
                width: '100%'
            }, conf.opts );
  
        _fieldTypes.select2._addOptions( conf, conf.options || conf.ipOpts );
        conf._input.select2( options );
  
        var open;
        conf._input
            .on( 'select2:open', function () {
                open = true;
            } )
            .on( 'select2:close', function () {
                open = false;
            } );
  
        // On open, need to have the instance update now that it is in the DOM
        this.one( 'open.select2-'+DataTable.Editor.safeId( conf.id ), function () {
            conf._input.select2( options );
  
            if ( open ) {
                conf._input.select2( 'open' );
            }
        } );
  
        return conf._input[0];
    },
  
    get: function ( conf ) {
        var val = conf._input.val();
        val =  conf._input.prop('multiple') && val === null ?
            [] :
            val;
          
        return conf.separator ?
            val.join( conf.separator ) :
            val;
    },
  
    set: function ( conf, val ) {
        if ( conf.separator && ! Array.isArray( val ) ) {
            val = val.split( conf.separator );
        }
  
        // Clear out any existing tags
        if ( conf.opts && conf.opts.tags ) {
            conf._input.val([]);
        }
  
        // The value isn't present in the current options list, so we need to add it
        // in order to be able to select it. Note that this makes the set action async!
        // It doesn't appear to be possible to add an option to select2, then change
        // its label and update the display
        var needAjax = false;
  
        if ( conf.opts && conf.opts.ajax ) {
            if ( Array.isArray( val ) ) {
                for ( var i=0, ien=val.length ; i<ien ; i++ ) {
                    if ( conf._input.find('option[value="'+val[i]+'"]').length === 0 ) {
                        needAjax = true;
                        break;
                    }
                }
            }
            else {
                if ( conf._input.find('option[value="'+val+'"]').length === 0 ) {
                    needAjax = true;
                }
            }
        }
  
        if ( needAjax && val ) {
            $.ajax( $.extend( {
                beforeSend: function ( jqXhr, settings ) {
                    // Add an initial data request to the server, but don't
                    // override `data` since the dev might be using that
                    var initData = conf.urlDataType === undefined || conf.urlDataType === 'json' ? 'initialValue=true&value='+JSON.stringify(val)
                    : $.param({initialValue: true, value: val});
 
                    if ( typeof conf.opts.ajax.url === 'function' ) {
                        settings.url = conf.opts.ajax.url();
                    }
                     
                    if ( settings.type === 'GET' ) {
                        settings.url += settings.url.indexOf('?') === -1 ?
                            '?'+initData :
                            '&'+initData;
                    }
                    else {
                        settings.data = settings.data ?
                            settings.data +'&'+ initData :
                            initData;
                    }
                },
                success: function ( json ) {
                    var addOption = function ( option ) {
                        if ( conf._input.find('option[value="'+option.id+'"]').length === 0 ) {
                            $('<option/>')
                                .attr('value', option.id)
                                .text( option.text )
                                .appendTo( conf._input );
                        }
                    };
  
                    if ( Array.isArray( json ) ) {
                        for ( var i=0, ien=json.length ; i<ien ; i++ ) {
                            addOption( json[i] );
                        }
                    }
                    else if ( json.results && Array.isArray( json.results ) ) {
                        for ( var i=0, ien=json.results.length ; i<ien ; i++ ) {
                            addOption( json.results[i] );
                        }
                    }
                    else {
                        addOption( json );
                    }
  
                    conf._input
                        .val( val )
                        .trigger( 'change', {editor: true} );
                }
            }, conf.opts.ajax ) );
        }
        else {
            conf._input
                .val( val )
                .trigger( 'change', {editor: true} );
        }
    },
  
    enable: function ( conf ) {
        $(conf._input).removeAttr( 'disabled' );
    },
  
    disable: function ( conf ) {
        $(conf._input).attr( 'disabled', 'disabled' );
    },
  
    // Non-standard Editor methods - custom to this plug-in
    inst: function ( conf ) {
        var args = Array.prototype.slice.call( arguments );
        args.shift();
  
        return conf._input.select2.apply( conf._input, args );
    },
  
    update: function ( conf, data ) {
        var val = _fieldTypes.select2.get( conf );
  
        _fieldTypes.select2._addOptions( conf, data );
  
        // Restore null value if it was, to let the placeholder show
        if ( val === null ) {
            _fieldTypes.select2.set( conf, null );
        }
  
        $(conf._input).trigger('change', {editor: true} );
    },
 
    focus: function ( conf ) {
        if ( conf._input.is(':visible') && conf.onFocus === 'focus' ) {
            conf._input.select2('open');
        }
    },
 
    owns: function ( conf, node ) {
        if ( $(node).closest('.select2-container').length || $(node).closest('.select2').length || $(node).hasClass('select2-selection__choice__remove') ) {
            return true;
        }
        return false;
    },
 
    canReturnSubmit: function() {
        return false;
    }
};
 
 
}));
$(document).on('select2:open', () => {
    let allFound = document.querySelectorAll('.select2-container--open .select2-search__field');
    $(this).one('mouseup keyup',()=>{
        setTimeout(()=>{
            allFound[allFound.length - 1].focus();
        },0);
    });
});
$(document).ready(function() {
   
  //$.fn.dataTable.render.moment("M/D/YYYY");
  var editor = new $.fn.dataTable.Editor({
    table: "#example",
    fields: [
      {
         "label": "Priority:",
         "name": "priority",
         "type": "select2",
         "options": [
             { "label": "1 (highest)", "value": "1" },
             { "label": "2",           "value": "2" },
             { "label": "3",           "value": "3" },
             { "label": "4",           "value": "4" },
             { "label": "4",           "value": "4" },
             { "label": "4",           "value": "4" },
             { "label": "4",           "value": "4" },
             { "label": "4",           "value": "4" },
             { "label": "4",           "value": "4" },
             { "label": "4",           "value": "4" },
             { "label": "4",           "value": "4" },
             { "label": "4",           "value": "4" },
             { "label": "4",           "value": "4" },
             { "label": "4",           "value": "4" },
             { "label": "4",           "value": "4" },
             { "label": "4",           "value": "4" },
             { "label": "4",           "value": "4" },
             { "label": "4",           "value": "4" },
             { "label": "4",           "value": "4" },
             { "label": "4",           "value": "4" },
             { "label": "4",           "value": "4" },
             { "label": "5 (lowest)",  "value": "5" }
         ]
     },
      
      
    ]
  });
  $("#example").DataTable({
    dom: "Bfrtip",
    columns: [
            { data: 'priority' },
    ],
    buttons: [
      { extend: "create", editor: editor },
      { extend: "edit", editor: editor },
      { extend: "remove", editor: editor }
    ]
  });
  
});
6 warnings
Line 158: 'i' is already defined.
Line 158: 'ien' is already defined.
Line 229: 'arrow function syntax (=>)' is available in ES6 (use esnext option) or Mozilla JS extensions (use moz).
Line 230: 'let' is available in ES6 (use esnext option) or Mozilla JS extensions (use moz).
Line 231: 'arrow function syntax (=>)' is available in ES6 (use esnext option) or Mozilla JS extensions (use moz).
Line 232: 'arrow function syntax (=>)' is available in ES6 (use esnext option) or Mozilla JS extensions (use moz).
Output 300px

This bin was created anonymously and its free preview time has expired. Get a free unrestricted account

Dismiss x
public
Bin info
anonymouspro
0viewers