0

I am using stupidtable (http://joequery.github.io/Stupid-Table-Plugin/) to add sorting to a table. I use a callback to add an appropriate up or down arrow to show which column has been used for the sort and whether it is ascending or descending.

The original event handler used the code:

table.bind('aftertablesort', function (event, data) {

    // data.column - the index of the column sorted after a click
    // data.direction - the sorting direction (either asc or desc)

    var th = $(this).find("th");
    th.find(".arrow").remove();
    var arrow = data.direction === "asc" ? "↑" : "↓";
    th.eq(data.column).append('<span class="arrow">' + arrow +'</span>');
});

This works fine when there is only one row of headers, but if there are multiple rows it doesn't because it finds the first header row not the last.

I'm struggling to find out how to find the bottom row, following another thread I tried:

var th = $(this + ' thead tr:last').find("th");

But this gives a syntax error, I guess because I am not using 'this' properly. Can anyone help?

Thanks,

Steph

4

4 回答 4

2

您不能将对象与字符串组合来构建选择器。尝试这个:

var th = $(this).find('thead tr:last').find("th");
于 2013-06-03T17:30:06.053 回答
1

this不是字符串,而是 DOM 元素。

您可以将其用作上下文选择器,如下所示:

var th = $('thead tr:last', this).find("th");

但是.find比上下文快,所以使用这个:

var th = $(this).find("thead tr:last th");
于 2013-06-03T17:30:43.283 回答
0

如果this指的是你的桌子,你应该使用find

var th = $(this).find('thead tr:last')

以下行

$(this + ' thead tr:last')

不起作用,因为this是一个对象,而你正在与一个字符串混合,这将导致

$("[object HTMLTableElement] thead tr:last")
于 2013-06-03T17:29:47.737 回答
0

您确实知道这thead是针对表格的标题部分的。所以当问“底行”时,你确定它是你想要的标题还是正文?

this如果是父项,则查找最后一个标题行。

var last_header_row = $(this).find("thead").last("tr");

this如果是父项,则查找正文中的最后一行。

var last_body_row = $(this).find("tbody").last("tr");

请注意,该this引用位于事件回调函数的上下文中。在不知道上下文是什么的情况下,我们不能保证它是一个 DOM 元素引用。

如果您有来自表内部的参考,thisatr或某物在哪里。将调用替换find(..)closest(..)

于 2013-06-03T17:38:09.843 回答