Show TOC

Sorting and Filtering in JSON ModelsLocate this document in the navigation structure

If you use a JSON model for data binding, sorting and filtering is implemented in JavaScript because the data is available on the client.

Context

You can use custom sorting and filtering methods in the JSON model. To define custom methods, set the fnCompare method on the Sorter object or the fnTest method on the filter object after creating it.

Procedure

  1. The fnTest method of the Filter gets the value to test as the only parameter and returns, whether the row with the given value should be filtered or not.
    var oFilter = new sap.ui.model.Filter("property");
    oFilter.fnTest = function(value) {
        return (value > 100); 
    };
  2. The fnCompare method of the Sorter gets the two values to compare as parameters and returns -1, 0 or 1, dependent which of the two values should be ordered before the other:
    var oSorter = new sap.ui.model.Sorter("property");
    oSorter.fnCompare = function(value1, value2) {
        if (value1 < value2) return -1;
        if (value1 == value2) return 0;
        if (value1 > value2) return 1;
    };