简体   繁体   English

数据表按参数排序jQuery

[英]Datatable sorting jQuery by parameters

Yo guys, im working on some project that contains datatable, 大家好,我正在处理一些包含数据表的项目,

The datatable working by ajax that returns json and inserting all data to the table for example : 由ajax工作的datatable返回json并将所有数据插入到表中,例如:

ID   NAME     ROLE     CITY         ADDRESS
----------------------------------------------------------
 2   ALBERT   ADMIN    Albany       CenterStreet
 3   AMBER    USER     BurrRidge    AintreeLane
 4   NICOLE   ADMIN    BurrRidge    AintreeLane
 5   MARY     USER     Albany       Broadway
 6   SHELLY   USER     Albany       CenterStreet

I'm trying to sort the table so when I click a button it will toggle the sort for all admins to the first rows, so it will show albert and nicole first but if press one more button that called "Albany" it will show ALBERT in the first row cause he had those to parameters, but when i click to toggle CenterStreet albert will be in first and shelly after him cause she is in Albany and CenterStreet even she is not an admin. 我正在尝试对表格进行排序,因此当我单击一个按钮时,它将把所有管理员的排序切换到第一行,因此它将首先显示albert和nicole,但是如果再按下一个名为“ Albany”的按钮,它将显示ALBERT在第一行中,因为他拥有这些参数,但是当我单击以切换CenterStreet时,阿尔伯特将居于首位,之后因为他在奥尔巴尼和CenterStreet中,因此她不是管理员。

I've tried some ways of doing this, but unfortunately no success, does any one know how to do this kind of thing with datatables on jQuery? 我已经尝试了一些方法来执行此操作,但不幸的是没有成功,有人知道如何使用jQuery的数据表来执行这种操作吗?

Use this function to sort the jsonobject. 使用此函数对jsonobject进行排序。

var ja = [{id:1,name:XYZ},{id:2,name:ABC}];
var asc = true;
var prop = 'id';
sort(ja,prop,asc);


function sort(properties, prop, asc) {
        properties = properties.sort(function (a, b) {
            if (asc) {
                return (a[prop] > b[prop]) ? 1 : ((a[prop] < b[prop]) ? -1 : 0);
            } else {
                return (b[prop] > a[prop]) ? 1 : ((b[prop] < a[prop]) ? -1 : 0);
            }
        });
        return properties;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM