【问题标题】:Remove column filter from datatables using multicolumn filtering and childrows使用多列过滤和子行从数据表中删除列过滤器
【发布时间】:2014-06-29 08:17:04
【问题描述】:

我正在使用利用多列过滤选项和子行的数据表。

第一列是为子行的控制图像保留的。我需要从第一列中删除搜索输入,因为它没有数据。

我不知道如何删除第一列搜索。我需要这方面的帮助!

JS信息

/* Formatting function for row details - modify as you need */
function format ( d ) {
// `d` is the original data object for the row
return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;padding-right:50px;">'+
    '<tr>'+
        '<td>Category:</td>'+
        '<td>'+d.LongDesc+'</td>'+
        '<td rowspan="3"><img src="' + d.Thumb + '"/></td>'+
    '</tr>'+
    '<tr>'+
        '<td>Location:</td>'+
        '<td>'+d.Location+'</td>'+
    '</tr>'+
    '<tr>'+
        '<td>Notes:</td>'+
        '<td>'+d.Notes+'</td>'+
    '</tr>'+
'</table>';
}

$(document).ready(function() {
 // Setup - add a text input to each footer cell
  $('#example thead th').each( function () {
    var title = $('#example thead th').eq( $(this).index() ).text();
    $(this).html( '<input type="text" placeholder="Search '+title+'" />' );
  } );

  var table = $('#example').DataTable( {
    "ajax": "../data/cat-data.txt",
    "columns": [
        {
            "class":          'details-control',
            "orderable":      false,
            "data":           null,
            "defaultContent": ''
        },
        { "data": "Year" },
        { "data": "Make" },
        { "data": "Model" },
        { "data": "Engine" },
        { "data": "PartNumber" }
    ],
    "order": [[1,'asc'], [2,'asc'], [3,'asc'], [4,'asc'], [5,'asc']],
    "bSort": false,
    "bPaginate": true,
    "bLengthChange": true,
    "bInfo": false,
    "bAutoWidth": true,
    "iCookieDuration": 60*60*24, // 1 day 
  } );

  // Add event listener for opening and closing details
  $('#example tbody').on('click', 'td.details-control', function () {
    var tr = $(this).parents('tr');
    var row = table.row( tr );

    if ( row.child.isShown() ) {
        // This row is already open - close it
        row.child.hide();
        tr.removeClass('shown');
    }
    else {
        // Open this row
        row.child( format(row.data()) ).show();
        tr.addClass('shown');
    }
  } );

   // Apply the filter
  $("#example thead input").on( 'keyup change', function () {
    table
        .column( $(this).parent().index()+':visible' )
        .search( this.value )
        .draw();
   } );


} );

【问题讨论】:

  • 您是否使用 columnfilter 插件来实现搜索列功能?
  • 不,我正在使用带有数据表 1.10 的 api 多过滤器选择

标签: jquery search datatable multiple-columns


【解决方案1】:

试试下面的代码

var table = $('#example').DataTable( {
    "ajax": "../data/cat-data.txt",
    "columns": [
        {
            "class":          'details-control',
            "orderable":      false,
            "data":           null,
            "defaultContent": '',
            "searchable": false 
            ^------------------^ // added this to disable search on first column
        },
        { "data": "Year" },
        { "data": "Make" },
        { "data": "Model" },
        { "data": "Engine" },
        { "data": "PartNumber" }
    ],
    "order": [[1,'asc'], [2,'asc'], [3,'asc'], [4,'asc'], [5,'asc']],
    "bSort": false,
    "bPaginate": true,
    "bLengthChange": true,
    "bInfo": false,
    "bAutoWidth": true,
    "iCookieDuration": 60*60*24, // 1 day 
  } );

更多详情请参考this链接。

如果您无法删除搜索,请修改您的代码,如下所示

$('#example thead th').each( function () {
        var title = $('#example thead th').eq( $(this).index() ).text();
        if($(this).index() !=0) // check if this is not first column header
        $(this).html( '<input type="text" placeholder="Search '+title+'" />' );
    } );

【讨论】:

  • 我已经尝试过了,但是没有用。我在网站上的帖子中尝试了很多不同的东西,但都没有奏效。
  • 控制台有错误吗? .. 最好在 jsfiddle 上分享您的代码,以便我可以帮助您。
  • 我想我需要在 'code'$(document).ready(function() { // 设置 - 向每个页脚单元格中添加文本输入 $ ('#example thead th').each( function () { var title = $('#example thead th').eq( $(this).index() ).text(); $(this).html ( '' ); } );
  • [link]live.datatables.net/leqewub/1 在 chrome 中工作,但在 live.datatables.net 上不能在 firefox 中使用
  • 我可以在浏览器控制台上看到“Uncaught SyntaxError: Unexpected token
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-26
  • 1970-01-01
  • 2013-09-09
  • 1970-01-01
  • 2022-01-22
  • 2021-05-06
  • 1970-01-01
相关资源
最近更新 更多