How to use jQuery UI AutoComplete

How to use jQuery UI AutoComplete

dynasoftdynasoft Posts: 446Questions: 69Answers: 3

Hi

I'm trying to use the above extension but keep getting a simple text box in lieu of JQuery's combobox. Also, where do I put the js code given on https://editor.datatables.net/plug-ins/field-type/editor.autoComplete, after the creation of the datatable, 'var dataTable = $('#tblDataTable').DataTable( {' which for me is inside '$(document).ready(function () {'? Many thanks.

Here's my code:

    $(document).ready(function () {

        var editor = new $.fn.dataTable.Editor({

            ajax: {
                ...
            },
            table: '#tblDataTable',
            fields: [ {
                    label: '@(lblo.lblNumber)*:',   //*HERE*
                    name: 'FNFNumber',
                    type: 'autoComplete',
                    opts: {
                        "source": [
                            'Item 1', 'Item 2', 'Items 3'
                        ],
                    },
                }
            ],
            i18n: {
                error: {
                    system: '@(lblo.lblError5)'
                }
            }
        });

        var dataTable = $('#tblDataTable').DataTable( {

            order: [[0, 'desc']],
            dom: 'Bfrtip',
            ajax: {
                ...
            },
            columns: [
                { data: 'FNFNumber' ,                        
                    className: 'text-left'
                }
            ],
            select: true,
            buttons: [
                { extend: 'create', editor: editor },
                { extend: 'edit', editor: editor },
                { extend: 'remove', editor: editor }
            ]
        });

        (function ($, DataTable) {

            if ( ! DataTable.ext.editorFields ) {
                DataTable.ext.editorFields = {};
            }

            var _fieldTypes = DataTable.Editor ?
                DataTable.Editor.fieldTypes :
                DataTable.ext.editorFields;

            _fieldTypes.autoComplete = {
                create: function ( conf ) {
                    conf._input = $('<input type="text" id="'+conf.id+'">')
                        .autocomplete( conf.opts || {} );

                    return conf._input[0];
                },

                get: function ( conf ) {
                    return conf._input.val();
                },

                set: function ( conf, val ) {
                    conf._input.val( val );
                },

                enable: function ( conf ) {
                    conf._input.autocomplete( 'enable' );
                },

                disable: function ( conf ) {
                    conf._input.autocomplete( 'disable' );
                },

                canReturnSubmit: function ( conf, node ) {
                    if ( $('ul.ui-autocomplete').is(':visible') ) {
                        return false;
                    }
                    return true;
                },

                owns: function ( conf, node ) {
                    if ( $(node).closest('ul.ui.autocomplete').length ) {
                        return true;
                    }
                    return false;
                },

                // Non-standard Editor method - custom to this plug-in
                node: function ( conf ) {
                    return conf._input;
                },

                update: function ( conf, options ) {
                    conf._input.autocomplete( 'option', 'source', options );
                }
            };

        })(jQuery, jQuery.fn.dataTable);

    })

Answers

  • kthorngrenkthorngren Posts: 21,344Questions: 26Answers: 4,954

    It looks like the problem may be that the autoComplete plugin code is running after the Editor initialization. Try putting the autocomplete function before your $(document).ready. That way it runs before the Editor initialization.

    Kevin

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    Hi

    I tried as you suggested but still get a plain text box.

    I tried:

    (function ($, DataTable) {

            (function ($, DataTable) {
                ... 
            })(jQuery, jQuery.fn.dataTable);
    
            $(document).ready(function () {
                ...
            })
    

    and,

            $(document).ready(function () {
                (function ($, DataTable) {
                    ... 
                })(jQuery, jQuery.fn.dataTable);
    
                ...
            })
    
  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    Forgot to say I have added a ref. to file editor.autoComplete.js

  • kthorngrenkthorngren Posts: 21,344Questions: 26Answers: 4,954
    edited July 2019

    Forgot to say I have added a ref. to file editor.autoComplete.js

    Are you saying that you are loading editor.autoComplete.js and have the code in your Javascript as depicted above? If so you probably should do one or the other.

    The autocomplete plugin seems to work here:
    http://live.datatables.net/runogire/1/edit

    The inpiut doesn't look or behave much differently then the jQuery UI example:
    https://jqueryui.com/autocomplete/

    Are you seeing something different or expect something different? Maybe you can update my example to show the issue.

    Note: the field is not updated, likely due to how the base example is setup.

    Kevin

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    Hi

    Thanks. I think we're getting there. I'm fine just having the text box and the autocomplete entries hanging off it. The issue seems to now be w/ my refs. If I type say 'Item', I can scroll through the entires from the array but they don't show as a drop-down list underneath the text box. I have the following refs for that page:

        <script type="text/javascript" language="javascript" src="~/assets/scripts/jquery/js/jquery-3.3.1.min.js"></script>
        <script type="text/javascript" language="javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>
    
        <link rel="stylesheet" type="text/css" href="~/assets/scripts/adminlte/css/AdminLTE.min.css">
        <link rel="stylesheet" type="text/css" href="~/assets/scripts/adminlte/css/_all-skins.min.css">
        <script type="text/javascript" language="javascript" src="~/assets/scripts/adminlte/js/adminlte.min.js"></script>
    
        <link rel="stylesheet" type="text/css" href="~/assets/scripts/bootstrap/css/bootstrap.min.css">
        <script type="text/javascript" language="javascript" src="~/assets/scripts/bootstrap/js/bootstrap.min.js"></script>
    
        <link rel="stylesheet" type="text/css" href="~/assets/scripts/bootstrap-select/css/bootstrap-select.min.css" />
        <script type="text/javascript" language="javascript" src="~/assets/scripts/bootstrap-select/js/bootstrap-select.min.js"></script>
    
        <script type="text/javascript" language="javascript" src="~/assets/scripts/moment/js/moment.min.js"></script>
    
        @*<link rel="stylesheet" type="text/css" href="~/assets/scripts/editor-datatables/css/jquery.dataTables.min.css" />*@
        <link rel="stylesheet" type="text/css" href="~/assets/scripts/editor-datatables/css/rowReorder.dataTables.min.css" />
        <link rel="stylesheet" type="text/css" href="~/assets/scripts/editor-datatables/css/shCore.css" />
        <link rel="stylesheet" type="text/css" href="~/assets/scripts/editor-datatables/css/buttons.bootstrap.min.css" />
        <link rel="stylesheet" type="text/css" href="~/assets/scripts/editor-datatables/css/select.bootstrap.min.css" />
        <link rel="stylesheet" type="text/css" href="~/assets/scripts/editor-datatables/css/dataTables.bootstrap.min.css" />
        <link rel="stylesheet" type="text/css" href="~/assets/scripts/editor-datatables/css/editor.bootstrap.min.css" />
        <script type="text/javascript" language="javascript" src="~/assets/scripts/editor-datatables/js/jquery.dataTables.min.js"></script>
        <script type="text/javascript" language="javascript" src="~/assets/scripts/editor-datatables/js/dataTables.buttons.min.js"></script>
        <script type="text/javascript" language="javascript" src="~/assets/scripts/editor-datatables/js/buttons.bootstrap.min.js"></script>
        <script type="text/javascript" language="javascript" src="~/assets/scripts/editor-datatables/js/dataTables.select.min.js"></script>
        <script type="text/javascript" language="javascript" src="~/assets/scripts/editor-datatables/js/dataTables.rowReorder.min.js"></script>
        <script type="text/javascript" language="javascript" src="~/assets/scripts/editor-datatables/js/shCore.js"></script>
        <script type="text/javascript" language="javascript" src="~/assets/scripts/editor-datatables/js/dataTables.editor.min.js"></script>
        <script type="text/javascript" language="javascript" src="~/assets/scripts/editor-datatables/js/dataTables.bootstrap.min.js"></script>
        <script type="text/javascript" language="javascript" src="~/assets/scripts/editor-datatables/js/editor.bootstrap.min.js"></script>
        <script type="text/javascript" language="javascript" src="~/assets/scripts/editor-datatables/js/editor.autoComplete.js"></script>
    
        <link rel="stylesheet" type="text/css" href="~/assets/scripts/jquery-ui/css/jquery-ui-1.12.1.css">
        <script type="text/javascript" language="javascript" src="~/assets/scripts/jquery-ui/js/jquery-ui-1.12.1.js"></script>
    
    @*<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>*@
    
    @*<link href="https://nightly.datatables.net/css/jquery.dataTables.css" rel="stylesheet" type="text/css" />
    <script src="https://nightly.datatables.net/js/jquery.dataTables.js"></script>*@
    
    @*<link href="https://nightly.datatables.net/buttons/css/buttons.dataTables.css?_=c6b24f8a56e04fcee6105a02f4027462.css" rel="stylesheet" type="text/css" />
    <script src="https://nightly.datatables.net/buttons/js/dataTables.buttons.js?_=c6b24f8a56e04fcee6105a02f4027462"></script>*@
    
    @*<link href="https://nightly.datatables.net/select/css/select.dataTables.css?_=9a6592f8d74f8f520ff7b22342fa1183.css" rel="stylesheet" type="text/css" />
    <script src="https://nightly.datatables.net/select/js/dataTables.select.js?_=9a6592f8d74f8f520ff7b22342fa1183"></script>*@
    
    @*<link href="https://editor.datatables.net/extensions/Editor/css/editor.dataTables.min.css" rel="stylesheet" type="text/css" />
    <script src="https://editor.datatables.net/extensions/Editor/js/dataTables.editor.min.js"></script>*@
    
    <link href="https://nightly.datatables.net/autofill/css/autoFill.dataTables.css?_=bc3763029fa6dfaf4c947ef25f079107.css" rel="stylesheet" type="text/css" />
    <script src="https://nightly.datatables.net/autofill/js/dataTables.autoFill.js?_=bc3763029fa6dfaf4c947ef25f079107"></script>
    
    @*<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>*@
    
    @*<script src="https://cdn.datatables.net/plug-ins/1.10.19/dataRender/datetime.js"></script>*@
    
    @*<link href="http://code.jquery.com/ui/1.10.0/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
    <script src="http://code.jquery.com/ui/1.10.0/jquery-ui.js"></script>*@
    
        <link rel="stylesheet" type="text/css" href="~/assets/scripts/jquery-ui-combobox/css/jquery-combobox.css">
        <script type="text/javascript" language="javascript" src="~/assets/scripts/jquery-ui-combobox/js/jquery-combobox.js"></script>
    
        <script type="text/javascript" src="~/assets/scripts/jquery-filestyle/js/bootstrap-filestyle.min.js"></script>
    
  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    " I can scroll through the entires from the array"

    should read

    " I can scroll through the entries from the array with the arrow key on keyboard"

  • kthorngrenkthorngren Posts: 21,344Questions: 26Answers: 4,954

    Its hard to say without seeing it but it could be the load order. Try loading editor.autoComplete.js after jquery-ui-1.12.1.js.

    Kevin

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    Thnks. Will try.

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    It gives me only one entry if I scroll, 'Item 1' and won't go beyond that 1st item in the array, and no drop-down list.

  • kthorngrenkthorngren Posts: 21,344Questions: 26Answers: 4,954

    Without seeing the problem it would be difficult to offer suggestions to fix. Please update my example to replicate your issue.

    Kevin

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    Thanks. I have updated the exemple and included the refs I use. Many thanks.

  • kthorngrenkthorngren Posts: 21,344Questions: 26Answers: 4,954
    edited July 2019

    It does work. Once you start typing the list appears. This is the same behavior as shown in the jQuery AutoComplete example:
    https://jqueryui.com/autocomplete/

    However the row is being selected at the same time. You can control this if you like with select.selector. This example may help reduce confusion when selecting rows for editing.
    https://datatables.net/extensions/select/examples/initialisation/checkbox.html

    If you are expecting a drop down to appear when clicking the input maybe the select2 or selectize plugins will work better:
    https://editor.datatables.net/plug-ins/field-type/

    Note that when you update a live.datatables.net example the link may change and you should post the updated URL.

    Kevin

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    Hi, I'm sorry but amybe I'm missing something. I don't see the list if I type Item, eg. Please see attached screenshot.

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    Here's the link: http://live.datatables.net/runogire/2/edit

    Combobox was what I had originally but I'm fine using dropdown list.

  • kthorngrenkthorngren Posts: 21,344Questions: 26Answers: 4,954

    Here is your example:
    http://live.datatables.net/runogire/2/edit

    I was looking at inline editing and it works:

    Maybe its a CSS issue. @colin or @allan will need to take a look.

    Kevin

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    Many thanks again. I'm not using inline diting here.

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    I'm getting a different resul when clicking in the name cell: Noting happens under FF and only Item shows under Chrome:

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    oops, sorry, wrong cell.

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    Yes, you are correct.

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin

    Its a z-index issue. Add:

    .ui-menu {
      z-index: 1200 !important;
    }
    

    to your CSS: http://live.datatables.net/runogire/4/edit

    Allan

  • dynasoftdynasoft Posts: 446Questions: 69Answers: 3

    Many thanks Allan. That worked nicely.

This discussion has been closed.