单击另一列时更改标题的 JavaScript

发布于 2024-12-29 05:24:28 字数 1769 浏览 1 评论 0原文

我有一个表格,在单击另一列中的标题后,我想重置任何其他列以表示排序依据。目前我有:

<html>
<head>
<script>
 function changeSort(obj)
 {  

  var firstName = document.getElementById('fName');
 var lastName = document.getElementById('lName');
 var phoneNumber = document.getElementById('phone');
var birthDate = document.getElementById('birth');


if(obj.id == 'fName')
{
//alert("fName");
 obj.title = (obj.title== 'sort ascending first name') ? 'sort descending first 
 name' : 'sort         ascending first name';   
 }

if(obj.id == 'lName')
{
 //alert("lName");
 obj.title = (obj.title== 'sort ascending last name') ? 'sort descending first name' : 'sort   
ascending last name';   
 }

 if(obj.id == 'phone')
  {
  //alert("phone");
  obj.title = (obj.title== 'sort ascending phone number') ? 'sort descending phone  
  number' : 'sort ascending phone number';   
  }

 if(obj.id == 'birth')
 {
  //alert("birth");
   obj.title = (obj.title== 'sort ascending birth date') ? 'sort descending birth      
   date' : 'sort   
  ascending birth date';   
   }   

   }

  </script>
 </head>
 <body

     <table> 

  <tr>
            <th id="fName" onclick='changeSort(this)' title="sort by First Name" >First Name</th>
    <th id="lName" onclick='changeSort(this)' title="sort by Last Name" >Last   
    Name</th>
    <th id="phone" onclick='changeSort(this)' title="sort by Phone Number" >Phone   
   Number</th>
    <th id="birth" onclick='changeSort(this)'title="sort by Birth Date" >Birth   
    Date</th>
    </tr>

   </table>

  </body>
  </html>

当我对第一列执行排序时,我希望它将标题更改为当前的升序或降序,但如果我决定按姓氏排序,我希望更改名字列标题返回按名字排序,而不是升序或降序。当点击另一个表头时,如何使更改后的表头的标题恢复为默认值?

I have a table and after a heading in another column is clicked I would like to reset any other columns to say sort by. Currently I have:

<html>
<head>
<script>
 function changeSort(obj)
 {  

  var firstName = document.getElementById('fName');
 var lastName = document.getElementById('lName');
 var phoneNumber = document.getElementById('phone');
var birthDate = document.getElementById('birth');


if(obj.id == 'fName')
{
//alert("fName");
 obj.title = (obj.title== 'sort ascending first name') ? 'sort descending first 
 name' : 'sort         ascending first name';   
 }

if(obj.id == 'lName')
{
 //alert("lName");
 obj.title = (obj.title== 'sort ascending last name') ? 'sort descending first name' : 'sort   
ascending last name';   
 }

 if(obj.id == 'phone')
  {
  //alert("phone");
  obj.title = (obj.title== 'sort ascending phone number') ? 'sort descending phone  
  number' : 'sort ascending phone number';   
  }

 if(obj.id == 'birth')
 {
  //alert("birth");
   obj.title = (obj.title== 'sort ascending birth date') ? 'sort descending birth      
   date' : 'sort   
  ascending birth date';   
   }   

   }

  </script>
 </head>
 <body

     <table> 

  <tr>
            <th id="fName" onclick='changeSort(this)' title="sort by First Name" >First Name</th>
    <th id="lName" onclick='changeSort(this)' title="sort by Last Name" >Last   
    Name</th>
    <th id="phone" onclick='changeSort(this)' title="sort by Phone Number" >Phone   
   Number</th>
    <th id="birth" onclick='changeSort(this)'title="sort by Birth Date" >Birth   
    Date</th>
    </tr>

   </table>

  </body>
  </html>

When I perform a sort on the first column i would like it to change the title to ascending or descending which it currently does but if I decide I would like to sort by last name I want the first name columns title to change back to sort by first name and to not be ascending or descending. How can I get the title of a changed table head to go back to the default when another table head is clciked?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一抹苦笑 2025-01-05 05:24:28

考虑到安德烈的观点,即有更简洁的方法,下面是重构的代码,使其独立于列数,并且更通用。

function changeSort(th){
    var titleTemplates = {
        asc:  'sort ascending %s',
        desc: 'sort decending %s',
        dflt: 'sort by %s'
    };
    titleTemplates.get = function(key, txt){
        var t = this[key] || this.dflt;
        return t.replace('%s', txt);
    };
    var headerRow = th.parentNode;
    var cells = headerRow.getElementsByTagName('th');
    for(var i=0; i<cells.length; i++){
        var c = cells[i];
        var s = c.getAttribute('sortCode');
        var s_ = (c !== th) ? '' : (!s || s == 'desc') ? 'asc' : 'desc';
        c.setAttribute('sortCode', s_);
        c.title = titleTemplates.get(s_, c.innerHTML);
    }
}

在 Opera 11.61 中测试

您可以(而且确实应该)进一步将点击处理程序附加到 javascript 中的 TH 元素而不是 HTML 中。

Picking up on André's point that there are cleaner ways, here's the code refactored to make it independent of the number of columns, and otherwise more general.

function changeSort(th){
    var titleTemplates = {
        asc:  'sort ascending %s',
        desc: 'sort decending %s',
        dflt: 'sort by %s'
    };
    titleTemplates.get = function(key, txt){
        var t = this[key] || this.dflt;
        return t.replace('%s', txt);
    };
    var headerRow = th.parentNode;
    var cells = headerRow.getElementsByTagName('th');
    for(var i=0; i<cells.length; i++){
        var c = cells[i];
        var s = c.getAttribute('sortCode');
        var s_ = (c !== th) ? '' : (!s || s == 'desc') ? 'asc' : 'desc';
        c.setAttribute('sortCode', s_);
        c.title = titleTemplates.get(s_, c.innerHTML);
    }
}

tested in Opera 11.61

You could (and really should) go further by attaching the click handler to the TH elements in javascript rather than in the HTML.

快乐很简单 2025-01-05 05:24:28

在函数的末尾你应该添加:

firstName.title = obj.id == 'fName'? firstName.title : 'sort by first name';
lastName.title = obj.id == 'lName'? lastName.title : 'sort by last name';
phoneNumber.title = obj.id == 'phone'? phoneNumber.title : 'sort by phone number';
birthDate.title = obj.id == 'birth'? birthDate.title : 'sort by birth date';

会有更干净的方法来做到这一点,但我们必须重构整个代码......

in the end of the function you should add:

firstName.title = obj.id == 'fName'? firstName.title : 'sort by first name';
lastName.title = obj.id == 'lName'? lastName.title : 'sort by last name';
phoneNumber.title = obj.id == 'phone'? phoneNumber.title : 'sort by phone number';
birthDate.title = obj.id == 'birth'? birthDate.title : 'sort by birth date';

There would be cleaner ways to do this, but we had to refactor the whole code...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文