2016-07-21 3 views
0

먼저 테이블을 만들었습니다. 그리고 부가 가치. 그런 다음 검색 (필터링)을 위해 textfield2에 리스너를 추가했습니다. 그리고 하나의 열에 값의 합계를 얻기위한 getStatistics 함수가 있습니다. 그러나 데이터를 필터링 한 후에 작동하지 않았습니다. 필터링 후 합계 값을 얻으려면 어떻게해야합니까?Javafx tableview는 필터링 된 목록에서 값을 가져옵니다.

ObservableList<CurrentreservsController.userdata> data; 
 
    data = FXCollections.observableArrayList(); 
 
....... 
 
....... 
 

 
     FilteredList<userdata> filt = new FilteredList<>(data, p ->true); 
 
       textfield2.textProperty().addListener((observable, oldValue, newValue) -> { 
 
       filt.setPredicate(userdata -> { 
 
        if (newValue == null || newValue.isEmpty()) { 
 
         return true; 
 
        } 
 
        String lowerCaseFilter = newValue.toLowerCase(); 
 
       if (userdata.otag.toString().toLowerCase().contains(lowerCaseFilter)) { 
 
         return true; // Filter matches first name. 
 
        } else if (userdata.cm.toString().toLowerCase().contains(lowerCaseFilter)) { 
 
         return true; // Filter matches last name. 
 
        } 
 
        return false; // Does not match. 
 
       }); 
 
      }); 
 
      SortedList<userdata> sortedData = new SortedList<>(filt); 
 
      sortedData.comparatorProperty().bind(tablesettings.comparatorProperty()); 
 
      tablesettings.setItems(sortedData); 
 
      seartol=0; 
 
      getStatistics();

getStatistic 기능

private void getStatistics() { 
 
      seartol=0; 
 
     for (userdata product : data) { 
 
     seartol = seartol + Integer.parseInt(product.tol.getValue().toString()); 
 
     } 
 
     label1.setText("Total sum: "+seartol); 
 
    }

답변

0

당신은 단순히 필터링 된 목록이 아닌 소스 목록을 기반으로 통계를 계산해야합니다.

private void getStatistics() { 
    seartol=0; 
    for (userdata product : filt) { 
     seartol = seartol + Integer.parseInt(product.tol.getValue().toString()); 
    } 
     label1.setText("Total sum: "+seartol); 
} 
+0

upsss. 감사 . 그것은 성공했습니다. – Gpear

+0

@Gpear : 해결책이 당신을 위해 작동한다면, 코멘트를 남기는 대신 그것을 받아들이는 것을 고려하십시오. 이렇게하면 다른 사람이 작동중인 솔루션을 찾았 음을 쉽게 감지 할 수 있습니다. – fabian

관련 문제