Datatables排序varchar [英] Datatables sorting varchar

查看:226
本文介绍了Datatables排序varchar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用Datatables.net中的Datatables,一切都是除了排序问题外,工作很棒。我有一些属性在第一列,我想订购这样的:

  1 
1a
1b
2
3
4a
5
etc

然而,回来是这样的:

  1 
10
100
11
11a

我看过各种帖子,但似乎没有工作,我相信这应该是我应该触发的数据插件,但找不到任何东西。



可能有人建议?

解决方案

您的数据包含数字和字符,因此默认情况下它们将被排序为字符串。您应该编写自己的插件来排序数据类型。看看这里这里
看看如何编写一个插件,以及如何使用它与你的表。



编辑:今天有一些时间来处理datatable的东西。如果你仍然需要一个解决方案,你可以去:

  //排序插件
jQuery.extend(jQuery .fn.dataTableExt.oSort,{
//预处理
numchar-pre:function(str){
var patt = / ^([0-9] +)( [a-zA-Z] +)$ /; //匹配数据,如1a,2b,1ab,100k等
var matches = patt.exec($。trim(str));
var number = parseInt(matches [1]); //提取数字部分
var str = matches [2] .toLowerCase(); //提取字符部分,使其不区分大小写
var dec = 0;
for(i = 0; i {
dec + =(str.charCodeAt(i)-96)* Math.pow 26, - (i + 1)); //处理字符作为基数26
}
return number + dec; //组合两个部分
},

// sort ascending
numchar-asc:function(a,b){
return ab;
},

//排序降序
numchar-desc:func (a,b){
return b-a;
}
});

//自动类型检测插件
jQuery.fn.dataTableExt.aTypes.unshift(
函数(sData)
{
var patt = / ^([0-9] +)([a-zA-Z] +)$ /;
var trimmed = $ .trim(sData);
if(patt.test(trimmed)
{
return'numchar';
}
return null;
}
);

您可以使用自动类型检测功能来自动检测数据类型,也可以设置数据输入列



aoColumns:[{sType:numchar}]


I have a SQL query that is pulling back results ordered correctly when I try the query in MSSQL Studio.

I am using Datatables from Datatables.net and everything is working great apart from a sorting issue. I have some properties in the first column and I would like to order these like this:

1
1a
1b
2
3
4a
5
etc

However what comes back is something like this:

1
10
100
11
11a

I have looked though various posts but nothing seems to work and I believe that this must be something I should trigger from the datatables plugin but cannot find anything.

Could someone advise?

解决方案

Your data contain numbers and characters, so they will be sorted as string by default. You should write your own plugin for sorting your data type. Have a look at here and here to see how to write a plugin and how to use it with your table.

Edit: got some time today to work with the datatable stuff. If you still need a solution, here you go:

//Sorting plug-in
jQuery.extend( jQuery.fn.dataTableExt.oSort, {
    //pre-processing
    "numchar-pre": function(str){
        var patt = /^([0-9]+)([a-zA-Z]+)$/;    //match data like 1a, 2b, 1ab, 100k etc.
        var matches = patt.exec($.trim(str));
        var number = parseInt(matches[1]);     //extract the number part
        var str = matches[2].toLowerCase();    //extract the "character" part and make it case-insensitive
        var dec = 0;
        for (i=0; i<str.length; i++)
        {
         dec += (str.charCodeAt(i)-96)*Math.pow(26, -(i+1));  //deal with the character as a base-26 number
        }
        return number + dec;       //combine the two parts
    },

    //sort ascending
    "numchar-asc": function(a, b){
        return a-b;
    },

    //sort descending
    "numchar-desc": function(a, b){
        return b-a;
    }
});

//Automatic type detection plug-in  
jQuery.fn.dataTableExt.aTypes.unshift(
   function(sData)
   {
        var patt = /^([0-9]+)([a-zA-Z]+)$/;
        var trimmed = $.trim(sData);
        if (patt.test(trimmed))
        {
            return 'numchar';
        }
        return null;
    }
 );

You can use the automatic type detection function to let the data type automatically detected or you can set the data type for the column

"aoColumns": [{"sType": "numchar"}]

这篇关于Datatables排序varchar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆