Search code examples
odatasapui5

SAPUI5 Auto Bind Smart Table with filters


I've a smart table which bonded to an oData service with auto bind enabled. And currently, it returns all the data for the entityset.

What I need is to filter data when loading the data from oData service. I've tried by adding filter in the controller, but it is not working.

View

<smartTable:SmartTable id=mytable" entitySet="SampleDetail"  tableType="ResponsiveTable"
                    useExportToExcel="false" beforeExport="onBeforeExport" useVariantManagement="false" useTablePersonalisation="true"
                    header="{i18n>tickets_table_header}" showRowCount="true" persistencyKey="ticketsSmartTable_persis" enableAutoBinding="true"
                    demandPopin="true" class="sapUiResponsiveContentPadding">
</smartTable:SmartTable>

And controller js

var serviceURL = this.getConfiguration("myDestination");
            serviceURL = serviceURL + "sample.xsodata";
            var oModel, oView, that = this;
            var filtersDef = [];
            filtersDef.push(new Filter("STATUS", sap.ui.model.FilterOperator.NE, "D"));

            oView = this.getView();
            oModel = new sap.ui.model.odata.v2.ODataModel(serviceURL, {
                useBatch: false
            });


            oModel.read("/SampleDetail", {
                async: true,
                success: function(e) {
                    that.setModel(oModel);

                },
                error: function(e) {
                    oModel.setData({

                    });
                },
                filters: filtersDef
            });

Solution

  • You can use beforeRebindTable event of smart table

                    <smartTable:SmartTable 
    ...
                    beforeRebindTable="onBeforeRebindTable"
    ...
                </smartTable:SmartTable>
    

    and on method you change filters.

    onBeforeRebindTable: function(oSource){
        var binding = oSource.getParameter("bindingParams");
        var oFilter = new sap.ui.model.Filter("STATUS", sap.ui.model.FilterOperator.NE, "D");
        binding.filters.push(oFilter);
    }