在jqGrid列上使用Formatter后,日期不会正确过滤

时间:2013-10-14 20:42:16

标签: jqgrid

我遇到一些问题,在我的jqGrid中正确过滤了一行日期。这是我的.cshtml的一部分:

<script type="text/javascript">
        $(function () {
            var width = $(window).width() - 50;
            $("#orders_grid").jqGrid({
                datatype: "local",
                width: width,
                height: "auto",
                search: true,
                autowidth: false,
                shrinkToFit: true,
                colNames: ['ID', 'Status', 'Category','Sub Category', 'Title', 'Owner', 'Team', 'Priority', 'Release', 'Business Line', 'Created', 'Update'],
                colModel: [
                    { name: 'ID',                                   width: 12,          align: 'center',    sorttype: 'int'},
                    { name: 'GridStatus',                           width: 15,          align: 'center'},
                    { name: 'GridCategory',                         width: 15,          align: 'center'},                                
                    { name: 'GridSubCategory',                      width: 15,          align: 'center'},
                    { name: 'Title',                                width: 60,          align: 'left'  },                                
                    { name: 'GridOwnerUser',                        width: 20,          align: 'center'},
                    { name: 'GridTeam',                             width: 30,          align: 'center'},                        
                    { name: 'GridPriority',                         width: 12,          align: 'center'},
                    { name: 'GridRelease',                          width: 12,          align: 'center'},                       
                    { name: 'GridBusinessLine',                     width: 12,          align: 'center'},
                    { name: 'CreatedDateTime',                      width: 14,          align: 'center',    sortable: true, sorttype: 'd',       formatter: dateFix },
                    { name: 'LastUpdateDateTime',                   width: 14,          align: 'center',    sortable: true, sorttype: 'd',        formatter: dateFix }
                ],
                rowNum: 20,
                rowList: [20,50,100,1000,100000],
                viewrecords: true,
                pager: '#gridpager',
                sortname: "ID",
                sortable: true,
                ignoreCase: true,
                headertitles: true,
                sortorder: "desc",
                onSelectRow: function (rowId)
                {
                    var id = $("#orders_grid").getCell(rowId, 'ID');
                    document.location.href = '/TicketCenter/Display/'+ id;
                }                
                }).navGrid("#orders_grid_pager", { edit: false, add: false, del: false }, {}, {}, {}, { multipleSearch: true, multipleGroup: false, showQuery: false, recreateFilter: true });
                    $("#orders_grid").jqGrid('filterToolbar', { stringResult: true, searchOnEnter: true, defaultSearch: 'cn' });
                    setTimeout(function () { searchOrders('@Model.filterFor'); }, 200);
        });

        function dateFix(LastUpdateDateTime)
        {
            var x = LastUpdateDateTime.substring(6, LastUpdateDateTime.length - 2);
            x = parseInt(x);
            x = new Date(x);
            x.setMinutes(x.getMinutes() - x.getTimezoneOffset());
            x = x.format("mm/dd/yyyy h:MM TT");
            return x;
        }

        function searchOrders(filter)
        {
            var data = { filter: filter } 
            var request = $.ajax({
                url: "@Url.Action("ListTickets", "TicketCenter")",
                type: "GET",
                cache: false,
                async: true,
                data: data,
                dataType: "json",
            });           
            request.done(function (orders) {             
                var thegrid = $("#orders_grid");
                thegrid.clearGridData();
                setTimeout(function()
                {
                    for (var i = 0; i < orders.length; i++)
                    {
                        thegrid.addRowData(i+1, orders[i]);
                    }
                    thegrid.trigger("reloadGrid");
                }, 500);
            });
            request.fail(function(orders) {
            });
        }
</script>

我需要能够在CreatedDateTime和LastUpdateDateTime列中进行搜索。当我从数据库中获取数据时,它最初是一个日期时间。加载网格时,日期显示为“/ Date102342523523463246236236”,显然为刻度。我用dateFix格式化器格式化了它,它以mm / dd / yyyy h:MM TT格式返回日期。现在当我尝试按日期搜索时,我得到了奇怪的结果。我认为基础数据仍然没有格式化,而是在搜索它。这是一个例子:

在我的数据库中,一个对象的CreatedDateTime为 2013-10-11 20:20:10.963

当jqGrid加载数据时,它会显示 / Date(1381537210963)

将格式化程序:dateFix添加到colModel后,它显示 10/11/2013 4:20 PM

如果我在搜索框中输入 10 ,则会返回该对象。

如果我输入 10 / ,则不会返回任何内容。

如果我输入 2013 ,则不会返回任何内容。

有没有办法解决这个问题?

1 个答案:

答案 0 :(得分:0)

决定在将日期发送到网格之前将日期转换为控制器中的字符串。解决。

相关问题