PDA

View Full Version : Help with multi-level nested JSON data using XTemplate



david33409
27 Jul 2009, 11:35 AM
I have a multi-level nested JSON Data store that I want to display in a report format using a DataView. I have two problems:

1) I'm unable to use XTemplate past 2 levels.
2) How to add a conditional tpl statement to not do anything if there is no data.

For example, here is the kind of tpl I want to execute: (dots added for readability)

'<div id="report1">',
'<tpl for=".">',
....'<h3 id=\'{id}\'>{text}</h3>',
....// Question: How do I add conditional statement to skip the loop if there are no 'documents' not all items will have documents
....'<tpl for="documents">',
........'<h5 id=\'{id}\'>{text}</h5>',
// Question: How do I add conditional statement to skip the loop if there are no 'fields'. Not all documents will have fields
........'<tpl for="fields">',
............'<h6 id=\'{id}\'>{text}</h6>',
........'</tpl>',
....'</tpl>',
// Question: How do I add conditional statement to skip the loop if there are no 'accounts'
....'<tpl for="accounts">',
........'<h4 id=\'{id}\'>{text}</h4>',
// Question: How do I add conditional statement to skip the loop if there are no 'docs'
........'<tpl for="docs">',
............'<h5 id=\'{id}\'>{text}</h5>',
// Question: How do I add conditional statement to skip the loop if there are no 'docfields'. Not all docs will have docfields
............'<tpl for="docfields">',
................'<h6 id=\'{id}\'>{text}</h6>',
............'</tpl>',
........'</tpl>',
....'</tpl>',
'</tpl>',
'</div>',

Anything in blue above causes no data to be displayed. If I remove the items in blue it works.

My JSON data looks like:

id
text
items - array of first level items
[
id - item id
text - item name
documents - optional array of 2nd level documents
[
id - document id
text - document name
fields - optional array of document fields
[
id - field id
text - field name
]
]
accounts - optional array of accounts
[
id - account id
text - account name
docs - optional array of account docs
[
id - doc id
text - doc name
docfields - optional array of doc fields
[
id - docfield id
text - docfield name
]

]
]
]

I cannot access anything at the fields, docs or docfields level.

Here is my JsonStore definition. I have to admit I'm not clear how to define the fields, docs or docfields in the "fields" definition. I've looked all over to find an example. I know that I can define arrays in the "fields" definition like 'documents' and 'accounts' but not sure how to define arrays within arrays.

var store = new Ext.data.JsonStore({
idProperty : 'id',
fields : ['id', 'text', 'short', 'desc', 'documents', 'accounts'],
url: 'eds/json/MD1.json'

Everything works if I remove the blue text from the tpl.

FYI - I have looked at dozens of posts and articles and haven't found what I need to resolve this issue. Please don't reply if your comment isn't helpful. There are a few users that seem to like to tear people down for asking questions. Since I'm evaluating ExtJS for an upcoming development project before I purchase, un-helpful comments may steer me clear of a purchase.

Thank you in advance for your time and consideration.