如何在我的HTML表格中添加排序符号?

huangapple go评论90阅读模式
英文:

How to add sorting symbols in my HTML table?

问题

You can add sorting symbols to your table header by modifying your HTML and JavaScript code as follows:

  1. In your HTML, you can add placeholders for sorting symbols in your table header cells (th). For example, you can use some Unicode characters like ▲ for ascending and ▼ for descending.
<th onclick="onColumnHeaderClicked(event)" scope="col">Klant ▲</th>
<th scope="col">Adres</th>
<th onclick="onColumnHeaderClicked(event)" scope="col"># panelen ▼</th>
  1. Modify your onColumnHeaderClicked function to update the sorting symbols based on the sorting direction (ascending or descending). You can use JavaScript to change the text content of the clicked header cell accordingly.

Here's a modified version of your function:

function onColumnHeaderClicked(ev) {
    const th = ev.currentTarget;
    const table = th.closest('table');
    const thIndex = Array.from(th.parentElement.children).indexOf(th);

    const ascending = !('sort' in th.dataset) || th.dataset.sort != 'asc';

    // Remove sorting symbols from all header cells
    const allTh = table.querySelectorAll(':scope > thead > tr > th');
    for (let th2 of allTh) {
        th2.textContent = th2.textContent.replace(' ▲', '').replace(' ▼', '');
        delete th2.dataset['sort'];
    }

    // Add sorting symbols to the clicked header cell
    th.textContent = th.textContent + (ascending ? ' ▲' : ' ▼');
    th.dataset['sort'] = ascending ? 'asc' : 'desc';

    sortTableRowsByColumn(table, thIndex, ascending);
}

With these changes, your table header cells will display sorting symbols (▲ for ascending and ▼ for descending) when clicked, providing a visual indication of the sorting direction.

英文:

I am trying to add sorting symbols to my HTML table.
Below is my Javascript code.

function sortTableRowsByColumn( table, columnIndex, ascending ) {

    const rows = Array.from( table.querySelectorAll( &#39;:scope &gt; tbody &gt; tr&#39; ) );
    
    rows.sort( ( x, y ) =&gt; {
    
        const xValue = x.cells[columnIndex].textContent;
        const yValue = y.cells[columnIndex].textContent;
        
        const xNum = parseFloat( xValue );
        const yNum = parseFloat( yValue );

        return ascending ? ( xNum - yNum ) : ( yNum - xNum );
    } );

    for( let row of rows ) {
        table.tBodies[0].appendChild( row );
    }
}

function onColumnHeaderClicked( ev ) {
    
    const th = ev.currentTarget;
    const table = th.closest( &#39;table&#39; );
    const thIndex = Array.from( th.parentElement.children ).indexOf( th );

    const ascending = !( &#39;sort&#39; in th.dataset ) || th.dataset.sort != &#39;asc&#39;;
    
    sortTableRowsByColumn( table, thIndex, ascending );

    const allTh = table.querySelectorAll( &#39;:scope &gt; thead &gt; tr &gt; th&#39; );
    for( let th2 of allTh ) {
        delete th2.dataset[&#39;sort&#39;];
    }
 
    th.dataset[&#39;sort&#39;] = ascending ? &#39;asc&#39; : &#39;desc&#39;;
}

Here is my html for table.

  &lt;table id=&quot;table&quot; class=&quot;table table-striped table-bordered table-sm&quot; style=&quot;font-size: 50%;&quot;&gt;
    &lt;thead&gt;
      &lt;tr&gt;
        &lt;th onclick=&quot;onColumnHeaderClicked(event)&quot; scope=&quot;col&quot;&gt;Klant  &lt;/th&gt;
        &lt;th scope=&quot;col&quot;&gt;Adres&lt;/th&gt;
        &lt;th onclick=&quot;onColumnHeaderClicked(event)&quot; scope=&quot;col&quot;&gt;# panelen&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;

My sorting works completely fine. However, to make it more intuitive, i want to add sorting symbols to my header. Can some one suggest a way to do it?

答案1

得分: 1

以下是翻译好的部分:

如评论中建议的,您可以使用图标和字体。还可以使用UTF-8字符集来添加字符到您的列标题。以下两个对此很有用:

  • &#x25b4;,将显示为 ▴
  • &#x25be;,将显示为 ▾

根据列是否未排序、升序排序或降序排序,您可以显示以下三种变体之一:

  1. <tr>客户 &#x25b4;&#x25be;</tr>,将显示为 客户 ▴▾
  2. <tr>客户 &#x25b4;</tr>,将显示为 客户 ▴
  3. <tr>客户 &#x25be;</tr>,将显示为 客户 ▾
英文:

As suggested in the comments you can use icons and fonts. It is also possible to use the UTF-8 character set to add a character to your column heading. The following two are useful for this purpose:

  • &amp;#x25b4; which will display as &#x25b4;
  • &amp;#x25be; which will display as &#x25be;

Depending on whether the column is unsorted, sorted ascending or sorted descending you display one of the following three variants:

  1. &lt;tr&gt;Klant &amp;#x25b4;&amp;#x25be;&lt;/tr&gt; which will look like Klant &#x25b4;&#x25be;
  2. &lt;tr&gt;Klant &amp;#x25b4&lt;/tr&gt; which will look like Klant &#x25b4;
  3. &lt;tr&gt;Klant &amp;#x25be;&lt;/tr&gt; which will look like Klant &#x25be;

huangapple
  • 本文由 发表于 2020年1月6日 23:27:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614728.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定