dgrid标题和内容重叠

时间:2016-08-24 14:36:55

标签: dojo dgrid

我正在使用模板化小部件来显示dgrid。除了标题和内容重叠之外,它似乎工作正常。这是我的Plnkr代码。有人建议创建一个与DijitRegistry混合的自定义网格,在我的情况下会导致以下错误:

Tried to register widget with id==dijit__TemplatedMixin_0 but that id is already registered. 

此外,我在一些人建议的两个事件中尝试了resize()方法,但这也没有帮助。

这是我的代码:

的index.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/dojo/1.10.1/dijit/themes/claro/claro.css" />
</head>
<body class="claro">
  <div id="myContainer"></div>
  <script type="text/javascript">
    var dojoConfig = {
      async: true,
      parseOnLoad: true
    };
  </script>
  <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/dojo/1.10.1/dojo/dojo.js"></script>
  <script type="text/javascript">
    require({
    packages: [
        {
            name: 'dgrid',
            location: '//cdn.rawgit.com/SitePen/dgrid/v0.3.16'
        },
        {
            name: 'xstyle',
            location: '//cdn.rawgit.com/kriszyp/xstyle/v0.2.1'
        },
        {
            name: 'put-selector',
            location: '//cdn.rawgit.com/kriszyp/put-selector/v0.3.5'
        }, 
        {
            name: 'myApp',
            location: window.location.href.replace(/\/$/, "")
      }
    ]
}, ["dojo/dom", "dojo/_base/array", "myApp/SimpleTemplatedGridWidget", "dojo/domReady!"], function(dom, arrayUtil, MyWidget) {
      var widget = new MyWidget().placeAt(dom.byId('myContainer'));
    });
  </script>
</body>
</html>

SimpleTempletedGridWidget.js

define([
     "dgrid/extensions/DijitRegistry",
     "dojo/_base/declare",
     "dgrid/OnDemandGrid",
     "dijit/_WidgetBase",
     "dijit/_TemplatedMixin",
     "dojo/text!./SimpleTemplate.html"
],
function (DijitRegistry, declare, Grid, _WidgetBase, _TemplatedMixin, template) {
    return declare([_WidgetBase, _TemplatedMixin], {
        templateString: template, //need to add
        data : [
            { first: 'Bob', last: 'Barker', age: 89 },
            { first: 'Vanna', last: 'White', age: 55 },
            { first: 'Pat', last: 'Sajak', age: 65 }
        ],

        columns : {
            first: 'First Name',
            last: 'Last Name',
            age: 'Age'
        },

        postCreate: function() {
          //myGrid = new (declare([Grid, DijitRegistry]))({
          //Including DijitRegistry in the custom grid as above causes the following error:
          //Tried to register widget with id==dijit__TemplatedMixin_0 but that id 
          //is already registered
            myGrid = new (declare([Grid]))({
                columns: this.columns,
                idProperty: "id"
            }, this.AttachGrid);
            myGrid.renderArray(this.data);
            myGrid.startup();
            //Resize does not fix it
            myGrid.resize();
        },

        //People talk about using resizing the grid in onShow event,
        //but it looks like this event does not fire
        //source: https://github.com/SitePen/dgrid/issues/249
        onShow: function(){
          this.inherited(arguments);
          this.myGrid.resize();
          console.log("Shown!");
        }
    });
});

SimpleTemplate.html

<div data-dojo-attach-point='AttachGrid'></div>

1 个答案:

答案 0 :(得分:2)

您需要在放置网格后调用resize()(而不是在postCreate()中)。您可以通过在网格小部件中添加resize()函数来手动执行此操作:

    postCreate: function() {
        this.myGrid = new (declare([Grid]))({
            columns: this.columns,
            idProperty: "id"
        }, this.AttachGrid);
        this.myGrid.renderArray(this.data);
        this.myGrid.startup();
    },

    resize: function() {
        this.myGrid.resize();
    }

在index.html中:

  var widget = new MyWidget().placeAt(dom.byId('myContainer'));
  widget.resize();

以下是代码的分支:plnkr

我仍然不知道DijitRegistry导致错误的原因。