【问题标题】:jquery tablesorter add title/tooltip to show ascending/descendingjquery tablesorter 添加标题/工具提示以显示升序/降序
【发布时间】:2012-02-20 02:24:59
【问题描述】:

我正在使用 jquery 表排序器。我有一张像这里的桌子:

http://tablesorter.com/docs/

除了我希望每个列标题在鼠标悬停时都有一个标题/工具提示。例如,名字的默认标题将是“按名字排序”,然后它将更改为“对名字进行升序排序”或“对名字进行降序排序”,依此类推。由于图像在标题图像的 css 中,而不是在每个 th 上设置,我该怎么做?

table.tablesorter thead tr .headerSortDown {          background-image: url(desc.gif);  } 
table.tablesorter thead tr .headerSortUp {          background-image: url(asc.gif);  } 
table.tablesorter thead tr .header {          background-image: url(bg.gif);          background-    repeat: no-repeat;          background-position: center right;          cursor: pointer;  } 

【问题讨论】:

    标签: jquery image sorting html-table tablesorter


    【解决方案1】:

    这就是我的做法...为每个标题单元格设置一个数据标题,其中包含您要添加的文本。但不是说“by”、“ascending”或“descending”,我们将使用一个名为“{dir}”的可替换变量来表示方向:

    <table class="tablesorter">
         <thead>
             <tr>
                 <th data-title="sort {dir} last name">Last</th>
                 <th data-title="sort {dir} first name">First</th>
             </tr>
         </thead>
         <tbody>
             <tr><td>Smith</td><td>John</td></tr>
             <tr><td>Jones</td><td>Timothy</td></tr>
             <tr><td>Wong</td><td>Jin</td></tr>
             <tr><td>O'Brien</td><td>Shaun</td></tr>
             <tr><td>Parker</td><td>Peter</td></tr>        
         </tbody>
     </table>
    

    然后我们添加一个函数来遍历每个表格标题单元格并根据它找到的类名更新标题。调用它,并确保在每个表排序完成后调用它。

    var table = $('table'),
    
        updateTitles = function(){
            var t, $this;
            table.find('thead th').each(function(){
                $this = $(this);
                t = "by";
                if ($this.hasClass('headerSortUp')) {
                    t = "ascending";
                } else if ($this.hasClass('headerSortDown')) {
                    t = "descending";
                }
                $this.attr('title', $this.attr('data-title').replace(/\{dir\}/, t) );
            });
        };
    
    table.tablesorter();
    
    // bind to sort events 
    table.bind("sortEnd",function() { 
        updateTitles(); 
    });
    // set up titles
    updateTitles();
    

    这里是working demo

    【讨论】:

    • 您的示例效果很好,是否可以将所有这些添加到 tablesorter.js 内部的函数 setHeadersCss 中?看起来那是 tablesorter 添加和删除类的地方
    • 我不建议修改实际的插件代码,除非你可以维护它。如果在未来的某一天您决定更新插件,您需要记住您所做的更改以及进行所需的任何修改以使代码再次运行。
    • 当我使用过滤器小部件时,我只对一个简单的静态&lt;th data-title="foo"&gt;...&lt;/th&gt; 工作感兴趣。无论如何,它似乎没有出现在 2.10.8 版本中。有什么建议?谢谢。
    • @Leonid 如果您想要静态工具提示,请将data-title 更改为title。如果这不是你想要的,请打开一个新问题。
    • 我开了一个新问题,谢谢。 stackoverflow.com/questions/22262325/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-02
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多