【问题标题】:How to save the sate of a grid using a database instead of a localstore jqgrid如何使用数据库而不是本地存储 jqgrid 保存网格的状态
【发布时间】:2013-01-11 00:03:33
【问题描述】:

我有一个使用 jqGrid PHP API 和选择列附加组件(用于显示/隐藏列)创建的网格。我需要添加保存网格状态的功能(在进行任何过滤、更改列顺序或隐藏任何列之后)。

我看过几篇使用 cookie 或 localStore 来保存网格状态的帖子。但是,我希望能够将其保存在数据库中,以便稍后在用户下次登录系统时恢复它。

这是我为网格编写的 PHP 代码:

    require_once JQGRID_PATH."php/jqGrid.php";
    require_once JQGRID_PATH."php/jqGridPdo.php";

    // Connection to the server
    $conn = new PDO(DB_DSN,DB_USER,DB_PASS);

    // Create the jqGrid instance
    $grid = new jqGridRender($conn);

    // Write the SQL Query
    $grid->SelectCommand = 'SELECT timestamp, user_name, action_type FROM audit_log';

    // set the ouput format to json
    $grid->dataType = 'json';

    // Let the grid create the model
    $grid->setColModel();

    // Set the url from where we obtain the data
    $grid->setUrl('audit_log');

    // Set grid caption using the option caption
    $grid->setGridOptions(array(
        "caption"=>"Audit Log Report",
        "height"=>470,
        "width" => 1100,
        "rowNum"=>25,
        "shrinkToFit"=>false,
        "sortname"=>"timestamp",
        "sortorder"=>"desc",
        "rowList"=>array(25,50,100)
    )); 

    $grid->navigator = true;
    $grid->setNavOptions(
        'navigator', array(
            "excel"=>true,
            "add"=>false,
            "edit"=>false,
            "view"=>false,
            "del"=>false,
            "pdf"=>true
        )
    );

    $grid->renderGrid('#grid','#pager', true, null, null, true, true, true);

另外,我看到的使用 localStore 来保存网格状态的帖子都是使用 jQuery 而不是 jqGrid PHP Suite 构建的。关于如何使用 jqGrid PHP API 完成此任务的任何想法?

任何帮助将不胜感激。

谢谢!

【问题讨论】:

    标签: php jqgrid grid


    【解决方案1】:

    如果您的网格选项是一个数组,为什么不直接序列化该数组并将其保存到与该用户关联的记录中的数据库中?

    如果你想变得更花哨,你可以使用persist()load() 等方法扩展jqGridRender 类,以将选项保存到数据库并重新加载。

    所以假设你有一个用户对象,也许你会做这样的事情:

    $grid = new persistingJQGrid($conn); // instantiate your special extended class
    $grid->setUser($user);
    $grid->load();
    ...
    

    或在持久化时:

    $grid->persist()
    

    【讨论】:

      【解决方案2】:

      the answer 中,我展示了如何在localStorage 中保存一些表示网格状态的信息,以及如何使用这些信息创建网格。在答案中,您可以找到saveObjectInLocalStorage 方法,该方法使用window.localStorage.setItem 将代表状态的对象保存在本地存储中。在您需要将信息保存在数据库中时,您只需将 window.localStorage.setItem 替换为 $.ajax 即可将信息发送到服务器。以同样的方式,您可以替换 getObjectFromLocalStorage 在网格初始化期间使用 once 以使用先前保存的从数据库读取的数据直接初始化数据。因此该方法可以很容易地修改到任何保持网格状态的地方。

      【讨论】:

        猜你喜欢
        • 2011-04-04
        • 2014-10-12
        • 2015-02-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-14
        相关资源
        最近更新 更多