jqgrid加载保存状态数据未动态加载

时间:2018-03-02 10:52:52

标签: javascript java jqgrid thymeleaf

我有很多列的jqgrid页面。想法是允许用户在表格中选择列及其顺序,并将此订单和列数保存在cookie中。为此,我使用$ .jgrid.saveState()进行保存,并使用$ .jgrid.loadState()从cookie恢复jqgrid选项。这种方法很好。但我不想将数据保存到存储:只有列顺序和过滤器。我尝试了选项restoreData:false和storeData:false,但它没有用。 这里是我的jqgrid的代码:

jQuery(function ($) {
var table = $("#table");
var pager = "#pager";
var search_panel = "#search";
var localization = globalLocale;
var colNames = [ "Mobile direction total time (sec)", "Mobile direction q-ty  ", "Mobile time Percent",
    "City direction total time (sec)", "City  direction q-ty", 'City time Percent'   ];
var exportToCsvBut = "Export to excell";

resizeToFitPageSize(table);
resizeOnSidebar(table);

$(function () {
    var d = new Date();
    var month = d.getMonth();
    var day = d.getDate();
    var year = d.getFullYear();
    var defCurDate = moment().startOf('day').format('DD.MM.YYYY HH:mm:ss');
    var defNextDate = moment().endOf('day').format('DD.MM.YYYY HH:mm:ss');
    var from = $('#datetimepickerFrom');
    var to = $('#datetimepickerTo');
    if (from.val() === "" || to.val() === "") {
        from.val(defCurDate);
        to.val(defNextDate);
    }
    $("#datetimepickerFrom").datetimepicker({
        dateFormat: 'dd.mm.yy',
        timeFormat: 'HH:mm:ss',
        defaultDate: new Date(year, month, day + 1, 00, 00),
        onClose: function () {
            beforeRefresh();
        }
    });
    $("#datetimepickerTo").datetimepicker({
        dateFormat: 'dd.mm.yy',
        timeFormat: 'HH:mm:ss',
        defaultDate: new Date(year, month, day + 1, 00, 00),
        onClose: function () {
            beforeRefresh();
        }
    });


    defCurDate = $('#datetimepickerFrom').val();
    defNextDate = $('#datetimepickerTo').val();
});


var initDate = function (elem) {
    $(elem).datetimepicker({
        dateFormat: 'd.m.Y H:i:s',
        showButtonPanel: true
    });
};

var template = {width: 160, fixed: true, align: 'center', editable: false, stype: 'text'};


var colModel = [

    {
        name: 'mobileDirectionTotalTime',
        index: 'mobileDirectionTotalTime',
        sortname: 'mobileDirectionTotalTime',
        template: template,
        formatter: nullFormatter,
        sorttype: 'number'
    },
    {
        name: 'mobileDirectionQty',
        index: 'mobileDirectionQty',
        sortname: 'mobileDirectionQty',
        template: template,
        formatter: nullFormatter,
        sorttype: 'number'
    },
    {
        name: 'mobileDirectionPercent',
        index: 'mobileDirectionPercent',
        sortname: 'mobileDirectionPercent',
        width: 120,
        fixed: true, align: 'center', editable: false,
        template: "number",
        sorttype: 'number'
    },
    {
        name: 'cityDirectionTotalTime',
        edittype: "custom",
        sortname: 'cityDirectionTotalTime',
        formatter: null,
        sorttype: 'number'

    },
    {
        name: 'cityDirectionQty',
        index: 'cityDirectionQty',
        sortname: 'cityDirectionQty',
        template: template,
        sorttype: 'number'
    },
    {
        name: 'cityDirectionTimePercent',
        index: 'cityDirectionTimePercent',
        sortname: 'cityDirectionTimePercent',
        template: template,
        sorttype: 'number'
    },

];

table.jqGrid({
    url: URL_DATA,
    datatype: "json",
    jsonReader: {
        repeatitems: true
    },
    height: 'auto',
    colNames: colNames,
    colModel: colModel,
    shrinkToFit: false,
    forceFit: true,
    pager: pager,
    toppager: true,
    rowNum: 10,
    rowList: [5, 10, 15, 20, 25, 30],
    loadonce: true,
    viewrecords: true,
    storeNavOptions : true,
    //    navOptions: {reloadGridOptions: {fromServer: true}},
    loadComplete: function () {
        var table = this;
        setTimeout(function () {
            updatePagerIcons(table);
            enableTooltips(table);
        }, 0);
    },
    gridComplete: function () {
        /*var idarray = table.jqGrid('getDataIDs');
        if (idarray.length > 0) {
            var firstid = table.jqGrid('getDataIDs')[0];
            table.setSelection(firstid);
        }*/
    }
});

$(window).triggerHandler('resize.jqGrid');//trigger window resize to make the grid get the correct size


function beforeRefresh() {
    var from = $('#datetimepickerFrom');
    var to = $('#datetimepickerTo');
    if (from.val() != null && from.val() != '' && to.val() != null && to.val() != '') {
        resetFilter('table', 'globalSearchText');
        table.jqGrid().setGridParam({datatype: 'json'}).setGridParam({url: URL_DATA + "?from=" + from.val() + "&to=" + to.val()}).trigger("reloadGrid")
        if (from.val() === "" || to.val() === "") {
            from.val(defCurDate);
            to.val(defNextDate);
        }
    }

}

//navButtons
table.jqGrid('navGrid', pager,
    {   //navbar options
        cloneToTop: true,
        edit: false,
        add: false,
        del: false,
        search: true,
        searchicon: 'ace-icon fa fa-search orange',

        refresh: true,
        beforeRefresh: function () {
            var from = $('#datetimepickerFrom');
            var to = $('#datetimepickerTo');
            if (from.val() != null && from.val() != '' && to.val() != null && to.val() != '') {
                resetFilter('table', 'globalSearchText');
                table.jqGrid().setGridParam({datatype: 'json'}).setGridParam({url: URL_DATA + "?from=" + from.val() + "&to=" + to.val()}).trigger("reloadGrid")
                if (from.val() === "" || to.val() === "") {
                    from.val(defCurDate);
                    to.val(defNextDate);
                }
            }
        },
        refreshicon: 'ace-icon fa fa fa-refresh green',
        view: true,
        viewicon: 'ace-icon fa fa-search-plus grey'
    }, {}, {}, {},
    {
        multipleSearch: true,
        searchOnEnter: true,
        closeOnEscape: true,
        multipleSearch: true,
        closeAfterSearch: true
    },
    {
        //view record form
        recreateForm: true,
        beforeShowForm: function (e) {
            var form = $(e[0]);
            form.closest('.ui-jqdialog').find('.ui-jqdialog-title').wrap('<div class="widget-header" />')
            form.closest('div.ui-jqdialog').center();
        }
    }
);

addToolButton({
    caption: "",
    buttonicon: "ace-icon fa fa-calculator blue",
    title: "Выбрать столбцы",
    onClickButton: function () {

        $(this).jqGrid('columnChooser',
            {
                width: 550,
                msel_opts: {dividerLocation: 0.5}, modal: true
            });
        $("#colchooser_" + $.jgrid.jqID(this.id) + ' div.available>div.actions')
            .prepend('<label style="float:left;position:relative;margin-left:0.6em;top:0.6em">Поиск:</label>');
    }
}, table, pager);


addSearchField('table', 'globalSearchText');
$(window).triggerHandler('resize.jqGrid');
table.triggerHandler("jqGridAfterGridComplete");
destroyGrid(table);



    $('#table')
        .append($("<div style='margin-top: 10px'>" +
            "<button id='savestate'>save state</button>" +"<button id='loadstate'>load state</button>" +
            "</div>"));


    $("#savestate").click(function(){
        $.jgrid.saveState("table");
    });
    $("#loadstate").click(function(){
        $.jgrid.loadState("table", {}, {});
    })
});

document.addEventListener('contextmenu', function (e) {
    e.preventDefault();
});

这里是表页面的视图: view before load state

用户按下加载状态并保存状态后,显示之前但不是实际的数据: data not changed after another date selecting

它只显示保存在localstorage中的数据。 如果我添加storeData:false它只是diplayed表没有数据,并且不会从服务器加载新数据。 有人可以帮忙吗?

P.S如果有人会遇到同样的问题:  $(“#table”)。jqGrid()。setGridParam({datatype:'json'})。setGridParam({url:URL_DATA})。trigger(“reloadGrid”);在$ .jgrid.loadState之后(“table”,“',{restoreData:false});将在加载的网格中加载新数据。

1 个答案:

答案 0 :(得分:0)

我建议您在loadState中使用restoreData:false,并在此命令之后使用触发器重新加载网格

$("#loadstate").click(function(){
   $.jgrid.loadState("table", '', { restoreData: false});
   $("#table").trigger("reloadGrid");
});

这将使用当前设置从服务器重新加载新的网格数据