【问题标题】:Create a Dojo grid and Add Dialog from Data Model创建 Dojo 网格并从数据模型添加对话框
【发布时间】:2012-07-04 19:37:25
【问题描述】:

我正在开发一个应用程序,用户可以在其中对多个数据模型(又名数据库表)执行 CRUD 操作。我正在使用 Dojo,我对 dojox.grid 模块非常满意。但是用户还需要添加记录,所以每个表都必须有一个添加对话框

有没有一种方法/模块可以生成一个 Dojo 网格和一个 Add Dialog 只给定模型的数据结构?有点像dojox.gridstructure 参数,因此网格和添加对话框具有相同的数据类型、默认值、约束等。 当然,我可以编写一个自定义小部件来执行此操作,但我正在寻找这里存在的东西。

【问题讨论】:

    标签: javascript dojo


    【解决方案1】:

    答案是,没有这样的模块。您需要构建一个派生对话框。

    让我们看看需要什么;

    1. 当前网格
    2. 网格布局(单元格类型)
    3. 名称和标签(结构)

    假设 pr-grid 定义了一个“添加内容”按钮,并且该按钮“知道”所述网格的 ID,它的 onClick 函数应该会在对话框中启动一个表单。

    虽然有 dijit.form 小部件,但还有一系列预定义的 cellTypes,位于 dojox/grid/cells/_base.js 下。让我们制作一个类型和小部件为 1to1 的地图:

        var map = [{
            type: 'dojox.grid.cells.Cell',
            dijit: 'dijit.form.TextBox'},
        {
            type: 'dojox.grid.cells.Bool',
            dijit: 'dijit.form.CheckBox'},
        {
            type: 'dojox.grid.cells.Select',
            dijit: 'dijit.form.Select'},
        {
            type: 'dojox.grid.cells.DateTextBox',
            dijit: 'dijit.form.DateTextBox'}
                 ];
    

    在我们的 addContents 函数中,我们将使用 dojox.grid.DataGrid 中的“可编辑”功能。当我们知道有这样的——当然还有一个函数 pr-cell 来生成 DOM。这是任何 cellType 中都存在的 formatEditing 函数。

      // for instance
      dojox.grid.cells.Select.prototype.formatEditing( /* value */ "", /* row */ -1);
    

    唯一需要做的就是构建应在对话框中显示的内容 - 以下使用上述功能并提供适合 dijit 的标记以在 dijit.Dialog 中显示。

    function addContents(gridId) {
        var grid = dijit.byId(gridId);
        var contents = ['<form action="MySubmitUrl" data-dojo-type="dijit.form.Form"><table>'];
        dojo.forEach(grid.layout.cells, function(cell, idx) {
            var szHtml = cell.formatEditing("", -1);
            var dijitType = map.filter(function(e) {
                return e.type == cell.declaredClass;
            })[0].dijit;
            var name = grid.structure[0][idx].field;
            var label = grid.structure[0][idx].name;
            var elementMod = ' data-dojo-type="' + dijitType + '" id="' + name + '" name="' + name + '" ';
            contents.push('<tr><td>');
            contents.push('<label for="' + name + '">' + label + ': </label>');
            contents.push('</td><td>');
            contents.push(szHtml.replace(/^([^\ ]*)/, "$1" + elementMod));
            contents.push('</td></tr>');
        });
        contents.push('</table></form>');
        var dialog = new dijit.Dialog({
            content: contents.join("")
        });
        dialog.show();
    }
    

    内容很容易样式化,还应该提供一个提交/取消按钮,但我确定你明白了。 Running sample

    让我知道它是如何运行的(尚未测试过组合框/日期时间类型)

    【讨论】:

    • 非常感谢您提供此解决方案。虽然我做了很多修改,但这为我指明了正确的方向:)
    猜你喜欢
    • 1970-01-01
    • 2011-04-23
    • 1970-01-01
    • 1970-01-01
    • 2013-05-01
    • 2011-08-29
    • 2010-12-15
    • 2011-04-02
    • 2023-04-03
    相关资源
    最近更新 更多