springmvc 后台向頁面EasyUI的tree傳遞數據(JSon格式)


EasyUI的Tree需要提供的JSon格式為:id,text,state

請求的參數:id(當前節點的id

響應的結果:json數據。

[{    

    "id": 1,    

    "text": "Node 1",    

"state": "closed"

 }

{    

    "id": 2,    

    "text": "Node 2",    

"state": "closed"

 }

]

如果當前節點為父節點,state應為“closed”、如果是葉子節點“open

 

 

 

定義一個EasyUITreeJson 類來包裝JSon數據

public class EasyUITreeJson {
private long id;
private String text;
private String state; long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

}

 

/*

  service層

*/

@Service
public class TbItemCatImpl implements ITbItemCatService {
@Resource
private TbItemCatMapper tbItemCatMapper;

@Override
public List<EasyUITreeJson> getItemCatList(long parentId) {
// 根據parentId查詢分類列表
TbItemCatExample example = new TbItemCatExample();
// 設置查詢條件
Criteria createCriteria = example.createCriteria();
createCriteria.andParentIdEqualTo(parentId);
// 執行查詢
List<TbItemCat> list = tbItemCatMapper.selectByExample(example);
// 轉換成EasyUITree列表
List<EasyUITreeJson> resultList = new ArrayList<>();
for (TbItemCat tbItemCat : list) {
// 創建一個節點對象
EasyUITreeJson node = new EasyUITreeJson();
node.setId(tbItemCat.getId());
node.setText(tbItemCat.getName());
node.setState(tbItemCat.getIsParent() ? "closed" : "open");
// 添加到列表中
resultList.add(node);
}
return resultList;
}

}

 

 

 

/*

controller層

*/

@Controller
@RequestMapping("/item/cat")
public class TbItemCatController {
@Resource
private ITbItemCatService iTbItemCatService;

@RequestMapping("/list")
@ResponseBody
public List<EasyUITreeJson> getItemCatList(@RequestParam(value = "id", defaultValue = "0") long parentId) {
List<EasyUITreeJson> itemCatList = iTbItemCatService.getItemCatList(parentId);
return itemCatList;
}
}

 

 

效果如下:

 

 

總結:其實后台向easyUI中傳值多數以JSon格式,我們只需要在后台包裝一個JSon格式的數據集,再把它傳遞到頁面即可。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM