1

I am using free-jqgrid 4.15.4 for showing the data. There i have a need to search date column with Date less but not empty filter, but it is not filtering correctly. In result it gives me date which are greater than searched date.

Below code is used for custom filter in date column:

customSortOperations: {

dlne: {
                operand: "<!=''",
                text: "Date less but not empty",
                filter:function (options) {
                    var p = this.p, iCol = p.iColByName[options.cmName], cm = p.colModel[iCol],
                        newformat = cm.formatoptions != null && cm.formatoptions.newformat ?
                                cm.formatoptions.newformat :
                                $(this).jqGrid("getGridRes", "formatter.date.newformat"),
                        srcformat = cm.formatoptions != null && cm.formatoptions.srcformat ?
                                cm.formatoptions.srcformat :
                                $(this).jqGrid("getGridRes", "formatter.date.srcformat"),
                        fieldData = $.jgrid.parseDate.call(this, srcformat, options.item[options.cmName]),
                        searchValue = $.jgrid.parseDate.call(this, newformat, options.searchValue);
                    var retFData = convertD(fieldData), t = new Date(retFData);
                    if ((retFData.getFullYear() < searchValue.getFullYear()) && (retFData.getMonth() < searchValue.getMonth()) && (retFData.getDate() < searchValue.getDate())) {
                        return true
                    }
                }
           },
}

The convert function that I am using for converting string into date format, is written below:

function convertD(dateField) {
var date = new Date(dateField),
    mnth = ("0" + (date.getMonth() + 1)).slice(-2),
    day = ("0" + date.getDate()).slice(-2);
// year= (date.getFullYear())

var retVal = [day, mnth, date.getFullYear()].join("/");
return retVal;
}

I have taken the idea from here and made some changes but seems no avail. So requesting community to help on this.

Chandan
  • 217
  • 1
  • 3
  • 17
  • Could you provide **the demo** with some test data, which reproduce the problem? The exact format of your input data could be important. For example, you can get some jsfiddle demo, which I created before and to modify it so that one can see the problem. – Oleg Apr 27 '19 at 09:58
  • Additionally I don't understand the meaning of the line `var retFData = convertD(fieldData), t = new Date(retFData);`. If you use `srcformat` and `newformat` options of `formatter: "date"` corresponding the input and display data format, then `fieldData` and `searchValue` will be already `Date` objects. See the code and the demo from [the old answer](https://stackoverflow.com/a/29676941/315935). – Oleg Apr 27 '19 at 10:10
  • Hello @Oleg, (here)[https://jsfiddle.net/6rub7svz/1/] is the **js fiddle demo** with some test data. Here you will see some date columns are empty. What I want is to create a custom filter which is able to select rows without selecting empty rows. The operator here is **date less but not empty** . – Chandan Apr 27 '19 at 11:34

1 Answers1

1

The code of dlne could be fixed to for example the following:

dlne: {
    operand: "<!=''",
    text: "Date less but not empty",
    filter: function (options) {
        var p = this.p, iCol = p.iColByName[options.cmName], cm = p.colModel[iCol],
            newformat = cm.formatoptions != null && cm.formatoptions.newformat ?
                    cm.formatoptions.newformat :
                    $(this).jqGrid("getGridRes", "formatter.date.newformat"),
            srcformat = cm.formatoptions != null && cm.formatoptions.srcformat ?
                    cm.formatoptions.srcformat :
                    $(this).jqGrid("getGridRes", "formatter.date.srcformat"),
            fieldData, searchValue;

        // the exact condition to test for "empty" depend on the format of your data
        if (!options.item[options.cmName]) {
            return false; // ignore empty data
        }

        fieldData = $.jgrid.parseDate.call(this, srcformat, options.item[options.cmName]);
        searchValue = $.jgrid.parseDate.call(this, newformat, options.searchValue);
        return fieldData.getFullYear() < searchValue.getFullYear() ||
            (fieldData.getFullYear() === searchValue.getFullYear() &&
                fieldData.getMonth() < searchValue.getMonth()) ||
            (fieldData.getFullYear() === searchValue.getFullYear() &&
                fieldData.getMonth() === searchValue.getMonth() &&
              fieldData.getDate() < searchValue.getDate());
    }
}

see https://jsfiddle.net/OlegKi/51vfn4k9/11/

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Hello @Oleg, While the above answer works for other columns, there is one column which date is coming in following format: "/Date(1547145000000)/". So that is failing the result grid. Could you please tell how to change the format of that date column? – Chandan Apr 27 '19 at 12:26
  • Also, $(this).jqGrid("getGridRes", "formatter.date.srcformat") giving date format is in d-m-Y, which I want in dd/mm/yy (ex: 27/04/2019). – Chandan Apr 27 '19 at 12:37
  • @Chandan: Why you don't post the demo with your test data, like I asked you? Instead of that you posted my original demo. Could you modify my demo and include **your test data**, which includes some non-empty dates and empty dates too? Additionally it's important to use `srcformat`, which corresponds the format of your data. Try https://jsfiddle.net/OlegKi/51vfn4k9/16/, which uses `formatoptions: { srcformat: "d/m/Y", newformat: "d/m/Y" }`. The input data having the format `"/Date(1547145000000)/"` will be detected automatically and processed ignoring `srcformat`. – Oleg Apr 27 '19 at 12:40
  • If I correclty understand you then it seems you haven't seen the fiddle that I posted earlier. Here is the jsfiddle https://jsfiddle.net/40ckdxs1/ which includes test data almost similar to what I have used in my project. The format option `formatoptions: { srcformat: "d/m/Y", newformat: "d/m/Y" }` is in my original project but there this format `"/Date(1547145000000)/"` is I guess causing the issue. – Chandan Apr 27 '19 at 12:54
  • @Chandan: Your last demo uses `formatoptions: {srcformat:"d/m/Y", newformat: "d/m/Y" }`, but the input data don't corresponds the `srcformat`: see `"2007-09-06"` and `"2007-10-03"` mixed with dates like `"12/10/2007"`. You should use **one** format, which corresponds `srcformat`. Moreover if **another formatted data** (like "/Date(1547145000000)/") causing the issue then why you don't include the data in the demo? Just post the demo, which can be used to reproduce the problem, which you have. – Oleg Apr 27 '19 at 12:58
  • Boss, thanks for the answer, it was my overthinking/discrepant data which stretched the thread. :) but now its all sorted. so accepting the answer. Thanking you again. Here is two demo links in case any one want to see them: https://jsfiddle.net/40ckdxs1/2/ https://jsfiddle.net/6rub7svz/1/ – Chandan Apr 27 '19 at 13:43