Hybrid View
-
5 Dec 2012 8:51 PM #1
How do I use calculations on JSON loaded data + rewrite some of the fields?
How do I use calculations on JSON loaded data + rewrite some of the fields?
I'm just wondering how do I the following:
1) do calculations on JSON data fields before outputting them in the grid? for example, if I have Spend and Revenue fields in the example below, how do I fill the ROI field in the grid with Revenue/Spend calculation?
2) how can I wrap links around some of these entries? for example, again, below, how can I get "a href=..." wrapped around PubName with some of the variables from the JSON feed itself, so have something like <a href="PubId">PubName</a> output instead of just PubName in the grid?
Code:Ext.onReady(function(){ var data = <?php echo $json; ?>; Ext.define('Placements', { extend: 'Ext.data.Model', fields: [ {name: 'PubId', type: 'int'}, {name: 'PubName', type: 'string'}, {name: 'Imp', type: 'int'}, {name: 'Spend', type: 'double'}, {name: 'Revenue', type: 'double'}, ] }); var store = Ext.create('Ext.data.Store', { autoLoad: true, data : data, model: 'Placements', proxy: { type: 'memory', reader: { type: 'json', //record: 'data_type' } } }); Ext.create('Ext.Panel', { id: 'junk', renderTo: 'grid-table', width: 1024, height:800, layout: 'vbox', items: [ { xtype: 'grid', title: 'Placements', width: 1024, height: 800, store: store, viewConfig: { emptyText: 'No data' }, columns: [ { header: 'Placement', dataIndex: 'PubName'}, { header: 'Impressions', dataIndex: 'Imp'}, { header: 'Spend', dataIndex: 'Spend'}, { header: 'Revenue', dataIndex: 'Revenue'}, { header: 'ROI', dataIndex: ''}, ] } ] }); })
-
5 Dec 2012 8:59 PM #2
or should this be done directly in the JSON feed/source itself?
is there any difference in speed if the JSON feed is rewritten in PHP vs. the calculations being done on the frontend, through ExtJS?
-
5 Dec 2012 9:04 PM #3
Evan Trimboli
Sencha Developer
Twitter - @evantrimboli
Don't be afraid of the source code!
-
5 Dec 2012 9:26 PM #4
#1 - I kinda get it... I can assign it to Model fields, but how do I assign that actual converted value to the columns in the grid now for each entry?
-
5 Dec 2012 9:42 PM #5
Create a new field in your model called ROI, then specify the convert method, it will just act as a normal field.
Evan Trimboli
Sencha Developer
Twitter - @evantrimboli
Don't be afraid of the source code!
-
5 Dec 2012 10:22 PM #6
did... got it working, kinda.
now trying to figure out how to make the new ROI field sortable... have this in Ext.data.Model:
{name: 'ROI', type: 'int', sortable: true, sortType: 'int'}
and this in Column view:
{ header: 'ROI', renderer: returnROI, width: 50, sortable: true, sortType: 'int'},
(calling helper function returnROI that returns the calculated ROI)
ideas?
current setup isn't sortable...


Reply With Quote