SAPUI5 in-table search (Uncaught Error: Only “Stri

2019-08-14 17:38发布

问题:

I have a SAPUI5 table with both String value data (e.g. names) and integer data (e.g. IDs). When I use the in-table search options for strings, it works perfectly.

When I try to search for part of an ID, it throws the following error:

(Uncaught Error: Only "String" values are supported for the FilterOperator: "Contains".)

I want to search through my integers as though they were strings

Attempt 1:

var oTextView = new sap.ui.commons.TextView( {
            text : {
                path : "id",
                formatter : function(oContext) {
                    if (oContext) {
                        return oContext.toString();
                    } else
                        return;
                }
                }
            });

Attempt 2:

var oTextView = new sap.ui.commons.TextView( {
            text : {
                path : "id",
                type : new sap.ui.model.type.String(),
                }
            });

Attempt 3:

combining previous attempts with this (result is I cannot open in-table search for this column at all anymore):

oColumn.setFilterProperty(sap.ui.model.type.String())
oColumn.setSortProperty(new sap.ui.model.type.String())

Edit: view/controller

view:

var oTable = new sap.ui.table.Table();
oTable.setModel(sap.ui.getCore().getModel("myModel"));
oTable.bindRows("/myPath");

var oTextView = new sap.ui.commons.TextView();
            oTextView.bindProperty("text", "myProperty");

var oColumn =   var oColumn = new sap.ui.table.Column({
label : new sap.ui.commons.Label( {
                text : "My Column"
            }),
            template : oTextView,
            sortProperty : "myProperty",
            filterProperty : "myProperty",
});
oTable.addColumn(oColumn);

Controller:

var oModel = new sap.ui.model.json.JSONModel(MyData); 
sap.ui.getCore().setModel(oModel, "myModel");

回答1:

I had the same problem and casting to string solved it. However I will file a bug report, because the filter should also work with numbers. Especially because the sorting is only working as expected with numbers. As a work around, I did the following:

  1. Cast to string via: var my_string = my_num.toString(); This seems better, then the implicit cast via the concatination proposed above.
  2. Use the string value in the binding for the filter
  3. Use the number value in the binding for the sorting

Here some pseudo code, because someone asked for it. The string value is only added to the model for the sorting:

var your_data_model = [{int_value = 111},{int_value=222}];
your_data_model[0].string_value = "111"; //add a string value for filter
your_data_model[1].string_value = "222"; //add a string value for filter

//in your table object add the sorting as usal on the column level
var oColumn = new sap.ui.table.Column({
    label : oLabel,
    template : oObject
});
oColumn.setSortProperty("int_value"); //sort well for numbers
oColumn.setFilterProperty("string_value"); //filter worlds for string


回答2:

Try using:

var oTextView = new sap.ui.commons.TextView( {
    text : "" + "{id}" + ""
});

This way your Integers will be given out as strings