2016-11-14 64 views
0

我試圖在列表中只顯示唯一值。該值從以下JSON模式加載,我需要與所有唯一的名稱來填充列表:在sap.m.List中顯示唯一值

{"Customers": [ 
{"name": "Customer A","age": "30"}, 
{"name": "Customer B","age": "25"}, 
... 
]} 

在尋找一個可能的解決方案我跑進sap.m.ListBinding類及其功能getDistinctValues(sPath) >>see API

該函數應該從給定的相對路徑返回一個不同值的數組。我試過以下方式使用該功能:

var oModel = this.getView().getModel("customers"), 
oListBinding = new ListBinding(oModel, "customers>Customers", oModel.getContext("/Customers")), 
arrValues = oListBinding.getDistinctValues("name"); 

但是我總是收到arrValues=null。任何想法,我在做什麼錯在這裏? 我也試過使用customers>name,customers>/name/name沒有任何運氣。

+0

你嘗試過'新的ListBinding(oModel,「/ Customers」)'?或者'如果你想使用上下文,新的ListBinding(oModel,「」,oModel.getContext(「/ Customers」))'' ListBinding應該有一個指向數組的路徑。 – schnoedel

回答

0

如果getDistinctValues()失敗,或者您需要跨多個屬性測量獨立性,則下面的技術可能有用。雖然這種使用情況是用於輸入框建議我提供它的技術:

handleSuggestClient: function(oEvent){ 
     var sValue = oEvent.getParameter("suggestValue"); 
     var filters = []; 
     var uniqueNames = []; 
     filters = [new sap.ui.model.Filter([ 
       new sap.ui.model.Filter("", function(oNode) { // blank first param passes in entire node to test function 
        var sVal = ((oNode.client + oNode.contact) || "").toUpperCase() 
        if (uniqueNames.indexOf(sVal) === -1){ 
         uniqueNames.push(sVal); 
         return sVal.indexOf(sValue.toUpperCase()) > -1; 
        } else { 
         return false; 
        } 
       }) 
     ], false)]; 
     this.oSFClient.getBinding("suggestionItems").filter(filters); 
     this.oSFClient.suggest(); 
    }, 

內傳遞給過濾器的構造的功能包括邏輯,以測試的獨特性和添加匹配到一個陣列等

如果這個話題離題,讓我知道,我會刪除答案。