Jquery数据表数字排序


Jquery datatable numeric sort

我使用jquery数据表对以下数据进行表排序。

        <table width="50%" border="0" id="pls-pitching">
    <tr>
    <td><span>1</span>
    <input name="t" type="text" value="1" /></td>
    </tr>
    <tr>
    <td><span>5</span>
    <input name="t" type="text" value="5" /></td>
    </tr>
    <tr>
    <td><span>2</span>
    <input name="t" type="text" value="2" /></td>
    </tr>
    <tr>
    <td><span>3</span>
    <input name="t" type="text" value="3" /></td>
    </tr>
    <tr>
    <td><span>10</span>
    <input name="t" type="text" value="10" /></td>
    </tr>
    </table>

在这里,我想将文本框值排序为1,2,3,5,10我无法实现文本框值排序。所以我在文本框之前添加了<span>标签,比如(<span>10</span>)。现在它对值进行排序。但它有点像1,10,2,3,5

但我需要像数字排序一样排序。

我使用了以下javascript命令

$('#pls-pitching').dataTable();

是否有可能实现文本框值排序?请做必要的事。感谢

您必须编写自己的函数来进行比较。以下是如何做到这一点的示例

我遇到了同样的问题,就是这样

1.定义自定义排序类型(对于数值)

jQuery.fn.dataTableExt.oSort['num-html-asc'] = function(a,b) {
    var x = a.replace( /<.*?>/g, "" );
    var y = b.replace( /<.*?>/g, "" );
    x = parseInt( x );
    y = parseInt( y );
    return ((x < y || isNaN(y) ) ? -1 : ((x > y || isNaN(x)) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['num-html-desc'] = function(a,b) {
    var x = a.replace( /<.*?>/g, "" );
    var y = b.replace( /<.*?>/g, "" );
    x = parseInt( x );
    y = parseInt( y );
    return ((x < y || isNaN(x)) ? 1 : ((x > y || isNaN(y) ) ? -1 : 0));
};

2.初始化数据表时设置排序类型,

 $(".fooTable").dataTable({
    "aoColumns": [
         {"sType" : "num-html" }]
 });