File: jquery.dataTables.sorting.ipAddress.js

package info (click to toggle)
ruby-jquery-datatables-rails 3.4.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,292 kB
  • sloc: javascript: 16,004; ruby: 86; makefile: 2
file content (44 lines) | stat: -rw-r--r-- 1,073 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
 * Sorts a column containing IP addresses in typical dot notation. This can
 * be most useful when using DataTables for a networking application, and
 * reporting information containing IP address. Also has a matching type
 * detection plug-in for automatic type detection.
 *
 *  @name IP addresses
 *  @summary Sort IP addresses numerically
 *  @author Brad Wasson
 *
 *  @example
 *    $('#example').dataTable( {
 *       columnDefs: [
 *         { type: 'ip-address', targets: 0 }
 *       ]
 *    } );
 */

jQuery.extend( jQuery.fn.dataTableExt.oSort, {
  "ip-address-pre": function ( a ) {
    var m = a.split("."), x = "";

    for(var i = 0; i < m.length; i++) {
      var item = m[i];
      if(item.length == 1) {
        x += "00" + item;
      } else if(item.length == 2) {
        x += "0" + item;
      } else {
        x += item;
      }
    }

    return x;
  },

  "ip-address-asc": function ( a, b ) {
    return ((a < b) ? -1 : ((a > b) ? 1 : 0));
  },

  "ip-address-desc": function ( a, b ) {
    return ((a < b) ? 1 : ((a > b) ? -1 : 0));
  }
} );