admin管理员组文章数量:1414628
I am using datatables version 1.10. I have a requirement where
- when clicked on sort icons (up and down arrows) sorting should work server side
- when clicked on th , sorting should work locally.This is because user clicks on thead by mistake and servers gets unwanted burden.
Objective: I wanted to keep both
- local sorting [clicking thead not icons](only for data currently in table display/page)
- server side sorting [only by clicking icons].
THE PROBLEM: DataTables uses css background-image for sorting. This makes it difficult to detect click on icons since css background-image can not be captured on clicks as per my knowledge.
This is what i have e up with but still no progress
JSFIDDLE: /
JS:
$(document).ready(function () {
///
var url = "";
//
$('th').on("click.DT", function (event) {
event.stopImmediatePropagation();
});
var table = $('#example').DataTable({
"processing": true,
"serverSide": false,
"ajax": url
});
//
//$('th').off('click.DT');
// sort internally
table.column('2').order('asc');
});
UPDATE-1:
JSFIDDLE: /
I was able to go one step ahead by adding a sortMask and then bind a click on it.
sortMask is a div which id added to each column <thead> -> <tr> -> <th>
CSS:
.sortMask {
width:19px;
height:19px;
float:right;
display:inline-block;
z-index:0;
margin-right: -19px;
/*background:red;*/
}
JS:
$('th').on("click.DT", function (e) {
//stop Propagation if clciked outsidemask
//becasue we want to sort locally here
if (!$(e.target).hasClass('sortMask')) {
e.stopImmediatePropagation();
}
});
I am using datatables version 1.10. I have a requirement where
- when clicked on sort icons (up and down arrows) sorting should work server side
- when clicked on th , sorting should work locally.This is because user clicks on thead by mistake and servers gets unwanted burden.
Objective: I wanted to keep both
- local sorting [clicking thead not icons](only for data currently in table display/page)
- server side sorting [only by clicking icons].
THE PROBLEM: DataTables uses css background-image for sorting. This makes it difficult to detect click on icons since css background-image can not be captured on clicks as per my knowledge.
This is what i have e up with but still no progress
JSFIDDLE: http://jsfiddle/bababalcksheep/mae29pau/10/
JS:
$(document).ready(function () {
//http://www.datatables/reference/api/
var url = "http://www.json-generator./api/json/get/cbEfqLwFaq?indent=2";
//
$('th').on("click.DT", function (event) {
event.stopImmediatePropagation();
});
var table = $('#example').DataTable({
"processing": true,
"serverSide": false,
"ajax": url
});
//
//$('th').off('click.DT');
// sort internally
table.column('2').order('asc');
});
UPDATE-1:
JSFIDDLE: http://jsfiddle/bababalcksheep/mae29pau/14/
I was able to go one step ahead by adding a sortMask and then bind a click on it.
sortMask is a div which id added to each column <thead> -> <tr> -> <th>
CSS:
.sortMask {
width:19px;
height:19px;
float:right;
display:inline-block;
z-index:0;
margin-right: -19px;
/*background:red;*/
}
JS:
$('th').on("click.DT", function (e) {
//stop Propagation if clciked outsidemask
//becasue we want to sort locally here
if (!$(e.target).hasClass('sortMask')) {
e.stopImmediatePropagation();
}
});
Share
Improve this question
edited Aug 3, 2015 at 7:32
Daniil Ryzhkov
7,6064 gold badges44 silver badges59 bronze badges
asked Dec 5, 2014 at 14:33
djangodjango
2,9196 gold badges51 silver badges83 bronze badges
4
- BTW try to make the client make those kind of things, because if you have several users clicking on sort the server will have an unnecessary load. – Alberto Bonsanto Commented Dec 5, 2014 at 14:46
- can you insert icon in <th> like <th> <i class="sendToServer fa fa-lock pull-right"></i> </th> and then on click of icon ,send it to server and then $('.sendToServer').on('click',function(e){ // server side coding ; e.stopPropogation(); }) and anywhere on thead except icon if he clicks it will get sorted normally – Alok Commented Dec 5, 2014 at 14:46
- clicking on a table heading to sort is normal expected user behavior and the most mon user interface. Doesn't make sense that they do it by accident – charlietfl Commented Dec 5, 2014 at 14:48
- consider I want both options, there is a server side sorting by clicking icons only, and local sorting (is done by click thead (not Icons)).Local sorting is done only for records that are currently in display. That is my objective to keep them both separate. – django Commented Dec 5, 2014 at 17:44
1 Answer
Reset to default 4I think the better solution here is to wrap the text not the icon:
<th><div>First name <div>abc</div></div></th>
And:
$('th').on("click", function (event) {
if($(event.target).is("div"))
event.stopImmediatePropagation();
});
Please note: you should use normal click
even instead of click.DT
because in this case it will be fired before DataTables
one.
http://jsfiddle/mae29pau/38/
本文标签: javascriptDatatables 110 sort only by cliking sort icons in thStack Overflow
版权声明:本文标题:javascript - Datatables 1.10 sort only by cliking sort icons in th - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745159985a2645395.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论