cancel
Showing results for 
Search instead for 
Did you mean: 

Filters are not getting added dynamically to view settings dialog control in sapui5

former_member127164
Participant
0 Kudos

Hi friends,

I am trying to add filters dynamically to sap.m.ViewSettingsDialog, but the filters and filter items are not getting added. And there was no error that is coming up in browser console to analyze this issue.

Can somebody help me on this issue.

Here I have added my application code below. So please take a look into the code.

FilterDialog.fragment.xml

<core:FragmentDefinition xmlns="sap.m" xmlns:core="sap.ui.core"> <ViewSettingsDialog id="FilterDialog" confirm="handleConfirm"> <sortItems> <ViewSettingsItem text="Field 1" key="1" selected="true" /> <ViewSettingsItem text="Field 2" key="2" /> <ViewSettingsItem text="Field 3" key="3" /> </sortItems> <groupItems> <ViewSettingsItem text="Field 1" key="1" selected="true" /> <ViewSettingsItem text="Field 2" key="2" /> <ViewSettingsItem text="Field 3" key="3" /> </groupItems> <filterItems> <!-- <ViewSettingsFilterItem text="Field1" key="1"> <items> <ViewSettingsItem text="Value A" key="1a" /> <ViewSettingsItem text="Value B" key="1b" /> <ViewSettingsItem text="Value C" key="1c" /> </items> </ViewSettingsFilterItem> --> </filterItems> </ViewSettingsDialog> </core:FragmentDefinition>

Controller

addFilterToDialog: function(filters){

filters = [ { "key" : "Emp Id", "values" : ["3001","3002","3003"] }, { "key" : "Department", "values" : ["Electrical","Accounts","Mechanical"] } ];

var filterDialog = this._oFilterDialog;

this._oFilterDialog.removeAllFilterItems();

this._oFilterDialog.destroyFilterItems();

for(var i=0; i<filters.length; i++){ var oFilter = new sap.m.ViewSettingsFilterItem({ text: filters[i].key, key: filters[i].key }); for(var j=0;j<filters[i].values.length; j++){ var oFilterItem = new sap.m.ViewSettingsItem({text:filters[i].values[j], key:filters[i].values[j]}); oFilter.addItem(oFilterItem); } filterDialog.addFilterItem(oFilter); }

}

Here I am trying to add the filters dynamically based on backend data.But here I have used the sample data i.e., filters array.

Thanks in advance.

Accepted Solutions (1)

Accepted Solutions (1)

maheshpalavalli
Active Contributor

Hi M SUNEEL KUMAR REDDY

Its better to put your code inside the code display box as it is not readable at all.

Secondly, its better, preferable and suggested by SAP to use aggregation bindings for these kind of scenarios..

I've used your data and created the filters via binding, see the code below:

// COntroller level, i've created the json model in this format:

			var filters = [{
				"key": "Emp Id",
				"values": [{
					"key": "3001"
				}, {
					"key": "3002"
				}, {
					"key": "3003"
				}]
			}, {
				"key": "Department",
				"values": [{
					key: "Electrical"
				}, {
					key: "Accounts"
				}, {
					key: "Mechanical"
				}]
			}];
			this.getView().setModel(new sap.ui.model.json.JSONModel({
				filters: filters
			}), "filterDiloag");

// Now in the view, I've made this changes to fetch the filters from the binding:
// 

<core:FragmentDefinition xmlns="sap.m" xmlns:core="sap.ui.core">
//-> Comment here adding the aggregation filteritems
	<ViewSettingsDialog id="FilterDialog" confirm="handleConfirm" filterItems="{filterDiloag>/filters}">
		<sortItems>
			<ViewSettingsItem text="Field 1" key="1" selected="true"/>
			<ViewSettingsItem text="Field 2" key="2"/>
			<ViewSettingsItem text="Field 3" key="3"/>
		</sortItems>
		<groupItems>
			<ViewSettingsItem text="Field 1" key="1" selected="true"/>
			<ViewSettingsItem text="Field 2" key="2"/>
			<ViewSettingsItem text="Field 3" key="3"/>
		</groupItems>
		<filterItems>
//-> Comment here adding the aggregation and bindings
			<ViewSettingsFilterItem key="{filterDiloag>key}" text="{filterDiloag>key}" items="{filterDiloag>values}">
				<items>
					<ViewSettingsItem text="{filterDiloag>key}" key="{filterDiloag>key}"></ViewSettingsItem>
				</items>
			</ViewSettingsFilterItem>
			<!-- <ViewSettingsFilterItem text="Field1" key="1"> <items> <ViewSettingsItem text="Value A" key="1a" /> <ViewSettingsItem text="Value B" key="1b" /> <ViewSettingsItem text="Value C" key="1c" /> </items> </ViewSettingsFilterItem> -->
		</filterItems>
	</ViewSettingsDialog>
</core:FragmentDefinition>
smithshah
Explorer
0 Kudos

What if I don't want to manually add each and every ViewSettingsItem for sorting and rather use some existing JSONModel and bind it here?? How can we achieve that??

Answers (0)