我正在使用来自JSON文件的数据创建树面板,当我展开类别时正确显示元素但是如果我展开另一个类别元素打开desappier,则会发生展开和收缩事件中的所有元素。
以下是行为的图片
拳头扩展事件:
第二次扩展活动:
第三次扩展活动:
以下是我使用的代码:
模特:
Ext.define('app.model.modeloCapa', {
extend: 'Ext.data.Model',
fields: ['nombre','url'],
proxy: {
type: 'ajax',
url: "data/jsonprovisional.json",
reader: {
type : 'json',
root : 'Result',
}
}});
商店:
Ext.define('app.store.capa', {
extend: 'Ext.data.TreeStore',
requires: 'app.model.modeloCapa',
model: 'app.model.modeloCapa'});
观点:
initComponent: function() {
var anchoPanatallaRes = window.screen.width;
var altoPantallaRes = window.screen.height;
var anchoTOC = 330;
var altoTOC = 473;
if (anchoPanatallaRes <= 1024) {
anchoTOC = 282;
altoTOC = 373;
}
var treeStore = Ext.getStore('capa');
function addUrl(value, p, record) {
return value ? Ext.String.format(
'<a href="'+value+'"target="_blank"'+'>Ver metadato'+'</a>'
):'';
}
var tree = Ext.create('Ext.tree.Panel', {
title: '',
reserveScrollbar: true,
loadMask: true,
useArrows: true,
rootVisible: false,
store: 'capa',
allowDeselect : true,
border : true,
animate: true,
listeners: {
checkchange: function(node, checked, eOpts) {
console.log('nodo seleccionado:', node, checked, eOpts);
},
select: function( record, index, eOpts ){
console.log('Elemento seleccionado:', record, index, eOpts);
},
beforedeselect: function( record, index, eOpts ){
console.log("Elemento deseleccionado :", record);
}
},
columns: [{
xtype: 'treecolumn',
text: 'Capa',
flex: 5,
sortable: true,
dataIndex: 'nombre'
},{
text: 'Metadato',
flex: 2,
dataIndex: 'url',
renderer: addUrl
}],
tbar: [{
labelWidth: 100,
xtype: 'triggerfield',
fieldLabel: 'Nombre capa:',
triggerCls: 'x-form-clear-trigger',
onTriggerClick: function() {
this.reset();
treeStore.clearFilter();
this.focus();
},
enableKeyEvents: true,
listeners: {
keyup: function() {
var field = tree.down('textfield'),
v;
try {
v = new RegExp(field.getValue(), 'i');
treeStore.filter({
filterFn: function(node) {
var children = node.childNodes,
len = children && children.length,
//Visibilidad de los hijos
visible = node.isLeaf() ? v.test(node.get('nombre')) : false, i;
//Visibilidad de los padres
for (i = 0; i < len && !(visible = children[i].get('visible')); i++);
return visible;
},
id: 'campoFiltroCapa'
});
} catch (e) {
field.markInvalid('Expresión no valida');
}
},
buffer: 250
}
}, {
xtype : 'button',
text: 'Click',
id : 'btnApagarCapas',
action: 'apagarCapas',
tooltip : 'Apagar todas las capas',
padding:0,
listeners : {
click : function(boton) {
console.log("BOTON PRESIONADO!!");
}
}
}]
});
Ext.apply(this, {
title: 'TABLA CONTENIDO',
id: 'ventanaCapas',
constrain: true,
header : {
titlePosition : 2,
titleAlign : 'center'
},
closable : false,
width : anchoTOC,
height : altoTOC,
x : 20,
y : 270,
layout : 'fit',
animCollapse : true,
collapsible : true,
collapseDirection : Ext.Component.DIRECTION_LEFT,
listeners : {
collapse : function(p) {
}
},
items: [tree],
});
this.callParent(arguments);
}
我正在使用的JSON文件:
{"Result": [{
"nombre": "Componente Biótico y Abiótico",
"id": 1,
"Result":[{
"nombre": "Recursos hídricos",
"id": 2,
"Result": [{
"id": 268,
"titulo": "HIDROGEOLOGIA_ATLAS",
"url": "http://srsigaremap:8080/geoserver/web/?wicket:bookmarkablePage=:org.geoserver.web.demo.MapPreviewPage",
"nombre": "HIDROGEOLOGIA_ATLAS",
"metadato": "",
"wfs": false,
"leaf": true,
"checked": false
}]
},{
"nombre": "Fauna",
"id": 3,
"Result": [{
"id": 269,
"titulo": "AVES_ATLAS_ATLAS",
"url": "http://srsigaremap:8080/geoserver/web/?wicket:bookmarkablePage=:org.geoserver.web.demo.MapPreviewPage",
"nombre": "AVES_ATLAS_ATLAS",
"metadato": "",
"wfs": false,
"leaf": true,
"checked": false
}]
},{
"nombre": "Ecosistemas",
"id": 4,
"Result": [{
"id": 270,
"titulo": "ECOSISTEMAS_MARINOS_ATLAS",
"url": "http://srsigaremap:8080/geoserver/web/?wicket:bookmarkablePage=:org.geoserver.web.demo.MapPreviewPage",
"nombre": "ECOSISTEMAS_MARINOS_ATLAS",
"metadato": "",
"wfs": false,
"leaf": true,
"checked": false
}]
}]},{
"nombre": "ARE",
"id": 5,
"Result":[{
"nombre": "Minero Energético",
"id": 2,
"Result": [{
"id": 271,
"titulo": "GASODUCTO_ATLAS",
"url": "http://srsigaremap:8080/geoserver/web/?wicket:bookmarkablePage=:org.geoserver.web.demo.MapPreviewPage",
"nombre": "GASODUCTO_ATLAS",
"metadato": "",
"wfs": false,
"leaf": true,
"checked": false
},{
"id": 272,
"titulo": "TITULOS_MINEROS_ATLAS",
"url": "http://srsigaremap:8080/geoserver/web/?wicket:bookmarkablePage=:org.geoserver.web.demo.MapPreviewPage",
"nombre": "TITULOS_MINEROS_ATLAS",
"metadato": "",
"wfs": false,
"leaf": true,
"checked": false
}]
}]},{
"nombre":"Agrología",
"id":13,
"Result":[{
"nombre": "Cobertura y uso",
"id": 2,
"Result": [{
"id": 271,
"titulo": "COBERTURA_VEGETAL_GUAJIRA_ATLAS",
"url": "http://srsigaremap:8080/geoserver/web/?wicket:bookmarkablePage=:org.geoserver.web.demo.MapPreviewPage",
"nombre": "COBERTURA_VEGETAL_GUAJIRA_ATLAS",
"metadato": "",
"wfs": false,
"leaf": true,
"checked": false
}]
}]}]}
答案 0 :(得分:2)
仅当所有节点都具有唯一的“id”属性时,TreePanel才能正常工作。
在您的JSON文件中,我们可以看到“Recursoshídricos”节点,“MineroEnergético”和“Cobertura y uso”具有相同的ID:
perl -ne 'if (1 .. /^:127\.200\.200\.100:.*/) {
print "$.\t$_";
} else {
print $.--, "\t$_"
}' file.txt \
| sort -k1,1n -k2 | cut -f2-
这就是当您单击“ARE”节点时“Recursoshídricos”消失的原因。确保为所有节点设置唯一的ID(无论它们的级别如何),您的问题都将得到解决。