推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
lancegin

有没有比较了解 DataTable 的,数字按照字符串的类型进行排序很诡异啊

  •  
  •   lancegin · Jan 22, 2016 · 2918 views
    This topic created in 3777 days ago, the information mentioned may be changed or developed.

    官网的 api 也是看的太糟心了,不知道有没有遇到过类似问题的同学?怎么解决的?

    5 replies    2016-01-27 17:59:08 +08:00
    qihboy
        1
    qihboy  
       Jan 22, 2016
    数字的大小的比较或排序,是右“对齐”的方式。而字符串是左对齐,把数字变成字符串排序,得变成一样的长度 ,然后左边位不够的补 0
    lancegin
        2
    lancegin  
    OP
       Jan 22, 2016
    @qihboy 默认的只有字符串排序吗?数字排序都必须要转换成字符串排序?
    qihboy
        3
    qihboy  
       Jan 22, 2016   ❤️ 1
    http://datatables.net/plug-ins/sorting/
    可以指定排序类型,看第一个“ How to use ”下面的部分
    magicdawn
        4
    magicdawn  
       Jan 27, 2016   ❤️ 1
    可以先转成 Number, or 通过 render 动态去转 https://datatables.net/reference/option/columns.render

    col.render = function(cell, type, row){
    if(type === 'sort'){
    return Number(cell);
    }
    return cell;
    }
    lancegin
        5
    lancegin  
    OP
       Jan 27, 2016
    @magicdawn 谢谢,已经找到问题所在了,,,,, json 数据丢失了,导致本应该 float 的地方出现了 NaN 和 undefined ,就变成字符串的排序了。。醉了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3340 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 45ms · UTC 11:08 · PVG 19:08 · LAX 04:08 · JFK 07:08
    ♥ Do have faith in what you're doing.