S. Moreno
S. Moreno

Reputation: 536

extjs5 nested grid (table:subtable)

Actually i have two tables (Amenazas and Salvaguardas), the first table show Amenazas and the second Salvaguardas. Every Salvaguarda is associated to any Amenazas so i want to show in every row another sub-table (with the Salvaguardas table) using rowExpand with ExtJS5. Something similar to this example. How i insert another grid with the salvaguardaStore and only show the Salvaguardas related to the Amenaza of that row? (Somethin similar to Customers - Purchases) Here is the code:

 //MODEL
    // AMENAZAS
Ext.define('Amenazas', {
    extend: 'Ext.data.Model',
    fields: [ 'id', 'codigo', 'denominacion', 'a_impacto', 'a_riesgo','c_impacto', 'c_riesgo','i_impacto', 'i_riesgo', 'd_impacto', 'd_riesgo','t_impacto', 't_riesgo','total_impacto', 'total_riesgo',]
});

    // SALVAGUARDAS
Ext.define('Salvaguardas', {
    extend: 'Ext.data.Model',
    fields: [ 'id_amenaza','tipo','modo', 'codigo','denominacion', 'eficiencia',]
});

The DataStore (in my DB Amenaza.id = Salvaguardas.id_amenaza creating a 1:n association in every row)

// DATASTORE
    //AMENAZAS
var amenazaStore = Ext.create('Ext.data.Store', {
    model: 'Amenazas',
    data: [
        { id: 1, codigo: 'E.1', denominacion: 'Errores de los usuarios', a_riesgo: '0', c_riesgo: '0', i_riesgo: '0', d_riesgo: '1015875', t_riesgo: '0',  total_riesgo: '1015875'},
        { id: 2, codigo: 'E.5', denominacion: 'Deficiencias en la organización', a_riesgo: '0',  c_riesgo: '0',  i_riesgo: '0',  d_riesgo: '526750',  t_riesgo: '0',  total_riesgo: '526750'},
        { id: 3, codigo: 'E.8', denominacion: 'Escapes de información',  a_riesgo: '0', c_riesgo: '0',  i_riesgo: '0',  d_riesgo: '752500', t_riesgo: '0',  total_riesgo: '752500'},
        { id: 4, codigo: 'E.9', denominacion: 'Alteración accidental de la información',  a_riesgo: '0',  c_riesgo: '0',  i_riesgo: '0',  d_riesgo: '376250',  t_riesgo: '0',  total_riesgo: '376250'}
    ]
});

    //SALVAGUARDAS
    var salvaguardaStore = Ext.create('Ext.data.Store', {
    model: 'Salvaguardas',
    data: [
        { id_amenaza: 1, tipo: 'Correctiva', modo: 'Correctiva', codigo: 'corr-01', denominacion: 'correctiva 1', eficiencia: 'MB' }
 ]
});

And the Grid uses a rowExpander to expand every row and show information, i want to show inside the rowExpand the second table with his own Salvaguardas.

//GRIDPANEL
Ext.create('Ext.grid.Panel', {
    renderTo: 'example-grid',
    store: amenazaStore,
    width: 980,
    height: 790,
    title: '<bean:write name="informesAGRForm" property="nombreActivo"/>',
    plugins: [
              {
        ptype: 'rowexpander',
        rowBodyTpl : new Ext.XTemplate(
            '<p><b>Denominación:</b> {denominacion}</p>',
            '<p><b>Código:</b> {codigo}</p><br>',
            '<p><b>Riesgo Total:</b> {total_riesgo}</p>')
              }
    ],
    collapsible: false,
    animCollapse: false,
    columns: [
        {
            text: 'ID',
            hidden: true,
            hideable: false,
            dataIndex: 'id'
        },   
        {
            text: 'Codigo',
            width: 50,
            sortable: true,
            hideable: false,
            dataIndex: 'codigo'
        },          
        {
            text: 'Denominación',
            width: 150,
            dataIndex: 'denominacion',
        },
        {
            text: ' Autenticidad',
            flex: 1,
            dataIndex: 'a_riesgo'
        },
        {
            text: 'Confidencialidad',
            flex: 1,
            dataIndex: 'c_riesgo'
        },
        {
            text: 'Integridad',
            flex: 1,
            dataIndex: 'i_riesgo'
        },
        {
            text: 'Disponibilidad',
            flex: 1,
            dataIndex: 'd_riesgo'
        },
        {
            text: 'Trazabilidad',
            flex: 1,
            dataIndex: 't_riesgo'
        },
        {
            text: 'Total',
            flex: 1,
            dataIndex: 'total_riesgo'
        }]
    });

Thank you in advance

Upvotes: 1

Views: 3095

Answers (1)

Krzysztof
Krzysztof

Reputation: 16130

It is easy actually. You should include subtable plugin in your project (you can download it from http://dev.sencha.com/extjs/5.0.0/examples/ux/grid/SubTable.js). Then configure it like so:

{
    ptype: "subtable",
    headerWidth: 24,
    columns: [{
        text: 'id_amenaza',
        dataIndex: 'id_amenaza',
        width: 100
    }, {
        width: 100,
        text: 'codigo',
        dataIndex: 'codigo'
    }],
    getAssociatedRecords: function(record) {
        var result = Ext.Array.filter(
            salvaguardaStore.data.items,
            function(r) { return r.get('id_amenaza') == record.get('id'); }
        );
        return result;
    }
}

The most important bit is getAssociatedRecords override. You must return array of records to display, so you can basically filter salvaguardaStore data.

Working sample: http://jsfiddle.net/7czs02yz/11/

Upvotes: 4

Related Questions