在网格 Extjs 6 中放置模型的参考信息
Place information of a model with reference in a grid Extjs 6
我尝试在网格中显示模型信息,但它不显示参考模型的信息。
我的模特人物
Ext.define('Myapp.model.Person',{
extend: 'Ext.data.Model',
idPropery: 'dni',
fields: [
"dni",
{name:'age', type: 'int'},
{name:'names', type: 'string'}
]
});
我的劳模:
Ext.define('Myapp.model.Worker',{
extend: 'Ext.data.Model',
idPropery: 'idWorker',
fields: [
"idWorker",
{name:'salary', type: 'int'},
{name: 'personId', reference: 'Person', unique:true}
]
});
我的商店:
Ext.define('Myapp.store.Worker',{
extend: 'Ext.data.Store',
alias: 'sworker',
model: 'Myapp.model.Worker',
autoLoad: true,
proxy: {
type: 'ajax',
api: {
read: 'allWorkers'
},
reader: {
type: 'json',
rootProperty: 'data',
totalProperty: 'total'
}
}
});
我的json格式:
data: [
{"idWorker":'COD0001',"salary":2700,"person":{"dni":"57547854", "age": 22, "names": "Diana"}},
{"idWorker":'COD0002',"salary":1700,"person":{"dni":"00257854", "age": 27, "names": "Carlo"},
{"idWorker":'COD0003',"salary":5000,"person":{"dni":"54787854", "age": 18, "names": "Pedro"},
{"idWorker":'COD0004',"salary":1800,"person":{"dni":"57547854", "age": 30, "names": "Ramon"}
],
total: 4
我的网格:
Ext.define('Myapp.view.worker.WorkerList', {
extend: 'Ext.grid.Panel',
xtype: 'workerList',
store: {
type: 'sworker'
},
columns: [
{text: 'My code', dataIndex: 'idWorker'},
{text: 'Names ', dataIndex: 'names'},
{text: 'Age ', dataIndex: 'age'},
{text: 'Salary ', dataIndex: 'salary'}
],
dockedItems: [{
xtype: 'pagingtoolbar',
dock: 'bottom',
displayInfo: true
}]
});
上面的代码不显示'names'、'age'。所以我做了一些改变。
columns: [
{text: 'My code', dataIndex: 'idWorker'},
{text: 'Names ', xtype: 'templatecolumn', tlp: '{person.names}'},
{text: 'Age ', xtype: 'templatecolumn', tlp: '{person.age}'},
{text: 'Salary ', dataIndex: 'salary'}
]
但现在不适用于 'name' 和 'age.有人可以帮助我。
请考虑以下解决方案:
勾选这个FIDDLE
Ext.application({
name: 'Fiddle',
launch: function () {
Ext.define('MyModel', {
extend: 'Ext.data.Model',
idPropery: 'idWorker',
fields: [
"idWorker", {
name: 'salary',
type: 'int'
},
{
name: 'age',
type: 'string',
mapping: "person['age']"
},
{
name: 'names',
type: 'string',
mapping: "person['names']"
}
]
});
var store = Ext.create('Ext.data.Store', {
model: 'MyModel',
autoLoad: true,
proxy: {
type: 'memory',
enablePaging: true,
data: [{
"idWorker": 'COD0001',
"salary": 2700,
"person": {
"dni": "57547854",
"age": 22,
"names": "Diana"
}
}, {
"idWorker": 'COD0002',
"salary": 1700,
"person": {
"dni": "00257854",
"age": 27,
"names": "Carlo"
}
}, {
"idWorker": 'COD0003',
"salary": 5000,
"person": {
"dni": "54787854",
"age": 18,
"names": "Pedro"
}
}, {
"idWorker": 'COD0004',
"salary": 1800,
"person": {
"dni": "57547854",
"age": 30,
"names": "Ramon"
}
}],
reader: {
//type: 'array'
type: 'json',
rootProperty: 'data'
}
}
});
var list = Ext.create('Ext.grid.Panel', {
title: 'MyApp',
renderTo: Ext.getBody(),
store: store,
columns: [{
text: 'My code',
dataIndex: 'idWorker'
}, {
text: 'Names ',
dataIndex: 'names'
}, {
text: 'Age ',
dataIndex: 'age'
}, {
text: 'Salary ',
dataIndex: 'salary'
}],
dockedItems: [{
xtype: 'pagingtoolbar',
dock: 'bottom',
displayInfo: true
}]
});
}
});
我尝试在网格中显示模型信息,但它不显示参考模型的信息。
我的模特人物
Ext.define('Myapp.model.Person',{
extend: 'Ext.data.Model',
idPropery: 'dni',
fields: [
"dni",
{name:'age', type: 'int'},
{name:'names', type: 'string'}
]
});
我的劳模:
Ext.define('Myapp.model.Worker',{
extend: 'Ext.data.Model',
idPropery: 'idWorker',
fields: [
"idWorker",
{name:'salary', type: 'int'},
{name: 'personId', reference: 'Person', unique:true}
]
});
我的商店:
Ext.define('Myapp.store.Worker',{
extend: 'Ext.data.Store',
alias: 'sworker',
model: 'Myapp.model.Worker',
autoLoad: true,
proxy: {
type: 'ajax',
api: {
read: 'allWorkers'
},
reader: {
type: 'json',
rootProperty: 'data',
totalProperty: 'total'
}
}
});
我的json格式:
data: [
{"idWorker":'COD0001',"salary":2700,"person":{"dni":"57547854", "age": 22, "names": "Diana"}},
{"idWorker":'COD0002',"salary":1700,"person":{"dni":"00257854", "age": 27, "names": "Carlo"},
{"idWorker":'COD0003',"salary":5000,"person":{"dni":"54787854", "age": 18, "names": "Pedro"},
{"idWorker":'COD0004',"salary":1800,"person":{"dni":"57547854", "age": 30, "names": "Ramon"}
],
total: 4
我的网格:
Ext.define('Myapp.view.worker.WorkerList', {
extend: 'Ext.grid.Panel',
xtype: 'workerList',
store: {
type: 'sworker'
},
columns: [
{text: 'My code', dataIndex: 'idWorker'},
{text: 'Names ', dataIndex: 'names'},
{text: 'Age ', dataIndex: 'age'},
{text: 'Salary ', dataIndex: 'salary'}
],
dockedItems: [{
xtype: 'pagingtoolbar',
dock: 'bottom',
displayInfo: true
}]
});
上面的代码不显示'names'、'age'。所以我做了一些改变。
columns: [
{text: 'My code', dataIndex: 'idWorker'},
{text: 'Names ', xtype: 'templatecolumn', tlp: '{person.names}'},
{text: 'Age ', xtype: 'templatecolumn', tlp: '{person.age}'},
{text: 'Salary ', dataIndex: 'salary'}
]
但现在不适用于 'name' 和 'age.有人可以帮助我。
请考虑以下解决方案:
勾选这个FIDDLE
Ext.application({
name: 'Fiddle',
launch: function () {
Ext.define('MyModel', {
extend: 'Ext.data.Model',
idPropery: 'idWorker',
fields: [
"idWorker", {
name: 'salary',
type: 'int'
},
{
name: 'age',
type: 'string',
mapping: "person['age']"
},
{
name: 'names',
type: 'string',
mapping: "person['names']"
}
]
});
var store = Ext.create('Ext.data.Store', {
model: 'MyModel',
autoLoad: true,
proxy: {
type: 'memory',
enablePaging: true,
data: [{
"idWorker": 'COD0001',
"salary": 2700,
"person": {
"dni": "57547854",
"age": 22,
"names": "Diana"
}
}, {
"idWorker": 'COD0002',
"salary": 1700,
"person": {
"dni": "00257854",
"age": 27,
"names": "Carlo"
}
}, {
"idWorker": 'COD0003',
"salary": 5000,
"person": {
"dni": "54787854",
"age": 18,
"names": "Pedro"
}
}, {
"idWorker": 'COD0004',
"salary": 1800,
"person": {
"dni": "57547854",
"age": 30,
"names": "Ramon"
}
}],
reader: {
//type: 'array'
type: 'json',
rootProperty: 'data'
}
}
});
var list = Ext.create('Ext.grid.Panel', {
title: 'MyApp',
renderTo: Ext.getBody(),
store: store,
columns: [{
text: 'My code',
dataIndex: 'idWorker'
}, {
text: 'Names ',
dataIndex: 'names'
}, {
text: 'Age ',
dataIndex: 'age'
}, {
text: 'Salary ',
dataIndex: 'salary'
}],
dockedItems: [{
xtype: 'pagingtoolbar',
dock: 'bottom',
displayInfo: true
}]
});
}
});