【问题标题】:DataTables Filtering on Alt Strings基于 Alt 字符串的 DataTables 过滤
【发布时间】:2019-04-22 21:59:46
【问题描述】:

使用 DataTables 1.10.19 和 this plugin,我使用 alt 属性作为要排序的数据。

这行得通;

{
    targets: [7],
    type: 'alt-string',
    render: function(data, type, row) {
        if (data == 1) {
            return '<a href="example.com"><i class="icon-ok" alt="Processed"></i></a>';
        }
    }
}

这不起作用;

{
    targets: [7],
    type: 'alt-string',
    render: function(data, type, row) {
        if (data == 1) {
            return '<a href="example.com?id=' + row[0] + '&approvalcode=' + row[9] + '"><i class="icon-ok" alt="Processed"></i></a>';
        }
    }
}

似乎不是当我添加row URL 查询字符串时,它会破坏alt 过滤器,尽管其他一切都按预期工作。

插件代码如下;

/**
 * Sort on the 'alt' tag of images in a column. This is particularly useful if
 * you have a column of images (ticks and crosses for example) and you want to
 * control the sorting using the alt tag.
 *
 *  @name Alt string
 *  @summary Use the `alt` attribute of an image tag as the data to sort upon.
 *  @author _Jumpy_
 *
 *  @example
 *    $('#example').dataTable( {
 *       columnDefs: [
 *         { type: 'alt-string', targets: 0 }
 *       ]
 *    } );
 */

jQuery.extend( jQuery.fn.dataTableExt.oSort, {
    "alt-string-pre": function ( a ) {
        return a.match(/alt="(.*?)"/)[1].toLowerCase();
    },

    "alt-string-asc": function( a, b ) {
        return ((a < b) ? -1 : ((a > b) ? 1 : 0));
    },

    "alt-string-desc": function(a,b) {
        return ((a < b) ? 1 : ((a > b) ? -1 : 0));
    }
} );

【问题讨论】:

  • 您能否从您的 html 中展示一个更好的代码摘录以及您如何初始化数据表和插件。
  • 好吧,看看这个FIDDLE,告诉我这是不是你想要的。 Ofc 你必须调整目标行,你的数据会有所不同。但我想你可以得到row[wherevermylinkisindex]
  • 好吧,现在最后一次尝试。如果条件为真,它将使用查询参数和 alt 标记呈现链接。按 alt 排序将起作用。切换到 codepen 导致小提琴有点粉碎了我的记忆。 Example

标签: javascript jquery html datatables filtering


【解决方案1】:

第一列的初始值为01。如果值为1,则插入带有查询参数的链接,&lt;i&gt; 将获得alt="Processed"。否则将插入不带参数的链接,alt 值为"Not Processed"。然后,呈现的表将按第一列内&lt;i&gt; 的 alt 属性排序。我也只是将 html 字符串与+ 连接起来。我将数据值解析为 int,因为它是作为字符串处理的。如果我不这样做,它将永远不会附加查询参数。

$(document).ready(function() {
  $("#example").DataTable({
    columnDefs: [
      {
        type: "alt-string",
        targets: 0,
        render: function(data, type, row, meta) {
          if (parseInt(data) === 1) {
            return (
              '<a href="www.example.com?id=' +
              row[2] +
              "&approvalcode=" +
              row[3] +
              '"><i class="icon-ok" alt="Processed">link' +
              row[2] +
              "</i></a>"
            );
          } else {
            return (
              '<a href="www.example.com"><i class="icon-ok" alt="New">link' +
              row[2] +
              "</i></a>"
            );
          }
        }
      }
    ]
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" rel="stylesheet"/>
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/plug-ins/1.10.19/sorting/alt-string.js"></script>
<table id="example" class="display" style="width:100%">
  <thead>
    <tr>
      <th>Link</th>
      <th>Condition</th>
      <th>ID</th>
      <th>Code</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td>1</td>
      <td>1</td>
      <td>1007</td>
    </tr>
    <tr>
      <td>0</td>
      <td>0</td>
      <td>2</td>
      <td>7001</td>
    </tr>
    <tr>
      <td>1</td>
      <td>1</td>
      <td>3</td>
      <td>42</td>
    </tr>
    <tr>
      <td>0</td>
      <td>0</td>
      <td>4</td>
      <td>1337</td>
    </tr>
    <tr>
      <td>1</td>
      <td>1</td>
      <td>5</td>
      <td>80085</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <th>Link</th>
      <th>Condition</th>
      <th>ID</th>
      <th>Code</th>
    </tr>
  </tfoot>
</table>

【讨论】:

  • 因此在您的示例中,搜索“已处理”应显示条件 = 1? 的结果。因为这行不通。
  • 上面的例子只是展示了动态渲染内容的alt标签排序,而不是过滤/搜索。您可以搜索链接名称,但不能搜索 alt 标签。
  • 如果您需要进行搜索,不妨看看Custom filtering
猜你喜欢
  • 2019-08-16
  • 1970-01-01
  • 2018-02-19
  • 2011-12-05
  • 1970-01-01
  • 1970-01-01
  • 2018-06-21
  • 2019-12-25
  • 2014-02-21
相关资源
最近更新 更多