당신은 너무 여러 번 클릭 핸들러를 추가 :
는
$('th')
.wrapInner('<span title="sort this column"/>')
.each(function(){
...
$('#accending_1,#accending_2').click(function(e){
이것이 할 것입니다 것은 모든 일에 대해, 그리고 4 개 번째 태그의 두 행이, 아이디 accending_1와 요소에 클릭 처리기를 추가 및 accending_2. 그러면 각 버튼에 8 개의 클릭 핸들러가 추가됩니다!
이 문제를 해결할 수있는 방법은 다양합니다. 대신 각 버튼의 사용 클래스에 대한 특정 아이디의를 가진 헤더에 그들을 상대 찾기의 :
$('th')
.wrapInner('<span title="sort this column"/>')
.each(function(){
$('.accending', this).click(
example
참고 현재 TH의 후손으로 선택을 제한하는 마지막 줄에 this
매개 변수입니다. 그것은 여전히 TH의 최상위 행에 대한 검색을 수행 할 것입니다.
이. 그냥 버튼을 직접 검색하고 자신들이 무엇을 열 해결 아마 더 나은
$('.accending, .accending')
.wrapInner('<span title="sort this column"/>')
.click(function(e){
console.log("click");
var th = $(this).closest('th'),
thIndex = th.index();
table.find('td')
.filter(function(){
return $(this).index() === thIndex;
})
.sort(
function(a, b){
return $.text([a]) > $.text([b]);
}, function(){
// parentNode is the element we want to move
return this.parentNode;
}
);
});
$('.decending, .decending')
example
상기 코드의 중복을 많이하고, HTML은 여전히있다.
클릭 핸들러는 거의 동일하므로 정렬 기능 만 전달하면됩니다.
//This function returns another function which is a click handler.
//It takes the sort function as a parameter.
function createClickHandler(sortFunction){
return function(e){
var th = $(e.target).closest('th'),
thIndex = th.index();
console.log(th);
table.find('td')
.filter(function(){
return $(this).index() === thIndex;
})
.sort(sortFunction, function(){
// parentNode is the element we want to move
return this.parentNode;
}
);
};
}
$('.accending, .accending')
.wrapInner('<span title="sort this column"/>')
.click(createClickHandler(function(a, b){
return $.text([a]) > $.text([b]);
})
);
$('.decending, .decending')
.wrapInner('<span title="sort this column"/>')
.click(createClickHandler(function(a, b){
return $.text([a]) < $.text([b]);
})
);
example
html로
도 조금 정리 될 수있다. 버튼의 태그는 모두 동일하므로 자바 스크립트에서 삽입 핸들러를 검색 할 필요없이 직접 삽입 할 수 있습니다. 다시 열 머리글을 반복하므로 열 번호를 얻는 방법을 정리할 수 있습니다. 그리고 마지막으로 두 개의 다른 정렬 함수를 전달하는 것은 약간 낭비이므로 방향 매개 변수를 대신 전달해 보겠습니다. I는 역 변수를 제거
example
참고
//This function returns another function which is a click handler.
//It takes the sort function as a parameter.
function createClickHandler(column, isAccending){
return function(e){
table.find('td')
.filter(function(){
return $(this).index() === column;
})
.sort(function(a, b){
var order = $.text([a]) > $.text([b]);
return isAccending ? order : !order;
}, function(){
// parentNode is the element we want to move
return this.parentNode;
})
;
};
}
$('#controls th').each(function(column,item){
//Note we get the column index for for free with the each function
$(this)
.append($('<a title="sort this column" href="#">Ascending</a>')
.click(createClickHandler(column, true))
)
.append(' ')
.append($('<a title="sort this column" href="#">Decending</a>')
.click(createClickHandler(column, false))
)
;
});
. 그것은 결코 사용되지 않았습니다.
return $.text([a]) > $.text([b])
inverse ? -1 : 1
;
나는이 일을했지만 실제로 인해 automatic semicolon insertion에 첫 번째 줄에 반환의 사상 모르겠어요. 다음과 같이 해석됩니다.
return $.text([a]) > $.text([b]);
inverse ? -1 : 1;
그래서 역수는 데드 코드입니다. 그것은 자바 스크립트의 나쁜 비트 중 하나이며 아주 분명하지 않습니다. jsbin이 세미콜론 누락에 대해 경고했습니다. 여기에 코드를 제출하기 전에 항상 오류/경고를 수정해야합니다.
휠을 다시 발명하는 이유는 무엇입니까? 시도해보십시오 http://datatables.net –
그것의 특정 요구 사항 –
그 플러그인을 사용해야합니까? 전에 http://tablesorter.com/에서 플러그인을 사용했는데 잘 작동합니다. –