Results 1 to 4 of 4

Thread: What is the right way to create extra grid columns from one model field?

  1. #1
    Sencha User
    Join Date
    Sep 2009
    Posts
    35
    Answers
    2

    Default Answered: What is the right way to create extra grid columns from one model field?

    My model has a field like this:

    Code:
            { name: 'full_tag',         type: 'string' },
    The value of full_tag is a string like "<id>.<name>.<tag>". The last "<tag>" part may or may not exist.

    In my grid which is based on this model, I want to show three columns - one for <id>, one for <name> and one for <tag> (which may have empty values)

    I am able to add custom renderer functions for the columns like so:
    Code:
                { header: 'ID',
                    renderer: function(v,m,r) {
                        console.log(r);
                        return r.get('full_tag').split('.')[0];
                    }
                },
                { header: 'Name',
                    renderer: function(v,m,r) {
                        return r.get('full_tag').split('.')[1];
                    }
                },
                { header: 'Name',
                    renderer: function(v,m,r) {
                        var parts = r.get('full_tag').split('.');
                        if (parts.length == 3) { return parts[2]; }
                        else { return ""; }
                    }
                },
    While this works, it is clearly inefficient for the following reasons:
    1. I am doing splitting 3 times! I should do it once and use the split parts to render the columns
    2. Whenever I hide/show columns, change sorting, change grouping etc., the renderer functions run all over again.


    What is the right way to do this, such that the computation of these new columns (i.e. splitting) happens exactly once, and doesn't need to be re-done every time I change grid layout?

    UPDATE:
    I tried doing the splitting in model itself and it is much better! Now the fields are computed before they are consumed by the grid, so no need of custom grid column renderer functions.
    Code:
            { name: 'id',          type: 'string',
                convert: function(v,m) {
                    return m.get('full_tag').split('.')[0];
                }
            },
            { name: 'name',          type: 'string',
                convert: function(v,m) {
                    return m.get('full_tag').split('.')[1];
                }
            },
    But I still have to split the same string thrice. Is there a way to still split only once, and just create 3 new fields in one split operation?

  2. Figured it out. Instead of separate 'convert' for custom fields, I am creating the fields in the 'convert' of raw data field.

    Code:
            { name: 'full_tag',         type: 'string',
                convert: function(v,m) {
                    if(v) {
                        var parts = v.split('.',3);
                        m.data.id = parts[0];
                        m.data.name = parts[1];
                        m.data.tag = (parts.length==3) ? parts[2] : '-';
                        return v;
                    } else {
                        return '';
                    }
                }
            },

  3. #2
    Sencha User Animal's Avatar
    Join Date
    Mar 2007
    Location
    Bédoin/Nottingham
    Posts
    30,890
    Answers
    41

    Default

    Use the convert property of fields on your model to create custome fields Fromm the raw data.

    There are examples in the KitchenSink which do this.

  4. #3
    Sencha User
    Join Date
    Sep 2009
    Posts
    35
    Answers
    2

    Default

    Quote Originally Posted by Animal View Post
    Use the convert property of fields on your model to create custome fields Fromm the raw data.

    There are examples in the KitchenSink which do this.
    Perhaps you missed seeing my update on the ticket. I already switched from using renderer to convert function on fields. It works great. But I still have to call 'split' thrice on the same string. It would be more efficient if I could call split once (per record), and use the three parts to add the new custom fields per record.

    Is that possible? If not, what I have currently works just fine.

  5. #4
    Sencha User
    Join Date
    Sep 2009
    Posts
    35
    Answers
    2

    Default

    Figured it out. Instead of separate 'convert' for custom fields, I am creating the fields in the 'convert' of raw data field.

    Code:
            { name: 'full_tag',         type: 'string',
                convert: function(v,m) {
                    if(v) {
                        var parts = v.split('.',3);
                        m.data.id = parts[0];
                        m.data.name = parts[1];
                        m.data.tag = (parts.length==3) ? parts[2] : '-';
                        return v;
                    } else {
                        return '';
                    }
                }
            },

Similar Threads

  1. Replies: 1
    Last Post: 27 Sep 2014, 6:35 AM
  2. Replies: 3
    Last Post: 10 Oct 2013, 4:29 PM
  3. Replies: 10
    Last Post: 30 Apr 2012, 10:10 AM
  4. Replies: 6
    Last Post: 6 Feb 2012, 11:25 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •