Search code examples
uitableviewdata-bindingformattingsapui5number-formatting

SAPUI5: How to add custom functionality to column menu of ui.table?


I'd like to give my users the chance to reformat table cells after data has been loaded into the table. I thought a neat way to do this, is adding the functionality to the column menu. So when clicking on a table column, the menu appends with the standard "filtering, sorting" but also has a row called "formatting" which provides a few options (E.g. format a numeric cell from 55555,55 to 55.555,55)

Unfortunately I was unable to find a way to add a new row to my column menu. My sorting and filtering is added like this:

oTable.bindColumns("/columns", function(index, context) {
    var columnName = context.getObject().columnId;
    return new sap.ui.table.Column({
        label:columnName,
        template: columnName,
        sortProperty: columnName,
        filterProperty: columnName,
    });
});

How do I add new lines/functionalities to the column menu?

Update

This is how my table looks like in xml view:

 <table:Table id="uploadData" visibleRowCountMode="Auto" rowSelectionChange="tableRowSelectionChange" enableCellFilter="true" fixedColumnCount="0" enableBusyIndicator="true" customData="{Type: 'sap.ui.core.CustomData', key:'table-style-type', value:'custom-styled',  writeToDom: true }">
        <table:extension>
                <m:Button icon="sap-icon://download" press="onDataExportXLS" align="Right" />
        </table:extension>

        <table:columns>
                <!-- Columns dynamically created in controller -->
        </table:columns>
        <table:rows>
                <!-- Rows created in controller -->
        </table:rows>
</table:Table>

Solution

  • The sap.ui.table.Column has an aggregation called menu just for this. It accepts any custom menu items which are shown when clicking on a Column. This aggregation takes sap.ui.unified.Menu control.

    In the select function of the MenuItem aggregation, you can write the function to handle what needs to be done when the menu item is selected

    sap.ui.table.Column documentation

    sap.ui.unified.Menu documentation

    Check it in this sample here and its code here, click on the Quantity column and you will see a My custom menu entry

    A snippet of the code here in XML,

    <Column id="quantity" width="6rem" hAlign="End" sortProperty="Quantity">
        <m:Label text="Quantity" />
        <template>
            <m:Label text="{ path: 'Quantity', type: 'sap.ui.model.type.Integer'}" />
        </template>
        <menu>
            <u:Menu ariaLabelledBy="quantity">
                <u:items>
                    <u:MenuItem text="My custom menu entry" select="onQuantityCustomItemSelect" />
                    <u:MenuItem text="Sort" select="onQuantitySort" icon="sap-icon://sort" />
                </u:items>
            </u:Menu>
        </menu>
    </Column>
    

    The code in JS,

    var oColumn = new sap.ui.table.Column({
        label: "Some column Name",
        menu: new sap.ui.unified.Menu({
            items: [new sap.ui.unified.MenuItem({
                text: "My custom menu",
                select: function (oEvent) {
                    pressEventOnItem(oEvent);
                })
            ]
        })
    })