如何修改SmartFilterBar过滤器?移除空默认值过滤项
解决SmartFilterBar空默认值过滤的问题
嘿,我之前也踩过这个坑!你之前的代码没生效,主要是时机和操作逻辑没踩对点,试试这个可行的方案:
onFilterInit: function(oEvent) { const oSmartFilterBar = oEvent.getSource(); const oAppViewModel = this.getModel("appView"); const sPlantId = oAppViewModel.getProperty("/Role/Plantid"); // 获取Plant对应的过滤配置项 const oPlantFilterConfig = oSmartFilterBar.getControlConfigurationById("plantFilterBox"); if (!sPlantId) { // 销毁默认过滤值配置 oPlantFilterConfig.destroyDefaultFilterValues(); // 彻底移除已经加载到过滤条的Plant过滤项,避免空值被提交 oSmartFilterBar.removeFilter("Plant"); // 手动触发搜索,确保过滤条件生效 oSmartFilterBar.search(); } else { // 如果有值,确保默认值正确设置(可选,根据你的业务场景调整) const oDefaultFilter = oPlantFilterConfig.getFilterValues()[0]; if (oDefaultFilter) { oDefaultFilter.setLow(sPlantId); } oSmartFilterBar.search(); } }
关键要点说明:
- 准确判断值是否为空:直接从
appView模型获取Plantid,比操作控件更可靠,能避免控件初始化时序带来的问题。 - 彻底清理过滤项:
destroyDefaultFilterValues()只是移除了默认值的配置,但SmartFilterBar可能已经把空值加载到过滤集合中了,所以必须调用removeFilter("Plant")彻底移除这个过滤参数。 - 手动触发搜索:修改配置后调用
search(),强制过滤条重新执行查询,确保空值过滤不会被触发。
另外,你也可以试试XML条件绑定的方式(但复杂场景下JS逻辑更稳定):
<smartFilterBar:ControlConfiguration key="Plant" visibleInAdvancedArea="true" preventInitialDataFetchInValueHelpDialog="true" index="2" id="plantFilterBox"> <smartFilterBar:defaultFilterValues> <smartFilterBar:SelectOption low="{= ${appView>/Role/Plantid} ? ${appView>/Role/Plantid} : undefined }"></smartFilterBar:SelectOption> </smartFilterBar:defaultFilterValues> </smartFilterBar:ControlConfiguration>
不过这种绑定方式有时候会因为SAPUI5的绑定解析时序问题,还是出现空值提交的情况,所以更推荐用JS逻辑的方案。
内容的提问来源于stack exchange,提问作者MJBZA
相关产品推荐
相关产品推荐

