Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExtJS 4 Show data from multiple stores in grid

Tags:

grid

extjs

extjs4

I have 2 models - for example - Users and Orders

Ext.define('AM.model.User', {
    extend: 'Ext.data.Model',
    fields: ['id', 'username', 'firstName', 'lastName', 'state', 'city'],

    associations: [
    {
            type: 'hasMany', 
            model: 'Order', 
            name: 'orders'
    },],        
});

Ext.define('AM.model.Order', {
    extend: 'Ext.data.Model',
    fields: ['id', 'userId', 'date', 'description', 'value'],    
    belongsTo: 'User',   
});

and their stores. I'm looking for a way to display data from both stores in grid. (so my columns would be firstName, lastName, orderDate, orderDescription, orderValue...

What is the proper way to display them?

Thanks.

like image 837
user779612 Avatar asked Jun 09 '11 11:06

user779612


2 Answers

You should do this with your server side code and get this all data into a single store.

like image 200
Kunal Avatar answered Sep 19 '22 17:09

Kunal


If you want to do this with associations, you need to define a renderer for your grid column.
Like so:

{
        text: "orderDescription",
        renderer: function(value,meta,record){//1.
       //var orderStore = record.orderStore;//2a.
       //var orderList = ordersStore.data.items;//2b.
       var orderList = record.orders().data.items; //3.This line is the same as 2 the lines above(2a,2b).
       var order = orderList[0]; //4. TODO..loop through your list. 
       var description = order.data.description; //5. 
       return description ;
 },


I will try to explain for anyone who wants to do it this way.

  1. The third parameter is the current record in your 'User' Store being renderd for the grid. The first two just need to be there, to receive record. Leaving them out will not work.

  2. 1a/1b. I left these there to demonstrate how the association works. Basically, each record in your 'User' Store gets its own corresponding 'ordersStore'. It will be called 'ordersStore', because you put in your association [name: 'orders'].
  3. The orders() method is automatically created, again based on the name 'orders'. This just returns the orderStore. The store contains a field data -> which contains a field items. items is the actual list of orders.
  4. Now you have access to your list of orders. You can loop trough the orders now. Or if you have only one order, just the first one.
  5. Your order again contains a field data which contains your actual data.

like image 25
Renato H. Avatar answered Sep 22 '22 17:09

Renato H.