11

我可以更改 dataTables 中搜索文本字段的宽度吗?

我现在正在编写以下代码,但它不起作用。

$('#example').dataTable()
          .columnFilter({   sPlaceHolder: "head:before",
                    aoColumns: [    { type: "text",width:"10px" },
                                { type: "date-range" },
                                                { type: "date-range" }
                        ]

        });

如果我的数据表是动态生成的,如下所示:

$('#example').dataTable({
                                "aaData": aDataSet,
                                "aoColumns": [
                                    { "sTitle": "#","sWidth": "10px" },
                                    { "sTitle": "ID" },
                                    { "sTitle": "Staff Name" },
                                    { "sTitle": "Rig Days" },
                                    { "sTitle": "Manager"},
                                    { "sTitle": "Grade"},
                                    { "sTitle": "Tools"},
                                    { "sTitle": "Vacations"},
                                    { "sTitle": "Presently On"},
                                    ]
                            });
                            }

如何在这个动态创建的 DataTable 中添加搜索字段以按每列搜索?

4

9 回答 9

18

上述解决方案都不适合我;然后我得到了这个:

$(document).ready(function () {             
  $('.dataTables_filter input[type="search"]').css(
     {'width':'350px','display':'inline-block'}
  );
});

而且效果很好!

如果您想在搜索框中也放置一个占位符,请使用此 ..

$('.dataTables_filter input[type="search"]').
  attr('placeholder','Search in this blog ....').
  css({'width':'350px','display':'inline-block'});

这肯定会奏效。

于 2016-06-23T21:57:19.577 回答
9

要更改搜索框的宽度,我所要做的就是进入我的.css文件并输入以下内容:

.dataTables_filter input { width: 300px }
于 2015-04-28T21:59:34.347 回答
3

它对我有用

    <script>
    var datatable = $('#table').DataTable({
    ...<datatables properties>,
    initComplete: function () {
    $('.dataTables_filter input[type="search"]').css({ 'width': '350px', 'display': 'inline-block' });
    }
</script>
于 2018-04-27T14:09:40.087 回答
2

唯一对我有用的是这个CSS。

$(document).ready(function(){

   $('#datatable-buttons_filter').css({"position":"relative","left":"-100px"});
});
于 2019-04-12T07:50:55.040 回答
1

尝试使用css改变宽度

例子

.text_filter{
    width:45%;
    min-width:200px;
}
于 2013-10-01T03:33:40.487 回答
1

我在我的环境中应用了这个解决方案(Laravel 5.2、yajra/Laravel-DataTables 6.0、Bootstrap 3.x):

我的桌子:

<table id="my-table" class="table table-striped table-hover" style="font-size: 80%">
<thead>
    <tr>
        <th class="input-filter" style="text-align:center;width: 5%">ID</th>
        ...
    </tr>
</thead>
<tbody></tbody>
<tfoot>
    <tr>
        <th style="text-align:center;width: 5%">ID</th>
        ...
    </tr>
</tfoot>

我的脚本:

<script type="text/javascript">
    var dt = $('#my-table').DataTable({
        stateSave: true,
        responsive: true,
        processing: true,
        serverSide: true,
        order: [[ 0, "desc" ]],
        lengthMenu: [[5, 10, -1], [5, 10, "All"]],
        ajax: {
            url: '...',
        },
        columns: [
            {data: 'id', name: 'id',orderable:true,searchable:true},
            ...
        ],
        language: {
            url: '....'
        },
        initComplete: function () {
            this.api().columns('.input-filter').every(function () {
                var column = this;
                var input = document.createElement("input");

                // start - this is the code inserted by me
                $(input).attr( 'style', 'text-align: center;width: 100%');
                // end  - this is the code inserted by me

                $(input).appendTo($(column.footer()).empty())
                .on('keyup', function () {
                    var val = $.fn.dataTable.util.escapeRegex($(this).val());
                    column.search(val ? val : '', true, true).draw();
                });
            });
        }
    });
</script>
于 2016-01-23T16:26:22.740 回答
0

Nativ JavaScript 解决方案。

$(document).ready(function() {
    $('#yourTableId').DataTable();

var addressTableFilter = document.getElementById('addressTable_filter');    
addressTableFilter.firstChild.getElementsByTagName('input')[0].style.width = "400px";
addressTableFilter.firstChild.getElementsByTagName('input')[0].setAttribute('placeholder', 'Search');
addressTableFilter.firstChild.removeChild(document.getElementById('addressTable_filter').firstChild.firstChild);
});

不要忘记将所有内容都包装在准备好的文档中(如果您使用它),否则其他行可能会在数据表启动之前启动,您将收到错误消息。这也将删除搜索标签并将其添加为输入框中的占位符。

于 2018-11-23T14:24:43.050 回答
0

我在使用 dataTables 时使用了以下代码

JS

$('#du_ft_table').dataTable( { <br/>
    "bProcessing": true,<br/>
    "bServerSide": true,<br/>
    scrollX: true,<br/>
    "sAjaxSource": "../server_processing_man.php?sourceC=du_ft&rights=1&mandatory=1&retailer_id=2725",
    "aoColumns": [
        null,
        null,
        null,
        null,
        null,
        null,

        null,
        null,
        null,
        null,
        { "mData": function(obj) { 
                return '<a class="btn btn-xs btn-primary" href="EBSNL_NW='+obj[0]+'" alt="Edit" title="Edit"><i class="fa fa-pencil-square-o"></i></a>';
        }}
    ]
} );

$('.dataTables_scrollFootInner tfoot th').each( function () {<br/>
    var title = $(this).text();<br/>
    if(title != '')  $(this).html( '<input type="text" placeholder="Search '+title+'" />' );<br/>
});

var duft_table = $('#du_ft_table').DataTable();

// Apply the search
duft_table.columns().every( function () {<br/>
    var that = this;<br/>

    $( 'input', this.footer() ).on( 'keyup change', function () {<br/>
        if ( that.search() !== this.value ) {<br/>
            that.search(this.value).draw();<br/>
        }<br/>
    } );
});  

在此处输入图像描述

于 2017-05-04T10:29:22.550 回答
0

这是中继器

      <asp:Repeater ID="rptClients" runat="server">
                    <HeaderTemplate>
                        <table id="example" class="display">
                            <thead>
                                <tr style="color:#fff;">
                                    <th>Edit</th>
                                    <th>Client Name</th>
                                    <th>Address</th>
                                    <th>City</th>
                                    <th>State</th>
                                    <th>Zip</th>
                                    <th>Phone</th>
                                    <th>Fax</th>
                                    <th>Client Type</th>
                                    <th>Active</th>
                                </tr>
                                <tr id="filterRow">
                                    <td>Edit</td>
                                    <td>Client Name</td>
                                    <td>Address</td>
                                    <td>City</td>
                                    <td>State</td>
                                    <td>Zip</td>
                                    <td>Phone</td>
                                    <td>Fax</td>
                                    <td>Client Type</td>
                                    <td>Active</td>
                                </tr>
                            </thead>
                            <tfoot style="display:none;">
                                <tr>
                                    <td>&nbsp;</td>
                                </tr>
                            </tfoot>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <tr>
                            <td><div class="padBtm-05 padTop-10"><asp:Button runat="server" ID="btnEdit" Text="Edit" /></div></td>
                            <td>
                                <%# Container.DataItem("ClientName")%>
                            </td>
                            <td>
                                <%# Container.DataItem("ClientAddress")%>
                            </td>
                            <td>
                                <%# Container.DataItem("ClientCity")%>
                            </td>
                            <td>
                                <%# Container.DataItem("State")%>
                            </td>
                            <td>
                                <%# Container.DataItem("ClientZip")%>
                            </td>
                            <td>
                                <%# Container.DataItem("ClientPhone")%>
                            </td>
                            <td>
                                <%# Container.DataItem("ClientFax")%>
                            </td>
                            <td>
                                <%# Container.DataItem("ClientType")%>
                            </td>
                            <td>
                                <%# Container.DataItem("Active")%>
                            </td>
                        </tr>
                    </ItemTemplate>
                    <FooterTemplate>
                        </table>
                    </FooterTemplate>
                </asp:Repeater>

所有列都有过滤器。在这里,我实际上隐藏了编辑列过滤器,因为它不需要一个。如果我不为此列呈现过滤器,我的过滤将关闭 1 列。在 javascript 中,我以我的表格行为目标,该行最终将成为我的标题占位符。根据占位符名称,您可以运行一个 if 语句,该语句允许您定位输入元素并设置项目的宽度。我发现设置过滤器的宽度会设置表格上的列宽。

 // apply the input styling
 $('#example thead td').each(function (i) {
            var title = $('#example thead th').eq($(this).index()).text();

            if (title == "Edit") {
                var serach = '<input type="text" style="display:none;" placeholder="' + title + '" />';
            } else if (title == "Client Name") {
                var serach = '<input type="text" style="width:370px;" placeholder="' + title + '" />';
            } else if (title == "Zip") {
                var serach = '<input type="text" style="width:50px;" placeholder="' + title + '" />';
            } else {
                var serach = '<input type="text" placeholder="' + title + '" />';
            }

            $(this).html('');
            $(serach).appendTo(this).keyup(function () { table.fnFilter($(this).val(), i) })
        });


  // Apply the search
        table.columns().every(function () {
            var that = this;

            $('input', this.footer()).on('keyup change', function () {
                if (that.search() !== this.value) {
                    that
                        .search(this.value)
                        .draw();
                }
            });
        });
于 2016-05-09T22:33:13.207 回答