EXT JS 4 use an model association to render a grid display value

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-03 14:59:48

I managed to get the association lookup working by using a callback function but found it much easier to simply do the lookup from the store myself.

Step One

I moved the Proxy from the InvoiceStatus store and onto the InvoiceStatus model and made the InvoiceStatus store autoload.

Step Two

I changed the render method of the Status column to lookup the display name from the InvoiceStatus store like so.

renderer: function(value, metaData, record, rowIndex, colIndex, store, view) {
    var store = Ext.data.StoreManager.lookup('InvoiceStatus');
    return store.getById(value).get('name');
},

This proved to be a much simpeler solution.

James

It looks like you need to set the belongsTo association on the InvoiceStatus child model. You would think that defining the association in one direction would automatically create the association in the other direction, but apparently that's not the case and you must define the association on both parent and children. See here for a more detailed explanation: Why isn't my ExtJS Store Association Working

Your hasOne should be like this:

hasOne: {
    name:           'status',
    instanceName:   'status',
    associationKey: 'status',
    model:          'MyApp.model.InvoiceStatus',
    foreignKey:     'invoice_status_id',
    getterName:     'getStatus',
    setterName:     'setStatus'
}

The following patch for ExtJS 4.2.2 will let you set dataIndex: 'status.name' without any additional renderers. Grid will show everything OK but sorting will not work.

This solution does not work with async (lazy) status load.

Ext.view.Table.prototype.renderCell = function(column, record, recordIndex, rowIndex, columnIndex, out) {
    var me = this,
        selModel = me.selModel,
        cellValues = me.cellValues,
        classes = cellValues.classes,
        // fieldValue = record.data[column.dataIndex]; // patched
        fieldValue = null,
        cellTpl = me.cellTpl,
        fullIndex, value, clsInsertPoint;

    // Patch start
    if (column.dataIndex && column.dataIndex.indexOf('.') > 0) {
        var associationParts = column.dataIndex.split('.'),
            v = record;

        for (var i = 0; i < associationParts.length-1; i++) {
            v = v['get' + associationParts[i].charAt(0).toUpperCase() + associationParts[i].slice(1)]();
        }
        fieldValue = v.get(associationParts[associationParts.length-1]);
    }
    else {
        fieldValue = record.data[column.dataIndex];
    }
    // Patch end

    cellValues.record = record;
    cellValues.column = column;
    cellValues.recordIndex = recordIndex;
    cellValues.rowIndex = rowIndex;
    cellValues.columnIndex = columnIndex;
    cellValues.cellIndex = columnIndex;
    cellValues.align = column.align;
    cellValues.tdCls = column.tdCls;
    cellValues.innerCls = column.innerCls;
    cellValues.style = cellValues.tdAttr = "";
    cellValues.unselectableAttr = me.enableTextSelection ? '' : 'unselectable="on"';

    if (column.renderer && column.renderer.call) {
        fullIndex = me.ownerCt.columnManager.getHeaderIndex(column);
        value = column.renderer.call(column.scope || me.ownerCt, fieldValue, cellValues, record, recordIndex, fullIndex, me.dataSource, me);
        if (cellValues.css) {
            // This warning attribute is used by the compat layer
            // TODO: remove when compat layer becomes deprecated
            record.cssWarning = true;
            cellValues.tdCls += ' ' + cellValues.css;
            delete cellValues.css;
        }
    } else {
        value = fieldValue;
    }
    cellValues.value = (value == null || value === '') ? '&#160;' : value;

    // Calculate classes to add to cell
    classes[1] = column.getCellId();

    // On IE8, array[len] = 'foo' is twice as fast as array.push('foo')
    // So keep an insertion point and use assignment to help IE!
    clsInsertPoint = 2;

    if (column.tdCls) {
        classes[clsInsertPoint++] = column.tdCls;
    }
    if (me.markDirty && record.isModified(column.dataIndex)) {
        classes[clsInsertPoint++] = me.dirtyCls;
    }
    if (column.isFirstVisible) {
        classes[clsInsertPoint++] = me.firstCls;
    }
    if (column.isLastVisible) {
        classes[clsInsertPoint++] = me.lastCls;
    }
    if (!me.enableTextSelection) {
        classes[clsInsertPoint++] = me.unselectableCls;
    }
    if (cellValues.tdCls) {
        classes[clsInsertPoint++] = cellValues.tdCls;
    }
    if (selModel && selModel.isCellModel && selModel.isCellSelected(me, recordIndex, columnIndex)) {
        classes[clsInsertPoint++] = (me.selectedCellCls);
    }

    // Chop back array to only what we've set
    classes.length = clsInsertPoint;

    cellValues.tdCls = classes.join(' ');

    cellTpl.applyOut(cellValues, out);

    // Dereference objects since cellValues is a persistent var in the XTemplate's scope chain
        cellValues.column = null;
};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!