Can we have 2 formatter options in jqgrid colModel

2020-03-24 09:03发布

I have a jqgrid, where it has a custom formatter to "column1". I need to sort the "column1" as date. I know that i can give "formatter:date", if i give this, my custom formatter does not work. Can we have 2 formatters or is there any other option for this ?

Thanks in advance!

1条回答
走好不送
2楼-- · 2020-03-24 09:44

No, you can only have one formatter per field, although a custom formatter can do anything including calling multiple formatters depending upon the value of the underlying data. In any case, the formatter is used to control how the data is displayed, but it should not affect how the rows are sorted.

If you are using local data, you can use the sorttype option to determine how a row of data is sorted. The sorting is based on the actual value of the cell; you are free to use a custom formatter to do whatever formatting is required.

Alternatively, if you are doing sorting server-side, I think you can still use the same approach. Just define whatever custom formatter you need, and then sort using the underlying date value of the column.


You should also be aware that there is an unformat option that can be used to retrieve the original data when using a custom formatter. I don't think you need it here, but wanted to mention it just-in-case you need to use it.

Does that help at all?


Update

To call the date formatter from your custom formatter function, you first need to set the appropriate options in your colmodel:

formatoptions: { srcformat:'m/d/Y', newformat:'ShortDate' },

Then you can call the date formatter just like any other function, from within your custom formatter:

function myFormatter (cellvalue, options, rowObject) {
    return $.fn.fmatter.date(cellvalue, options, rowObject);
}

The above should work, although I have not tested it. Please let me know how it goes...

查看更多
登录 后发表回答