需要帮助为嵌套JSON设置hasMany模型关系并在网格中显示数据-EXTJS 6

时间:2018-06-27 16:37:56

标签: extjs extjs6-classic

我从Elastic那里以这种格式得到了一些搜索结果

{
  "took": 267,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1911,
    "max_score": 29.118078,
    "hits": [
      {
        "_index": "myIndex",
        "_type": "doc",
        "_id": "27c9d14495f7732c6756f0d2688874f",
        "_score": 21.179974,
        "_source": {
          "file": {
            "filename": "file1.pdf"
          },
          "content": "samplecontent"
        }
      },
      {
        "_index": "myIndex",
        "_type": "doc",
        "_id": "27c9d14495f7732c6756f0d2688874f",
        "_score": 21.179974,
        "_source": {
          "file": {
            "filename": "file2.pdf"
          },
          "content": "samplecontent"
        }
      },
      {
        "_index": "myIndex",
        "_type": "doc",
        "_id": "27c9d14495f7732c6756f0d2688874f",
        "_score": 21.179974,
        "_source": {
          "file": {
            "filename": "file3.pdf"
          },
          "content": "samplecontent"
        }
      }
    ]
  }
}

,并希望在网格标题中显示hits.total(1911),并在网格中显示hits.hits._source.file.filename和hits.hits._source.content。我相信我需要设置一个hasMany / belongsTo模型,但是我对如何建立这种关系以及挑选并选择那些嵌套字段以显示在网格中的任何文档/示例都感到困惑。任何帮助或澄清将不胜感激

我的商店定义为

Ext.define('search.store.results', {
    extend: 'Ext.data.Store',

    alias: 'store.results',

    model: 'search.model.hits',

    autoLoad: false,

    proxy: {
        type: 'ajax',
        url: 'http://myServer:9200/_search',
        reader: {
            type: 'json',
            rootProperty: 'hits'
        }
    }
});

我的模型定义为

基本

Ext.define('search.model.Base', {
    extend: 'Ext.data.Model',

    schema: {
        namespace: 'search.model'
    }
});

点击

Ext.define('search.model.hits', {
    extend: 'search.model.Base',

    fields: [
        {name: 'total', type: 'number'}
    ],
    hasMany:{
        model: 'hit',
        name: 'hit'
    }
});

点击

Ext.define('search.model.hit', {
    extend: 'search.model.Base',

    fields: [
        {name: '_source', type: 'source'}
    ]
});

Ext.define('search.model.source', {
    extend: 'search.model.Base',

    fields: [
        {name: 'file', type: 'file'},
        {name: 'content', type: 'string'}
    ]
});

和文件

Ext.define('search.model.file', {
    extend: 'search.model.Base',

    fields: [
        {name: 'filename', type: 'string'}
    ]
});

但是,当我加载页面时,我立即获得了这些错误

Uncaught Error: [Ext.createByAlias] Unrecognized alias: data.field.source

Uncaught Error: [Ext.createByAlias] Unrecognized alias: data.field.file

更新:如果我将模型更改为使用“引用”而不是hasMany,结果就是这样

Ext.define('search.model.hits', {
    extend: 'search.model.Base',

    fields: [
        {name: 'total', type: 'number'},
        {name: 'hits', reference: 'hit'}
    ]
});

Ext.define('search.model.hit', {
    extend: 'search.model.Base',

    fields: [
        {name: '_source', reference: 'source'}
    ]
});

Ext.define('search.model.source', {
    extend: 'search.model.Base',

    fields: [
        {name: 'file', reference: 'file'},
        {name: 'content', type: 'string'}
    ]
});

Ext.define('search.model.file', {
    extend: 'search.model.Base',

    fields: [
        {name: 'filename', type: 'string'}
    ]
});

我的商店“加载”侦听器显示此信息,这些信息被分解为单独的数据结构,而不是全部都位于箭头所在的“数据”结构下(我用来查找商店记录的地方)

enter image description here

更新#2: 如果我将“匹配”模型修改为使用hasMany,而其余部分使用“引用”,则它似乎可以将数据正确地分组到记录中-现在,我只需要知道如何在网格中显示嵌套的结果< / p>

Ext.define('search.model.hits', {
    extend: 'search.model.Base',

    fields: [
        {name: 'total', type: 'number'}
    ],
    hasMany:{
        model: 'hit',
        name: 'hit'
    }
});

产生

enter image description here

0 个答案:

没有答案