从下拉列表的“特定列”中进行数据表搜索

时间:2019-02-27 15:31:05

标签: jquery datatable

我有一个包含四列的表格:

name | description | profile | action 

我想从单列(即name)中搜索/过滤记录。我试过了,但是它搜索了所有列:

var oTable = $('#carrier-profile-listing').DataTable({
             "pagingType": "full_numbers",
             "serverSide": true,
             "processing": false,
             "paging": true,
             "searching": { "regex": true },
             "pageLength": 50,
             "lengthMenu": [
                 [15, 20, 50, -1], [15, 20, 50, "All"] // change per page values here
             ],
             "initComplete": function() {
                 var $searchInput = $('div.dataTables_filter input');
                 $searchInput.unbind();
                 $searchInput.bind('keyup', function(e) {
                     if(e.keyCode == 13) {
                         oTable.search( this.value ).draw();
                     }
                 });
             },
             "ajax": {
                 "url": "/carriers_profile_list",
                 data: {"_token": "{{ csrf_token() }}"},
                 "type": "POST",
                 "dataType": 'json',
                 "async": true,
                 beforeSend: function () {
                     showAjaxLoader();
                 },
                 complete: function () {
                     hideAjaxLoader();
                 },
                 error: function (jqXHR, timeout, message) {
                 }
             },
             "columnDefs": [
                 { "width": "20%", "targets": 0 },
                 { "width": "20%", "targets": 1 },
                 { "width": "30%", "targets": 2 },
                 { "width": "30%", "targets": 3 }

             ],
             "columns": [
                 {
                     "data"      : "name",
                     "name"      : "name",
                     "orderable" : true
                 },
                 {
                     "data"      : "description",
                     "name"      : "description",
                     "orderable" : true
                 },
                 {
                     "data"      : "profile",
                     "name"      : "profile",
                     "orderable" : false
                 },
                 { "mRender": function ( data, type, row ) {
                    return '<a href="/commission-process/'+row['id']+'/edit">Edit</a> -- <a class="donot_check_form_data" href="/commission-process/download_mapped_sheet/'+row['id']+'">Download Sheet</a>';}
                 }
             ],

             fixedHeader: {
                header: true,
                 headerOffset: fixedHeaderOffset
            },
             "language": {
                 "lengthMenu": "Display _MENU_ records per page",
                 "zeroRecords": "No records found",
                 "infoEmpty": "No records available",
                 "infoFiltered": "(filtered from _MAX_ total records)"
             }
         });

$("#carrier-drpdwn").on("change", "select[name='carrier_id']", function() {
  var textSelected =   $("#carrier-drpdwn option:selected").text();
  oTable.search(textSelected).draw(); 
});

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

要实现此目的,您可以在DataTable中的各个search()对象上调用column。例如,如果要搜索索引为name的{​​{1}}列,请执行以下操作:

0

答案 1 :(得分:-1)

如果使用的是Datatables jQuery插件,则可以在其网站上找到很多非常有用的帮助。例如,示例https://datatables.net/examples/api/multi_filter.html将帮助您完成请求。