My json is as follow:
({
"status":"TRUE",
"message":"Words",
"data":[
{
"name":"paint",
"author":"snooky",
"word_id":"1",
"category":"Business",
"definitions":[
{
"rating":"Green",
"defintion":"to put color to in something",
"def_id":"1",
"example":null,
"author":"pit",
"is_favourite":"yesStar"
},
{
"rating":"Red",
"defintion":"this is a new definition of word paint.",
"def_id":"42",
"example":null,
"author":"bull",
"is_favourite":"noStar"
}
]
}
]
})
I am parsing this value and show it in tpl as shown below:
{
xtype: 'list',
store: 'words',
height: 140,
layout: 'fit',
scrollable: {
direction: 'vertical',
directionLock: true,
},
margin: '0 0 5px 0',
itemTpl: [
'<div>',
'<tpl for="data">',
'<ul class="parabox">',
'<li><h2><b>{name}</b></h2>',
'<tpl for="definitions">',
'<ul class="para-box-wrapper">',
'<li class="{rating}"><div >',
'<div class="paragraph-def" ><p id = "wordDefinition" >{defintion}</p></div>',
'<span class="authorBox"><i>Author: {author}</i></span>',
'<div id="favourite" class="{is_favourite}" ></div>',
'</div>',
'</li>',
'</ul>',
'</tpl>',
'</li>',
'</ul>',
'</tpl>',
'</div>',
].join(''),
listeners: {
itemtap : function(list, index, target, record, event) {
if (event.target.id=='wordDefinition') {
alert("Rating clicked!");
console.log(event.target.id);
console.dir("Definition--"+record);
//ratingStar();
}
if (event.target.id=='favourite') {
alert('Favourite clicked!');
console.log(event.target.id);
console.dir("Favourite--"+record);
//addToFavourite();
}
}
}
}
My Console is as follow:
![console][1]
As shown in above pic i want to access def_id and word_id when user clicks on the respective tpl as shown in below image when user clicks on definition i.e overweight football supporter i need it's word_id and when user clicks on star i need to get word_id.
Doing record.data.data[0].definitions[0].def_id i can get it but i want it to be dynamic as there may be 4-5 definition later.
![rate][2]
My store:
Ext.define('MyApp.store.wordsmenu',{
extend: 'Ext.data.Store',
config:
{
autoLoad:true,
model: 'MyApp.model.words',
id:'Contacts',
proxy:
{
type: 'ajax',
url: 'json',
reader:
{
rootProperty:''
}
}
}
});
My model are:
Ext.define('MyApp.model.words', {
extend: 'Ext.data.Model',
config: {
fields: [
{name: 'status', mapping: 'status'},
{name: 'message', mapping: 'message'},
{name:'data', mapping: 'data'},
{name: 'definitions', mapping: 'definitions.defintion'},
{name: 'ratings', mapping: 'definitions.rating'},
]
}
});
I am able show json value in tpl. Now, i should keep track of click on specific
definition and star and send its id to server but unable to get id.
For reference of record you can check here.
Any Help!
It seems that roll_id is a field of your model, so you should be able to get it from your record:
var rollId = record.get('roll_id');
ratingStart(rollId);
Related
In my ExtJS project, I have a grid bound to a store, but I want to do away with the grid's layout and use cards instead, similar to this angular sample.
Is there any container component by Sencha that takes a store and renders all records into a custom template? (based on sorting/filtering)
Deriving from the grid is a bit too much work, overriding the original templates breaks all kinds of things.
You can do this with dataview in extjs here is Demo
Ext.application({
name: 'Fiddle',
launch: function () {
var items = Ext.create('Ext.data.Store', {
autoLoad: true,
storeId: 'item-store',
fields: ['name'],
proxy: {
type: 'ajax',
url: 'data.json',
reader: {
type: 'json',
rootProperty: ''
}
}
});
Ext.create('Ext.panel.Panel', {
title: 'DataView',
height: 620,
bodyPadding: 10,
viewModel: [{
stores: {
itemStore: {
type: 'item-store'
}
},
data: {
name: '',
desc: ''
}
}],
items: [{
xtype: 'dataview',
tpl: [
'<tpl for=".">',
'<div class="dataview-item">',
'<p>{desc}</p>',
'</div>',
'</tpl>'
],
itemSelector: 'div.dataview-item',
store: items
}],
renderTo: Ext.getBody()
});
}
});
I try to define a dataview from http://docs.sencha.com/extjs/4.1.3/#!/api/Ext.view.View to http://jsfiddle.net/JtTDH/
Here is my code
Ext.define('Example', {
extend: 'Ext.view.View',
tpl: new Ext.XTemplate(
'<tpl for=".">',
'<div style="margin-bottom: 10px;" class="thumb-wrap">',
'<img src="{src}" />',
'<br/><span>{caption}</span>',
'</div>',
'</tpl>'
),
itemSelector: 'div.thumb-wrap',
emptyText: 'No images available',
initComponent: function() {
var store = Ext.create('Ext.data.Store', {
id:'imagesStore',
fields: [
{ name:'src', type:'string' },
{ name:'caption', type:'string' }
],
data: [
{ src:'http://www.sencha.com/img/20110215-feat-drawing.png', caption:'Drawing & Charts' },
{ src:'http://www.sencha.com/img/20110215-feat-data.png', caption:'Advanced Data' },
{ src:'http://www.sencha.com/img/20110215-feat-html5.png', caption:'Overhauled Theme' },
{ src:'http://www.sencha.com/img/20110215-feat-perf.png', caption:'Performance Tuned' }
]
});
this.store = store;
this.callParent(arguments);
}
});
I think that's correct but that's not working. How to fix that thank.
Your code is fine, but you need to define a rendering target for it. For example, you could add renderTo: Ext.getBody() to your definition, and it will work correctly. See a working example here: https://fiddle.sencha.com/#fiddle/md
I has a question for Sencha Touch2? why the dataView don't display?Thanks a lot
This is the model file,app/model/Worklist.js
Ext.define('Geo.model.Worklist', {
extend: 'Ext.data.Model',
config: {
fields: [
{name: 'name', type: 'string'},
{name: 'url', type: 'string'}
]
}
});
this is the store file,
app/store/Worklist.js
/**
* this is the store file,
* app/store/Worklist.js
*/
Ext.define('Geo.store.Worklist',{
extend: 'Ext.data.Store',
config: {
model: 'Geo.model.Worklist',
autoLoad: true,
data:[
{name:'book1',url:'images/html51.jpg'},
{name:'book2',url:'images/html52.jpg'},
{name:'book3',url:'images/html53.jpg'},
{name:'book4',url:'images/html54.jpg'},
{name:'book5',url:'images/html55.jpg'}
]
}
});
this is the view file,
app/view/dashboard/Show.js
/**
* this is the view file,
* app/view/dashboard/Show.js
*/
Ext.define('Geo.view.dashboard.Show', {
extend: 'Ext.DataView',
xtype: 'dashboard-show',
//fullscreen: true,
scrollable: 'vertical',
store: 'Worklist',
itemTpl: new Ext.XTemplate(
'<tpl for=".">',
'<div style="font-size:12px;">',
'<img src="{url}" titel="{name}"><br />',
'{name}',
'</div>',
'</tpl>'
)
});
/**
* Main Controller file,Application.js
*/
config: {
refs: {
main: 'mainview',
editButton: '#editButton',
dashboards: 'dashboards',
showDashboard: 'dashboard-show',
editDashboard: 'dashboard-edit',
saveButton: '#saveButton'
}
}
var workStore = Ext.create('Geo.store.Worklist');
this.showDashboard = Ext.create('Geo.view.dashboard.Show');
this.showDashboard.setStore(workStore);
this.getMain().push(this.showDashboard);
I don't know why it can't display when i tap one of list item,anybody can help me?thanks a lot
I find the problem,i forget the config property in Geo.view.dashboard.Show
it should be like this
config:{
fullscreen: true,
scrollable: 'vertical',
store: 'Worklist',
itemTpl: new Ext.XTemplate(
'<tpl for=".">',
'<div style="font-size:12px;">',
'<img src="{url}" titel="{name}"><br />',
'</div>',
'</tpl>'
)
}
thanks a lot
I am having trouble adding a listener to different elements generated from an XTemplate.
var data = {
users: [
{ id: 1, name: 'Ed Spencer' },
{ id: 2, name: 'Abe Elias'}
]
};
var store = new Ext.data.JsonStore({
autoLoad: true,
data : data,
root: 'users',
fields: [
{name: 'id', type: 'int'},
{name: 'name', type: 'string'}
]
});
var template = new Ext.XTemplate(
'<tpl for=".">',
'<div class="holder">',
'<div class="notclicked">foobar</div>',
'<div class="name">{name}</div>',
'<div class="id">{id}</div>',
'</div>',
'</tpl>'
);
var newPanel = new Ext.Panel({
title: "test",
items: new Ext.DataView({
store: store,
tpl: template,
itemSelector: 'div.holder',
emptyText: 'No foo to display'
})
});
newPanel.render('targetDiv');
What I would like do is make a 'click' listener for clicks on the "name" div and a different one for the "id" div. But so far I can only make a 'click' listener for the who "holder" div. As an extention, I'd like the div 'notclicked'... to not respond to clicks. I've been racking against this a while. Can anyone give me a push in the right direction?
I'm using 3.4. I've put a fiddle on jsfiddle: http://jsfiddle.net/tatagatha/7SfCf/6/
use 'delegate' with a tight selector
http://www.sencha.com/blog/event-delegation-in-sencha-touch
Well looks like 3.4 had no delegation of events yet.
So you would have to manually check which node was clicked:
here is your fiddle back with selction working.
http://jsfiddle.net/dbrin/R29U5/
This goes on dataview:
listeners: {
click: {
fn: this.onClick,
scope: this
}
},
onClick: function(event, item, options) {
console.log(arguments);
if (item.className === "id") {
console.log("Id clicked: " + item.innerHTML);
alert("ID clicked: " + item.innerHTML);
}
}
Am having a tree view which gets its data from the tree store. But, however, am able to see only the small folders that gets displayed as part of a tree structure. the name of the nodes is not getting displayed, and when i try to print the record, it just says that its an empty string. Here is the code:
App/View
Ext.define('App.view.Hierarchy', {
alias: 'widget.hierarchy',
extend: 'Ext.panel.Panel',
initComponent: function () {
var config = {
xtype: 'panel',
border: false,
autoScroll: true,
baseCls: 'topMenu',
html: '<div class="PageTitle" style="width:600px;"><b>' + LANG.HIERARCHYT + '</b><br>' + LANG.HIERARCHYTxt + '<br>' + '</div>',
items: [{
xtype: 'toolbar',
border: false,
dock: 'top',
items: [{
xtype: 'button',
text: LANG.BTADDREG,
iconCls: 'icon-add-tb',
cls: 'tip-btn',
iconAlign: 'right',
action: 'addRegion',
id: 'ADDREGION'
}]
},
{
xtype: 'treepanel',
title: 'Hierarchy Tree',
id: 'hierarchyTree',
border: false,
alias: 'widget.hierarchyTree',
height: 1000,
viewConfig: {
enableDD: true,
plugins: {
ptype: 'treeviewdragdrop'
}
},
collapsible: false,
useArrows: true,
rootVisible: false,
store: Ext.create('Campus.store.HierarchyTree'),
displayField: 'Title',
multiSelect: false,
singleExpand: false,
}]
}
var holder = Ext.getCmp('center');
holder.remove(0);
holder.add(Ext.apply(config));
this.callParent(arguments);
}
});
Model
Ext.define('App.model.HierarchyTree', {
extend : 'Ext.data.Model',
fields : ['Title', 'ID', 'LevelID', 'ParentID']
});
Store
Ext.define('App.store.HierarchyTree', {
extend: 'Ext.data.TreeStore',
model: 'App.model.HierarchyTree',
storeID: 'HierarchyTree',
proxy: {
type: 'ajax',
url: 'data/Locations.aspx',
reader: {
},
actionMethods: {
create: 'POST',
read: 'POST',
update: 'POST'
},
extraParams: {
mode: 'HIERARCHYFULLLIST'
}
},
autoLoad: true
});
Controller
Ext.define('App.controller.Hierarchy', {
extend: 'Ext.app.Controller',
stores: ['Me', 'Regions', 'Areas', 'Towns', 'HierarchyTree'],
model: ['Me', 'Teams', 'Regions', 'User', 'HierarchyTree'],
views: ['App.view.Hierarchy', 'App.view.AddRegions'],
refs: [{
ref: 'myHierarchyTree',
selector: 'hierarchyTree'
}],
init: function () {
this.getHierarchyTreeStore().load();
this.control({
'button[action=addRegion]': {
click: this.addRegion
},
'#hierarchyTree': {
itemclick: this.itemclick
}
})
},
itemclick: function (view, record) {
console.log(record.get('Title'))
}
});
Also, the JSON thats being returned is:
{"text":".","children": [{"text":"Asia Pacific","id":"537","level":"1", "expanded":
false,"singleClickExpand":true,"children":[{"text":"Japan", "cls":"xtreenode-Level2-
Indent", "id":"538", "hl1":"537","level":"2","singleClickExpand":true, "expanded":
false, "children":[]},
Treepanel's display field defaults to text, which is ok with the json being returned, but the problem is the store model, which should include as fields the text, cls ..and other attributes you have in your json. Otherwise these will not be mapped to the records and you get empty text.
Ext.define('App.model.HierarchyTree', {
extend : 'Ext.data.Model',
fields : ['Title', 'ID', 'LevelID', 'ParentID', 'text', 'level','cls,....]
EDIT
You have modified the displayField to be the Title, but the json doesn't contain the title attribute. You have to simple fixes, either you modify the model if the text is actually the title. You can do this by setting a mapping to the field.
Ext.define('App.model.HierarchyTree', {
extend : 'Ext.data.Model',
fields : [{name:'Title',type:'string',mapping:'text'}, 'ID', 'LevelID', 'ParentID',]
This will cause the Title to be populated by the text values from the json.
Also you can remove the displayField config and then the text value will be applied, but this will still mean that the 'Title' value will be empty.
OK, finally got it:)
Set the displayfield in the tree to 'text'
displayfield : 'text'
And, Once again thank you nscrob
And one more question, could you please give me any guidance on how to open different views depending upon the level in the tree that is being clicked
This works.
if (record.get('level')==1)
{
this.erWin = Ext.create('App.view.EditRegions');
this.erWin.showWin();
}
else{
//open another_view
}