master
			
			
		
		
							parent
							
								
									f155464ec1
								
							
						
					
					
						commit
						4d3417a334
					
				| @ -0,0 +1,56 @@ | ||||
| package view; | ||||
| 
 | ||||
| import javafx.collections.FXCollections; | ||||
| import javafx.collections.ObservableList; | ||||
| import javafx.scene.control.TreeItem; | ||||
| import javafx.scene.control.TreeView; | ||||
| import javafx.scene.layout.AnchorPane; | ||||
| import model.DataManager; | ||||
| import model.Goods; | ||||
| 
 | ||||
| public class CategoryManagementView extends AnchorPane { | ||||
| 
 | ||||
|     private TreeView<String> categoryTreeView; | ||||
| 
 | ||||
|     public CategoryManagementView() { | ||||
|         initializeComponents(); | ||||
|         loadCategoriesAndGoods(); | ||||
|     } | ||||
| 
 | ||||
|     private void initializeComponents() { | ||||
|         categoryTreeView = new TreeView<>(); | ||||
|         categoryTreeView.setShowRoot(false); // 不显示根节点
 | ||||
|         AnchorPane.setTopAnchor(categoryTreeView, 10.0); | ||||
|         AnchorPane.setRightAnchor(categoryTreeView, 10.0); | ||||
|         AnchorPane.setBottomAnchor(categoryTreeView, 10.0); | ||||
|         AnchorPane.setLeftAnchor(categoryTreeView, 10.0); | ||||
|         this.getChildren().add(categoryTreeView); | ||||
|     } | ||||
| 
 | ||||
|     private void loadCategoriesAndGoods() { | ||||
|         DataManager dataManager = DataManager.getInstance(); | ||||
|         ObservableList<Goods> allGoods = FXCollections.observableArrayList(dataManager.getAllGoods()); | ||||
| 
 | ||||
|         // 获取所有分类
 | ||||
|         ObservableList<String> categories = FXCollections.observableArrayList(allGoods.stream() | ||||
|                 .map(Goods::getCategory) | ||||
|                 .distinct() | ||||
|                 .toArray(String[]::new)); | ||||
| 
 | ||||
|         // 构建树结构
 | ||||
|         TreeItem<String> root = new TreeItem<>(); | ||||
|         for (String category : categories) { | ||||
|             TreeItem<String> categoryItem = new TreeItem<>(category); | ||||
|             for (Goods goods : allGoods) { | ||||
|                 if (goods.getCategory().equals(category)) { | ||||
|                     TreeItem<String> goodsItem = new TreeItem<>(goods.getName() + " (" + goods.getSpecification() + ")"); | ||||
|                     categoryItem.getChildren().add(goodsItem); | ||||
|                 } | ||||
|             } | ||||
|             root.getChildren().add(categoryItem); | ||||
|         } | ||||
| 
 | ||||
|         categoryTreeView.setRoot(root); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
					Loading…
					
					
				
		Reference in new issue