Skip to Content

Deleting null items from ComboBox binded from JSONModel

I am getting an array of objects through an API. I am binding a field called 'location' mentioned in each object to a ComboBox. Some of the fields are null and I want to remove that from ComboBox. Please suggest how to do that.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

3 Answers

  • Best Answer
    Posted on Feb 18, 2019 at 02:59 PM
    <ComboBox items="{
      path: '/myCollection',
      filters: [{
        path: 'location',
        operator: 'NE',
        value1: null
      }]
    }">
      <core:ListItem xmlns:core="sap.ui.core"
        key="{...}"
        text="{location}"
      />
    </ComboBox>

    When binding items, filter all objects out which have null in their "location" property.

    Add a comment
    10|10000 characters needed characters exceeded

    • Hi Swapnil Garg

      The path within filter object and the path within binding serve different purposes. In the filter, the syntax model>property is invalid. The path there awaits just the property name (or in other cases sequence of property names navigating with "/" in between) without the model name. So it should be:

      <ComboBox id="location" items="{
        path: 'Model>/value',
        filters: [{
          path: 'LocationFieldNew',
          operator: 'NE',
          value1: null
        }]
      }">
        <core:Item xmlns:core="sap.ui.core"
          key="{Model>ID}"
          text="{Model>LocationFieldNew}"
        />
      </ComboBox><br>
  • Posted on Feb 18, 2019 at 01:58 PM

    Hi Swapnil,

    You could either filter the data in your model (as suggested by Mahesh) or you apply a filter in the aggregation binding. This way your "null data" remains in your model in case you want to keep the records and send them back to the backend.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Feb 18, 2019 at 01:42 PM

    Hi Swapnil,

    You need to pass the filtered data to the combo box. Use JavaScript filter function to remove null or space objects.

    https://stackoverflow.com/questions/281264/remove-empty-elements-from-an-array-in-javascript

    BR

    Mahesh

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.