本文介绍了单独的列搜索(文本输入)在我的rails代码中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单独的列搜索(文本输入)在我的rails代码中不起作用.
我的mappings.js文件:

Individual column searching (text inputs) is not working in my rails code.
My mappings.js file :

            $(document).ready(function(){

              $("table[role='example_datatable']").each(function(){
                var dataTableInstance = $('#mappings').DataTable({
                    "order": [[ 4, "desc" ]],       
                  columnDefs: [  
                    { "searchable": false, "targets": 6}, 
                    { "orderable": false, "targets": 6}
                  ],
                  "bFilter": false,
                  dom: 'Bfrtip',
                  pageLength: 10,
                  processing: true,
                  serverSide: true,
                  ajax: $(this).data('url')
               });


              $('#mappings tfoot th').each(function(){
                        var title = $('#mappings thead th').eq($(this).index()).text();
                        $(this).html( '<input type="text" placeholder="Search '+title+'" name="'+title+'" />' );
                    });


              dataTableInstance.columns().every(function () {
              alert("hi");
              var datatableColumn = this;

              $(this.footer()).find('input').on('keyup', function () {
               alert("hi2");
               datatableColumn.search(this.value).draw();
              });
              });



              });
            })

我的mappings.html.erb文件:

My mappings.html.erb file:

            <%= content_tag :table, 
                            role: :example_datatable, 
                            id: 'mappings', 
                            class: 'table reconciletable table-striped table-bordered table-hover', 
                            data: { url: mappings_path(format: :json)} do %>

              <tfoot>
                <tr>
                  <th>Id</th>
                  <th>Entity</th>
                  <th>Item</th>
                  <th>Group</th>
                  <th>Record Created On</th>
                  <th>Record Created By</th>
                </tr>
              </tfoot>


              <thead>
                <tr>
                  <th>Id</th>
                  <th>Entity</th>
                  <th>Item</th>
                  <th>Group</th>
                  <th>Record Created On</th>
                  <th>Record Created By</th>
                  <th></th>
                </tr>
              </thead>

              <tbody>

              </tbody>


            <% end %>

我能够获取每一列的输入文本,但是一旦我尝试搜索&没有任何反应,并且无法根据列搜索过滤我的数据表.这是我的代码.请帮帮我:'(

I'm able to get the input texts for each column but once I try searching & nothing happens and it's not able to filter my datatable according to column search. This is my code. please help me out :'(

推荐答案

尝试一下

//Apply the search
    table.columns().eq( 0 ).each( function ( colIdx ) {
        $( 'input', table.column( colIdx ).footer() ).on( 'keyup change', function () {
            table
                .column( colIdx )
                .search( this.value )
                .draw();
        } );
    } );

工作演示

这篇关于单独的列搜索(文本输入)在我的rails代码中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 23:38