【问题标题】:Sort table by background-color of last cell in row按行中最后一个单元格的背景颜色对表格进行排序
【发布时间】:2012-12-15 22:27:14
【问题描述】:

是否可以在页面加载时通过一行中最后一个单元格的 css 属性对表格进行排序?我希望表格行按最后一个单元格的 bg-color 排序。

表:

<table>
    <tr>
        <td>Foo</td>
        <td>Foo</td>
        <td style="font-weight:bold; background-color:#dee">Foo</td>
    </tr>
    <tr>
        <td>Foo</td>
        <td>Foo</td>
        <td style="background-color:#ffa">Foo</td>
    </tr>
    <tr>
        <td>put me on top</td>
        <td>Foo</td>
        <td style="background-color:#eee">Foo</td>
    </tr>
</table>

我不知道从哪里开始,但我至少可以提醒颜色为 RGB:

$("table tr").find("td:last").each(function(index) {
    alert($(this).css("background-color"));
});

测试:http://jsfiddle.net/AqTTJ/6/

【问题讨论】:

  • “排序”是什么意思?你需要更具体。你想检查一个元素是否有,比如说#eee,如果有,把它放在顶部?
  • 您要按十六进制排序吗?还是通过rgb() 组件?还是按颜色字母顺序?
  • 抱歉,如果不清楚。我希望表格行按最后一个单元格的 bg-color 排序。请参阅jsfiddle.net/AqTTJ/2 - 通过十六进制代码定义排序列表将是一流的,但谷歌表示这很复杂。
  • 我还可以添加一些 css 类。
  • 如果您添加类,那么您应该能够使用 getClass 并从那里进行排序。

标签: jquery sorting html-table


【解决方案1】:

以下代码按手动键或按背景颜色整数值对行进行排序。您可以在jsFiddle 上试用。

JavaScript

$(document).ready(function() {
  // Variable declarations:
  var tableId = '#table';
  var autoSort = true;
  var rowArr = getRows(tableId);
  var keySort = [];

  // Add a click handler to the sort button
  // that will call autoSortKeys() and pass
  // in either a manual key array or an
  // auto-sorted array.
  $('#sort').click(function() {
    autoSort = $('#autoSort').is(':checked');
    keySort = autoSort
        ? autoSortKeys(rowArr)
        : [0xffee77, 0xaaffaa, 0xff6666];
    sortTable(tableId, rowArr, keySort);
  });
});

// Sorts a table based on the array of
// keys that are passed into the function.
function sortTable(tableId, rows, keys) {
  $(tableId).empty();
  $.each(keys, function(indexK, valueK) {
    $.each(rows, function(indexR, valueR) {
      if (valueR[0] === valueK) {
        $(tableId).append(valueR[1]);
      }
    });
  });
}

// Converts an rgb() value returned from Jquery
// into an integer value for comparison.
function rgbToInt(color) {
  var digits = /(.*?)rgb\((\d+), (\d+), (\d+)\)/.exec(color);
  var red = parseInt(digits[2]);
  var green = parseInt(digits[3]);
  var blue = parseInt(digits[4]);
  return parseInt((blue | (green << 8) | (red << 16)), 10);
};

// Stores every row in a table designated table
// as a sub-array comprised of the background color
// integer value and the row DOM element.
function getRows(tableId) {
  var arr = new Array();
  $(tableId+' tr').each(function() {
    var bg = $(this).find('td:last-child').css('backgroundColor');
    var key = rgbToInt(bg);
    arr.push([key, $(this)]); // [bg-color, <tr />]
  });
  return arr;
}

// Automatically sorts rows based on their background
// color integer value from highest to lowest.
function autoSortKeys(arr) {
  var keys = new Array();
  $.each(arr, function(index, value) {
    keys.push(value[0]);
  });
  var uniqueKeys = new Array();
  $.each(keys, function(index, value){
    if($.inArray(value, uniqueKeys ) === -1)
      uniqueKeys.push(value);
  });
  delete keys; // garbage collect
  return uniqueKeys .sort().reverse();
}

HTML

<table id="table">
  <tr>
    <td>Foo</td>
    <td>Foo</td>
    <td style="font-weight:bold; background-color:#fe7">One</td>
  </tr>
  <tr>
    <td>Foo</td>
    <td>Foo</td>
    <td style="font-weight:bold; background-color:#fe7">One</td>
  </tr>
  <tr>
    <td>Foo</td>
    <td>Foo</td>
    <td style="background-color:#f66">Three</td>
  </tr>
  <tr>
    <td>Foo</td>
    <td>Foo</td>
    <td style="font-weight:bold; background-color:#fe7">One</td>
  </tr>
  <tr>
    <td>Foo</td>
    <td>Foo</td>
    <td style="background-color:#afa">Two</td>
  </tr>
  <tr>
    <td>Foo</td>
    <td>Foo</td>
    <td style="background-color:#f66">Three</td>
  </tr>
</table>
<br />
<label for="autoSort"> Auto Sort </label>
<input type="checkbox" id="autoSort" checked="CHECKED" />
<input type="button" id="sort" value="Sort" />

【讨论】:

  • 这很好,但我不明白如何定义自定义订单?
  • 你不能,除非你明确说明你希望行的顺序。你的帖子说你想按背景颜色排序。
  • jsfiddle.net/eWq5X/3 - #f66 行在顶部,#fe7 行在中间,#afa 行在底部
  • Here is a hacky way。我所做的只是将最后一行移到第一行之前...排序了吗?
  • 查看新答案 - 我将行映射更改为二维数组,因为由于颜色不唯一,映射键被覆盖。
猜你喜欢
  • 2019-03-28
  • 1970-01-01
  • 2016-11-13
  • 1970-01-01
  • 2020-01-05
  • 1970-01-01
  • 1970-01-01
  • 2021-07-08
  • 2016-11-21
相关资源
最近更新 更多