Reputation: 451
I'm creating a tree panel with data from a JSON file, when I expand a categorie shows elements correctly but if i expand another categorie the elements opened desappier, this happens with all elements in the expand and contract event.
Here are a pictures of the behaviour
Fist expand event:
Second expand event:
Third expand event:
Here is the code that i'm using:
The model:
Ext.define('app.model.modeloCapa', {
extend: 'Ext.data.Model',
fields: ['nombre','url'],
proxy: {
type: 'ajax',
url: "data/jsonprovisional.json",
reader: {
type : 'json',
root : 'Result',
}
}});
The store:
Ext.define('app.store.capa', {
extend: 'Ext.data.TreeStore',
requires: 'app.model.modeloCapa',
model: 'app.model.modeloCapa'});
The view:
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);
}
And teh JSON file that i'm using:
{"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
}]
}]}]}
Upvotes: 1
Views: 949
Reputation: 776
TreePanel works fine only if all your nodes have a unique 'id' property.
In your JSON file, we can see that "Recursos hídricos" node, "Minero Energético" and "Cobertura y uso" have the same id :
"nombre": "Recursos hídricos",
"id": 2
"nombre": "Minero Energético",
"id": 2
"nombre": "Cobertura y uso",
"id": 2
That's why "Recursos hídricos" disapear when you click on your "ARE" node. Make sure to set a unique ID to all your nodes (no matter the level they are) and your problem will be solved.
Upvotes: 2