简体   繁体   English

自动完成数据表jquery搜索

[英]Auto complete with datatables jquery search

I want to implement Auto complete http://jqueryui.com/autocomplete/ to filter on each column 我想实现自动完成http://jqueryui.com/autocomplete/来过滤每一列

in datatables jquery plugin. 在datatables jquery插件中。

For example if i want to search Embeded Devices with ED in datatables search it will not do it for me...So i want to show auto complete and when user select it from list then i want datatable to filter. 例如,如果我想在数据表搜索中搜索嵌入式设备ED,它将不会为我做...所以我想显示自动完成,当用户从列表中选择它然后我想要数据表过滤。

var oTable = $('#listings_row').dataTable( );
$("thead input").keyup( function (
                oTable.fnFilter( this.value, parseInt($(this).attr('id')) );
            } );


            $("thead input").each( function (i) {
                asInitVals[i] = this.value;
            } );

            $("thead input").focus( function () {
                if ( this.className == "search_init" )
                {
                    this.className = "";
                    this.value = "";
                }
            } );

            $("thead input").blur( function (i) {
                if ( this.value == "" )
                {
                    this.className = "search_init";
                    this.value = asInitVals[$("#listings_row thead input").index(this)];
                }
            } );

How i can do it? 我怎么能这样做?

You can use my plugin for that , take a look at the example : 4'th column 您可以使用我的插件,看看示例:第4列

Here is the link to the github of my plugin: 这是我的插件的github的链接:

Yet Another DataTables Column Filter - (yadcf) 另一个DataTables列过滤器 - (yadcf)

Here is a code snippet, just set enable_auto_complete: true in relevant columns (in the below code column_number : 3 ): 这是一个代码片段,只需在相关列中设置enable_auto_complete: true (在下面的代码column_number : 3 ):

$(document).ready(function(){
  $('#example').dataTable().yadcf([
    {column_number : 0},
    {column_number : 1, filter_container_id: "external_filter_container"},
    {column_number : 2, data:["Yes","No"], filter_default_label: "Select Yes/No"},
    {column_number : 3, text_data_delimiter: ",", enable_auto_complete: true},
    {column_number : 4, column_data_type: "html", html_data_type: "text", filter_default_label: "Select tag"}]);
});

It might be too late,but after so much research and googling I ended up writing my own autocomplete.It was little tedious but the good thing is it works.First I built the js array containing all the columns of the table.Kept the array as source for autocomplete my own textbox and used it for search. 这可能为时已晚,但经过如此多的研究和谷歌搜索,我最终编写了自己的自动完成。这有点乏味,但好处是它有效。首先我构建了包含表的所有列的js数组。保持数组作为自动填充我自己的文本框的源并用于搜索。 One trick is embed an anchor tag inside the td tags to enable to set focus on the searched text. 一个技巧是在td标签内嵌入一个锚标签,以便能够将焦点设置在搜索到的文本上。 oTable is my datatable.myInputTextField is out of the box input box where I can search for the text.To enable facebook like autocomplete use the @ in the box. oTable是我的datatable.myInputTextField是开箱即用的输入框,我可以搜索文本。要启用像自动完成的Facebook,请使用框中的@。

    $("#myInputTextField").autocomplete({

    source:filterFieldValues,
    select:function(event,ui){          
        {
            if(ui!=null&&ui.item!=null){
                var searchedColumnValue=ui.item.value;
                if(searchedColumnValue!=null||searchedColumnValue!=''){
                    $("#myInputTextField").val('');
                    $("#myInputTextField").val(searchedColumnValue.trim());
                    var colValue=$("#myInputTextField").val();
                    $("a:contains('"+colValue+"')").parents("td").toggleClass("focus");
                    oTable.search($(this).val()).draw();                            
                    window.setTimeout(function(){
                        $("a:contains("+colValue+")").focus();
                        }, 5);

                }
            }
        }           
    },
    focus:function(event,ui){

    }
    }).autocomplete('disable')
    .on('keypress', function(e) {
        evt=e||window.event;               
        var code = evt.charCode || evt.keyCode;         
        //Detect whether '@' was keyed.
        if (evt.shiftKey && code === 64) {
            $(this).autocomplete('enable');
            return false;
        }           
        oTable.search($(this).val()).draw();
    });


$("#myInputTextField").blur(function()
{
    $("#myInputTextField").autocomplete('disable');
    oTable.search($(this).val()).draw();
});



$('#myInputTextField').on('input propertychange paste', function() {        
        oTable.search($(this).val()).draw();        
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM