【问题标题】:How can I force the jQuery Datatables header sort icon to the far right or far left of the cell?如何将 jQuery Datatables 标题排序图标强制到单元格的最右侧或最左侧?
【发布时间】:2011-12-21 15:08:27
【问题描述】:

默认情况下,列标题上的数据表排序图标显示在列标题文本下方。我想让它出现在同一行以及标题单元格的最右侧或最左侧。

以下是其中一个列标题的 HTML 显示方式(在 Firebug 中)。

<th class="ui-state-default" rowspan="1" colspan="1" style="width: 252px;">
    <div class="DataTables_sort_wrapper" style="text-align: left;">
        Account Name
        <span class="DataTables_sort_icon css_right ui-icon ui-icon-triangle-1-n"></span>
    </div>
</th>

我在&lt;span&gt;css-right 类中添加了display: inline-block;,以强制它出现在与herehere 相同的行上。但是,我仍然找不到如何强制图标出现在最右边或最左边。添加float:left;float:right; 会使图标跳到下一行。

有什么想法吗?

【问题讨论】:

  • 一个真正棘手的问题。开发者节目确实对此有所了解。我在多个项目中使用过 Datatables 并遇到了同样的问题。我喜欢解决方案的开发以及在这里所做的所有贡献。特别@user1301334 回答

标签: jquery datatables


【解决方案1】:

对我来说table.display 不存在,我添加了以下内容:

table.dataTable thead th div.DataTables_sort_wrapper {
    position: relative;
    padding-right: 20px;
}

table.dataTable thead th div.DataTables_sort_wrapper span {
    position: absolute;
    top: 50%;
    margin-top: -8px;
    right: 0;
}

以上对我有用。

【讨论】:

  • Matt K 的答案是一样的,但他比你早一年多这样做。
【解决方案2】:

虽然 Greg 的回答让我找到了解决方案(并且是我接受的答案),但为了清楚起见,并作为对可能遇到此问题的其他人的帮助,我添加了自己的答案。

对排序图标的定位影响最大的是这个默认的 DataTables CSS:

table.display thead th div.DataTables_sort_wrapper {
    padding-right: 20px;
    position: relative;
}

table.display thead th div.DataTables_sort_wrapper span {
    margin-top: -8px;
    position: absolute;
    right: 0;
    top: 50%;
}

如果class="display"属性不在表格中,则不应用上面的CSS。

【讨论】:

    【解决方案3】:

    我不知道这是否是最好的方法,但这是我在我的 jQuery-UI 数据表中看到的那个符号:

    来自站点的主样式表(不是 jQuery UI 样式表的一部分):

    .display thead th .DataTables_sort_wrapper span { float: right; }

    来自 jQuery UI CSS 文件:

    .ui-icon { 
      display: block; 
      // ... some other stuff including overflow: hidden 
    }
    

    当然还有其他一些风格,但我认为这些才是最重要的。根据您的问题,我很惊讶它还没有为您工作。

    【讨论】:

    • 当我应用你提到的样式时,文本和图标都向右浮动。我想将文本保留在左侧并将图标浮动到右侧,或者将图标浮动到文本的左侧。
    • 很难说。我记得我不得不折腾一下,很可能我们都有不同的风格。如果你有实时链接,我可以四处看看..?
    • 很遗憾,我没有实时链接,因为代码托管在公司内部网站上。如果这个问题留在那里,没有答案,足够长,我可能会在 jsFiddle 上放一些东西来玩。
    • Greg,在大量搜索 css 并弄乱了 jsFiddle 之后,我发现我忽略了一件事。我正在使用的表缺少 class="display" 属性。因为 css (.display thead th .DataTables_sort_wrapper span) 依赖于它,所以排序图标无法正确呈现。此外,执行此操作的 css 是:padding-right: 20px; position: relative; on DataTables_sort_wrapper 和:position: absolute; right: 0; top: 50%; on DataTables_sort_wrapper span。感谢您的回答,它使我得出了这个结论。
    • 在 jQuery UI、DataTables 和特定于站点的类之间,跟踪它们肯定会变得很棘手。 :-) 很高兴我的回答帮助您找到了结论,即使它不是独立的。
    【解决方案4】:

    问题在于,Datatables [经常] 将排序按钮放在文本下方。

    OP 特别想要文本右侧或左侧的按钮; 但是,http://britseyeview.com/ 的 Steve Teale 有一个解决方案,可以将排序按钮向上移动到文本上方和单元格的右上角。意思是,排序按钮总是在右上角,但有时在文本上方。基本上他重置了原来的 CSS。

    Steve 写道:如果我使用实现这些 [排序按钮] 的开箱即用背景样式,它们会出现在列名后面,所以我改用了:

    .sorting_asc {
      padding-right:18px;
      cursor: pointer;
      background: url('sort_asc.png') no-repeat top right;
    }
    
    .sorting_desc {
      padding-right:18px;
      cursor: pointer;
      background: url('sort_desc.png') no-repeat top right;
    }
    
    .sorting {
      padding-right:18px;
      cursor: pointer;
      background: url('sort_both.png') no-repeat top right;
    }
    
    .sorting_asc_disabled {
      padding-right:18px;
      cursor: pointer;
      background: url('sort_asc_disabled.png') no-repeat top right;
    }
    
    .sorting_desc_disabled {
      padding-right:18px;
      cursor: pointer;
      background: url('sort_desc_disabled.png') no-repeat top right;
    }
    

    您需要将您的路径添加到“background: url('/your/path/sort_both.png')”。

    Steve 的“数据表入门”在这里: http://britseyeview.com/software/dtgs/

    【讨论】:

      【解决方案5】:

      适用于最新版本的数据表(截至 15 年 3 月 31 日)。 在数据表 CSS 中更改以下内容-

      table.table thead .sorting,
      table.table thead .sorting_asc,
      table.table thead .sorting_desc,
      table.table thead .sorting_asc_disabled,
      table.table thead .sorting_desc_disabled {
      
          cursor: pointer;
          *cursor: hand;
          background-position: left center;
          padding-left: 20px;
      }
      
      table.table thead .sorting { background: url('../img/sort_both.png') no-repeat center left; }
      table.table thead .sorting_asc { background: url('../img/sort_asc.png') no-repeat center left; }
      table.table thead .sorting_desc { background: url('../img/sort_desc.png') no-repeat center left; }
      

      【讨论】:

        【解决方案6】:

        作为上述答案的替代方案,我使用了以下内容(基于早期的见解)。 对我来说好处是右侧没有添加额外的填充,这对于没有排序图标的列很有帮助。 (DataTables 仍然分配 DataTable_sort_wrapper 类)。

        table.dataTable th>div.DataTables_sort_wrapper {
          position: relative;
          white-space: nowrap;
        }
        table.dataTable th > div.DataTables_sort_wrapper > span.DataTables_sort_icon.css_right {
          display: inline-block;
          position: absolute;
          right: 0px;
          vertical-align: top;
        }
        

        【讨论】:

          【解决方案7】:
          table.dataTable thead .sorting,
          table.dataTable thead .sorting_asc,
          table.dataTable thead .sorting_desc,
          table.dataTable thead .sorting_asc_disabled,
          table.dataTable thead .sorting_desc_disabled {
              background-position: left center !important;
              background-repeat: no-repeat;
          }
          

          【讨论】:

            猜你喜欢
            • 2019-12-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-06-16
            • 1970-01-01
            相关资源
            最近更新 更多