2013-05-30 2 views
0

숨바꼭질들, 내가 열을 자연 정렬이 코드를 사용JQuery와 DataTables 종류의 숫자 사업부의 콘텐츠

:

-2 
-1 
0 
1 
2 



    function naturalSort(a, b) { 
    var re = /(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$|^0x[0-9a-f]+$|[0-9]+)/gi, 
     sre = /(^[ ]*|[ ]*$)/g, 
     dre = /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/, 
     hre = /^0x[0-9a-f]+$/i, 
     ore = /^0/, 
    // convert all to strings and trim() 
     x = a.toString().replace(sre, '') || '', 
     y = b.toString().replace(sre, '') || '', 
    // chunk/tokenize 
     xN = x.replace(re, '\0$1\0').replace(/\0$/, '').replace(/^\0/, '').split('\0'), 
     yN = y.replace(re, '\0$1\0').replace(/\0$/, '').replace(/^\0/, '').split('\0'), 
    // numeric, hex or date detection 
     xD = parseInt(x.match(hre)) || (xN.length != 1 && x.match(dre) && Date.parse(x)), 
     yD = parseInt(y.match(hre)) || xD && y.match(dre) && Date.parse(y) || null; 
    // first try and sort Hex codes or Dates 
    if (yD) if (xD < yD) return -1; 
    else if (xD > yD) return 1; 
    // natural sorting through split numeric strings and default strings 
    for (var cLoc = 0, numS = Math.max(xN.length, yN.length); cLoc < numS; cLoc++) { 
     // find floats not starting with '0', string or 0 if not defined (Clint Priest) 
     oFxNcL = !(xN[cLoc] || '').match(ore) && parseFloat(xN[cLoc]) || xN[cLoc] || 0; 
     oFyNcL = !(yN[cLoc] || '').match(ore) && parseFloat(yN[cLoc]) || yN[cLoc] || 0; 
     // handle numeric vs string comparison - number < string - (Kyle Adams) 
     if (isNaN(oFxNcL) !== isNaN(oFyNcL)) return (isNaN(oFxNcL)) ? 1 : -1; 
     // rely on string comparison if different types - i.e. '02' < 2 != '02' < '2' 
     else if (typeof oFxNcL !== typeof oFyNcL) { 
      oFxNcL += ''; 
      oFyNcL += ''; 
     } 
     if (oFxNcL < oFyNcL) return -1; 
     if (oFxNcL > oFyNcL) return 1; 
    } 
    return 0; 
}; 
// Natural Sorting 
jQuery.fn.dataTableExt.oSort['natural-asc'] = function (a, b) { 
    return naturalSort(a, b); 
}; 
jQuery.fn.dataTableExt.oSort['natural-desc'] = function (a, b) { 
    return naturalSort(a, b) * -1; 
}; 

그것은 잘 작동하지만 나는 같은 제목을 가진 사업부를 넣어 경우 콘텐츠가 숫자 이전에 올바르게 작동하지 않습니다.

<div title='juhu'>-1</div > 
<div title='juhu'>-2</div > 
<div title='juhu'>0</div > 
<div title='juhu'>1</div > 
<div title='juhu'>2</div > 

누구에게이 문제의 해결책이 있습니까?

+0

자바 스크립트는 div를 삭제하고 숫자 만 선택해야합니다 ... – Butters

답변

0

Javascript에서 console.log를 사용하는 것이 도움이됩니다. xN 및 yN 번호를 기록하면 양수가 <div title="juhu">,1,</div>처럼 보이지만 음수는 <div title="juhu">-,2,</div>처럼 보입니다.

토큰 화 된 문자열을 반복하면서 양수와 음수를 비교할 때 첫 번째 비교에서는 <div title="juhu"><div title="juhu">-을 비교합니다. 순수한 ASCII로 비교할 것이므로 "asciibetical"순서에서 <div title="juhu"><div title="juhu">-보다 작습니다.

function naturalSort(a, b) { 
a = typeof a.replace == 'function' ? 
     a.replace(/<[\s\S]*?>/g, "") : a; 
    b = typeof a.replace == 'function' ? 
     b.replace(/<[\s\S]*?>/g, "") : b; 

HTML 태그를 제거 것이다, 당신의 문제를 해결해야합니다

은 naturalSort 기능의 상단에이를 추가합니다.

DataTables이 우수하고 자주 내장 필요가 무엇을해야합니다이 페이지에서 봐, 그것은 도움이 될 수 있습니다. http://www.datatables.net/plug-ins/type-detection

내가 위에서 사용 된 코드는 섹션 "HTML과 숫자"에서왔다.