0
votes

I'm using a grid with Ext.grid.plugin.CellEditing. On the grid, there's a combobox and a date. The date is syncing correctly, but the combobox is trying to save the id of the combobox in a String description field.

My combobox has fields codeChecklistItemStatusId (foreign key), and codeChecklistItemStatus (display field for user).

When I edit the Checklist Item Status field, the field I've chosen for display field (dataIndex: 'codeChecklistItemStatus') is updated to be an integer. Upon saving, the field codeChecklistItemStatus gets changed from a String description to the new id value, and the field I want to change codeChecklistItemStatusId still has the original id value.

All the examples I've found online have a string value being saved instead of an id value. Is there any way to change the codeChecklistItemStatusId field instead of codeChecklistItemStatus?

I've put dataIndex: 'codeChecklistItemStatusId' in my grid and the grid then displays numbers to the user instead of a description. However, when saving the correct field codeChecklistItemStatusId is updated correctly.

The ChecklistItem store:

Ext.define('gui.store.IspIntakeChecklistItem',
{
extend: 'Ext.data.Store',
model: 'gui.model.IspIntakeChecklistItem',
root: 'data',
proxy:
{
    type: 'ajax',
    api:
    {
        read: 'ispIntakeChecklistItem/search',
        update: 'ispIntakeChecklistItem/quickEdit'
    },
    actionMethods:
    {
        read: 'POST'
    },
    reader:
    {
        type: 'json',
        rootProperty: 'data'
    },
    writer:
    {
        type: 'json',
        allowSingle: false,
        writeAllFields: true
    }
}
});

The CheckListItem model:

Ext.define('gui.model.IspIntakeChecklistItem',
{
extend: 'Ext.data.Model',
fields: [
        {
            name: 'id'
        },
        {
            name: 'codeChecklistItemStatusId'
        },
        {
            name: 'codeChecklistItemStatus'
        },
        {
            name: 'followUpDate'
        }
]
});

The store of the combobox:

Ext.define('gui.store.maint.CodeChecklistItemStatus',
{
extend: 'Ext.data.Store',
remoteSort: true,
pageSize: gui.data.Constants.MAX_CODE_RESULTS_IN_GRID,
model: 'gui.model.maint.CodeChecklistItemStatus',
root: 'data',
autoLoad: true,
proxy:
{
    type: 'ajax',
    api:
    {
        read: 'codeChecklistItemStatus/search',
        update: 'codeChecklistItemStatus/updateSortOrder'
    },
    actionMethods:
    {
        read: 'POST'
    },
    reader:
    {
        type: 'json',
        rootProperty: 'data'
    }
}
});

The model of the combobox:

Ext.define('gui.model.maint.CodeChecklistItem',
{
extend: 'Ext.data.Model',
fields: [
        {
            name: 'id'
        },
        {
            name: 'activeFlag'
        },
        {
            name: 'description'
        },
        {
            name: 'sortOrder'
        },
        {
            name: 'createDate'
        },
        {
            name: 'createUser'
        },
        {
            name: 'lastUpdDate'
        },
        {
            name: 'lastUpdUser'
        }
]
});

Here are a few of the fields in the grid that has CellEditing enabled:

         {
            text: 'Checklist Item Status',
            itemId: 'codeChecklistItemStatusGridFld',
            dataIndex: 'codeChecklistItemStatus',
            width: 200,
            editor: {
                xtype: 'combobox',                    
                queryMode: 'local',
                valueField: 'id',
                displayField: 'description',
                typeAhead: true,
                forceSelection: true,
                store: 'maint.CodeChecklistItemStatus',
                allowBlank: false
            },
            tdCls: 'editableCell'
        },
        {
            text: 'Follow Up Date',
            dataIndex: 'followUpDate',
            width: 150,
            editor: {
                xtype: 'datefield'
            },
            tdCls: 'editableCell'
        }

My controller function:

quickEditChecklistItem: function(editor, e, eOpts) {
    if (e.originalValue !== e.value) {
        debugger;
        var rec = e.record;
        var store = this.getIspIntakeChecklistItemStore();
        //store.add(rec); 
        store.sync({
            scope: this,
            success: function(batch, options) {

                Ext.create('gui.widget.Notify').success('Checklist Item saved successfully');
            },
            failure: function(batch, options) {
                Ext.create('gui.widget.Notify').failure('failure');
            }
        });
    }
}
1

1 Answers

0
votes

A colleague of mine figured out how to do this. On the grid all I need to define is the dataIndex for the combobox column:

{
    xtype: 'editableComboboxGridColumn',
    dataIndex: 'codeChecklistItemStatusId'
},

He created a reusable xtype that will handle all the messy code:

Ext.define('gui.widget.EditableComboboxGridColumn', {
extend: 'Ext.grid.column.Column',

xtype: 'editableComboboxGridColumn',    
width: 200,
tdCls: 'editableCell',

//Required property: dataIndex
initComponent: function() 
{
    var nameCamel = Utilities.toCamelCase(this.dataIndex);
    nameCamel = Utilities.removeId(nameCamel);

    if (this.text == ' ')
        this.text = Utilities.fieldNameToLabel(this.dataIndex);

    if (!this.itemId)
        this.itemId = Utilities.removeId(this.dataIndex) + "GridFld";

    if (!this.store)
        this.store = 'maint.' + nameCamel;

    if (!this.editor)
    {
        this.editor =
        {
            xtype: 'combobox',                    
            queryMode: 'local',
            valueField: (this.valueField ? this.valueField : 'id'),
            displayField: (this.displayField ? this.displayField : 'description'),
            typeAhead: true,
            forceSelection: true,
            store: this.store,
            allowBlank: false
        }
    }

    if (!this.renderer)
    {
        this.renderer = function (value, metaData, record)
        {
            var editor = metaData.column.getEditor(record);
            var storeRecord = editor.store.getById(value);
            if(storeRecord)
                return storeRecord.data[editor.displayField];
            else
                return null;
        }
    }

    this.callParent(arguments);
}
});

Other code in Utilities.js:

Ext.define('gui.Utilities',
{
alternateClassName: 'Utilities',
singleton: true,

fieldNameToLabel: function(name)
{
    var nameProper = '';

    for (var i = 0; i < name.length; i++) {
        var character = name.substr(i, 1);
        if (character.toUpperCase() == character)
            nameProper += ' ';

        if (nameProper.length == 0)
            nameProper += character.toUpperCase();
        else
            nameProper += character;
    }
    if (nameProper.indexOf('Code ') == 0)
        nameProper = nameProper.substring(5);

    if (nameProper.length == nameProper.indexOf(' Id') + 3)
        nameProper = nameProper.substring(0, nameProper.length - 3);

    var regex = / Flag$/
    if (regex.test(nameProper))
        nameProper = nameProper.replace(regex, '');

    if (nameProper.indexOf(' Upd ') > -1)
        nameProper = nameProper.replace(' Upd ', ' Update ');

    return nameProper;

},

toCamelCase: function(name)
{
    return name.substring(0, 1).toUpperCase() + name.substring(1);
},

removeId: function(name)
{
    var regex = /\s?Id$/;
    if (regex.test(name))
        name = name.replace(regex, '');

    return name;
}
});

We originally started with this code before developing the generic xtype:

renderer : function (value, metaData, record) {             
    var editor = metaData.column.getEditor(record);
    var storeRecord = editor.store.getById(value);
    if(storeRecord)
        return storeRecord.data[editor.displayField];
    else
        return null;
}