【问题标题】:how to remember jqrid advanced search dialog size and position in local storage如何记住 jqrid 高级搜索对话框的大小和在本地存储中的位置
【发布时间】:2012-12-03 00:26:54
【问题描述】:

jqgrid 高级搜索对话框窗口大小和位置可以通过鼠标改变。

这些更改不会被记住。 下次打开,显示默认大小和位置。

如何保存和恢复它,可能使用本地存储。 如果屏幕分辨率或大小发生变化,还应检查搜索对话框的一部分是否可见。

更新

我也尝试使用下面的代码扩展 Oleg 答案以保存/恢复窗口位置。 搜索窗口恢复到与最初不同的位置。看起来使用下面的代码检索的 left 和 top 值是错误的。如何恢复位置呢?

var oldJqDnRstop, searchParams = { width: 550, left: 5, top: 5 };
if ($.jqDnR) {
    oldJqDnRstop = $.jqDnR.stop; // save original function
    $.jqDnR.stop = function (e) {
        var $dialog = $(e.target).parent(), dialogId = $dialog.attr("id");
        oldJqDnRstop.call(this, e); // call original function
        if (typeof dialogId === "string" && dialogId.substr(0, 14) === "searchmodfbox_") {
            // save the dialog position here
            // we save "width" option as the property of searchParams object
            // used as parameter of navGrid
            searchParams.width = $dialog.width();
            searchParams.left  = $dialog.offset().left;
            searchParams.top  = $dialog.offset().top;
            saveWindowState();
        }
    };
}

更新2

在 Oleg 演示中,对话框标题可以移动到浏览器窗口之外。之后对话框不再可移动。如何解决这个问题?

【问题讨论】:

  • 你能提供一个jsfiddle.net吗?
  • 如果在搜索参数中使用了recreateFilter: true,就会出现这种情况。每次使用默认宽度和位置重新创建搜索表单

标签: jquery jquery-ui jqgrid


【解决方案1】:

我觉得你的问题很有趣。所以下面你会发现一个可能的实现。

可以将您的问题分为两部分:1)在使用recreateFilter: true的情况下,在多次打开期间保存搜索对话框的宽度2)保存localStorage内部的宽度。

如果您已经使用localStorage,第二部分相对容易。您只需使用一个附加参数扩展已保存的状态。确切的实现在某种程度上取决于localStorage 中的保存方式是如何实现的。在the answer 中,我展示了如何使用window.localStorage 的getItem 和setItem 方法来实现localStorage 中的保存。或者可以使用jStorage,如果您需要另外支持IE6/IE7等旧的网络浏览器,这是非常实用的。

所以我在下面解释一下在使用recreateFilter: true的情况下如何在多次打开期间保存搜索对话框的宽度。

我建议将方法“子类化”$.jqDnR.stop,因为没有回调将在对话框调整大小结束时使用。对应的代码如下

var oldJqDnRstop, searchParams = { width: 450 };

if ($.jqDnR) {
    oldJqDnRstop = $.jqDnR.stop; // save original function
    $.jqDnR.stop = function (e) {
        var $dialog = $(e.target).parent(), dialogId = $dialog.attr("id");
        oldJqDnRstop.call(this, e); // call original function
        if (typeof dialogId === "string" && dialogId.substr(0,14) === "searchmodfbox_") {
            // save the dialog position here
            // we save "width" option as the property of searchParams object
            // used as parameter of navGrid
            searchParams.width = $dialog.width();
        }
    };
}

// create the grid
$grid.jqGrid({
    ....
});

// create navigator with "Search" button where searchParams object
// will be used as parameter
$grid.jqGrid("navGrid", "#pager", {add: false, edit: false, del: false},
    {}, {}, {}, searchParams);

可以看对应的demohere。

更新:要保存搜索对话框的位置和宽度,需要将$.jqDnR.stop的新实现代码扩展为以下

$.jqDnR.stop = function (e) {
    var $dialog = $(e.target).parent(), dialogId = $dialog.attr("id"), position;
    oldJqDnRstop.call(this, e); // call original function
    if (typeof dialogId === "string" && dialogId.substr(0,14) === "searchmodfbox_") {
        // save the dialog position here
        searchParams.width = $dialog.width();
        position = $dialog.position();
        searchParams.left = position.left;
        searchParams.top = position.top;
    }
};

在上面的代码中,left 和 top 选项将被额外保存。修改后的demo是here。

【讨论】:

  • 谢谢。我也将它扩展到 rember 位置,但如果位置不同,则创建对话框,而不是最后一个位置。如何记住位置呢?我更新了问题。
  • @Andrus:不客气!更新后的演示是here
  • 非常感谢。在您的演示对话框中,标题可以移动到客户区域之外,之后就无法再访问了。如何解决这个问题?我更新了问题并添加了图片。我们是否可以使用相同的方法来记住编辑、添加和查看表单大小和尺寸,并使用 $.extend 将它们添加到 navGrid 调用中的其他属性?
  • @Andrus:我不知道如何重现这个问题,但我想在你发布的图片中top 的值是负数。可能您只需要添加对已保存值的验证,并确保您设置了正的 top 和 left 值。下一个问题的答案:我认为可以像我一样为任何其他对话框(添加/编辑/删除等)保存width、top、left、height 和dataheight在我的回答中描述了搜索对话框。
  • 要重现,用鼠标将搜索对话框拖到IE客户区底部之外,然后松开鼠标按钮。如何限制这种拖拽?在 chrome 中,搜索对话框在屏幕内恢复,但在 IE9 中,它在屏幕外恢复。最好不要像在常规 Windows 窗口中那样允许这样的拖动。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-15
  • 1970-01-01
  • 2014-08-01
  • 1970-01-01
  • 2011-02-08
  • 1970-01-01
相关资源
最近更新 更多