1

My scenario:

I am trying to sort date column of the table which uses DataTable jquery plugin and I have sorted the table but with the text fields having white-spaces. Hence, when I sort "ASC" order, then the empty text fields will occupy at first. This should not happen. I want to sort the table excluding the empty text boxes.

I tried the following code:

jQuery.extend(jQuery.fn.dataTableExt.oSort, {
"customdatesort-pre": function (formElement) {
    // returns the "weight" of a cell value
    var r, x;
    var a = $(formElement).val();

    if (a === null || a === "") {
        // for empty cells: weight is a "special" value which needs special handling
        r = false;
    } else {
        // otherwise: weight is the "time value" of the date
        x = a.split("/");
        r = +new Date(+x[2], +x[1] - 1, +x[0]);
    }
    //console.log("[PRECALC] " + a + " becomes " + r);
    return r;
},
"customdatesort-asc": function (a, b) {
    // return values are explained in Array.prototype.sort documentation
    if (a === false && b === false) {
        // if both are empty cells then order does not matter
        return 0;
    } else if (a === false) {
        // if a is an empty cell then consider a greater than b
        return 1;
    } else if (b === false) {
        // if b is an empty cell then consider a less than b
        return -1;
    } else {
        // common sense
        return a - b;
    }
},
"customdatesort-desc": function (a, b) {
    if (a === false && b === false) {
        return 0;
    } else if (a === false) {
        return 1;
    } else if (b === false) {
        return -1;
    } else {
        return b - a;
    }
}
});

Problem I found with this code:

In the above code, you can see the "customdatesort-pre": function (formElement) . The parameter formElement is taking only the empty values and not the textboxes with some date values.

What I need:

I need to sort the date columns excluding the empty textboxes.

0

1 Answer 1

1

With custom sorting pre obsoletes asc and desc. If you have defined a pre then the asc and desc methods will never be called. pre is meant as an optimisation feature, it is called once for each cell, and then the internal sorter will use that result for sorting. See this thread on datatables.net.

Instead you could place the pre function code outside the custom sort literal and call it from within the asc and desc methods :

customdatesortPre = function (formElement) {
  //customdatesort-pre code
}
jQuery.extend(jQuery.fn.dataTableExt.oSort, {
  "customdatesort-asc": function (a, b) {
    a = customdatesortPre(a), b = customdatesortPre(b);
    ...
  },
  "customdatesort-desc": function (a, b) {
    a = customdatesortPre(a), b = customdatesortPre(b);
    ...
  }
});

demo -> http://jsfiddle.net/9j9gpsrn/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.