【问题标题】:How to filter selected value of select in table using datatables如何使用数据表过滤表中选择的选定值
【发布时间】:2019-10-16 12:33:06
【问题描述】:

*我的 jsfiddle:http://jsfiddle.net/6e9r2js1/

这是某人的参考工作链接: http://jsfiddle.net/8c96qx2z/

这是某人的参考工作链接: http://jsfiddle.net/a3o3yqkw/

这是我的代码:

<table id="tabledetail">
<thead>
  <tr>
     <th>S.#</th>
     <th>Name</th>
     <th>Type</th>
   </tr>
</thead>
<tbody>
   <tr>
      <td>1</td>
      <td><input type="text" id="name_1" name="myName[]" value="Ben"></td>
      <td>
          <select class="form-control" id="type_1" name="myType[]">
              <option value="0" selected>No</option>
              <option value="1">Yes</option>
          </select>
      </td>
   </tr>
   <tr>
       <td>2</td>
      <td><input type="text" id="name_2" name="myName[]" value="Dan"></td>
      <td>
          <select class="form-control" id="type_2" name="myType[]">
              <option value="0">No</option>
              <option value="1" selected>Yes</option>
          </select>
      </td>
   </tr>
</tbody>
</table>

<script>
    var table = $('#tabledetail').DataTable({
        'orderCellsTop': true,
        'fixedHeader'  : true,
        'paging'       : true,
        'lengthChange' : true,
        'searching'    : true,
        'ordering'     : true,
        'info'         : true,
        'autoWidth'    : true,
        'columnDefs'   : [{ "type": "html-input", "targets": ['_all'] }]
    });
</script>

在键入它不搜索...

【问题讨论】:

  • 我检查了你的 jsfiddle 代码,我觉得很好
  • 那是其他人 jsfiddle 我想在我的代码中实现它..
  • 这里我尝试了这段代码但不起作用datatables.net/forums/discussion/54017/…
  • 动态创建列值,然后将函数分配给选择
  • 列选择的选定值已经动态地来自数据库,我希望我的搜索过滤器仅适用于选择和输入的选定值以及 td 中的简单文本

标签: jquery datatables


【解决方案1】:

我从这个 stackoverflow 答案中找到了一个小提琴网址:https://stackoverflow.com/a/27860934/540195

我已经更新了那个小提琴 (http://jsfiddle.net/s2gbafuz/),现在它工作正常,

您可以查看分叉的工作小提琴链接: http://jsfiddle.net/amitmondal/zc5a3eox/1/

$.fn.dataTableExt.ofnSearch['html-input'] = function(value) {
   return $(value).val();
};

var table = $("#example").DataTable({
    columnDefs: [
       { "type": "html-input", "targets": [1, 2, 3] }
    ] 
});

$("#example td input").on('change', function() {
    var $td = $(this).parent();
    $td.find('input').attr('value', this.value);
    table.cell($td).invalidate().draw();
});
$("#example td select").on('change', function() {
    var $td = $(this).parent();
    var value = this.value;
    $td.find('option').each(function(i, o) {
      $(o).removeAttr('selected');
      if ($(o).val() == value) {
      	 $(o).attr('selected', true);
         $(o).prop('selected', true);
      } 
    })
    table.cell($td).invalidate().draw();
});

如果您需要任何进一步的帮助,请随时告诉我。

【讨论】:

  • 嗨,你能检查一下我的小提琴jsfiddle.net/6e9r2js1我在那里实现了你的代码但不起作用
  • 我没有在你的小提琴中看到我的代码,请你再检查一遍好吗?
  • 是的,你是对的:),我提到了小提琴网址,现在也分享答案链接。
  • 请检查我要修复的这个小提琴jsfiddle.net/6e9r2js1/2你给定的小提琴我已经在搜索相同的stackoverflow分析器时提到了问题,我想要我的小提琴工作,谢谢。跨度>
  • 我设法解决了一些问题,请参阅这个小提琴jsfiddle.net/k7ymhgrs 我认为问题在于需要告知列号在哪里搜索的目标,因为我又添加了 1 个列,它开始起作用了
猜你喜欢
  • 1970-01-01
  • 2018-04-20
  • 2017-05-24
  • 2021-03-08
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 2020-10-09
  • 1970-01-01
相关资源
最近更新 更多