将 年-月-日 封装成tree树状结构

1. 查询sql   按时间降序排列

SELECT DISTINCT
date(SNAP_TIME) as SNAP_TIME,
DATE_FORMAT(SNAP_TIME, '%Y') as yyyy,
DATE_FORMAT(SNAP_TIME, '%m') as mm,
DATE_FORMAT(SNAP_TIME, '%d') as dd
FROM
asset_pic_info
WHERE
1 = 1
ORDER BY SNAP_TIME DESC

2. 编写后台逻辑业务代码
public interface AssetpicinfoManager {    //接口
/**
* 按年、月、日树形结构展示的界面
*/
Map getYYYYMMDDTree();
}
@Scope("singleton")
@Service("assetpicinfo-manager")
public class AssetpicinfoManagerImpl implements AssetpicinfoManager {    //实现类
@Override
public Map getYYYYMMDDTree() {
PersistenceCrudManager pcm = PersistenceFactory.getInstance();
Map map = new HashMap();
DAOMeta daoMeta = DAOHelper.getDAO("assetpicinfo-dao", "query_ymd_tree", null);  //获取sql
List<Map<String, Object>> list = pcm.findByNativeSQL(daoMeta);  // 执行sql
if(ListUtil.isNotEmpty(list)) {
map.put("list",list);  //将结果放到map中,然后返回map
}
return map;
}
}
3.action  中调用
public Map perform(Map inMap) throws BizException, SysException {
//Do before
Map map = assetpicinfoManager.getYYYYMMDDTree();
return map;
}

4. js 处理返回结果 返回结果为 (例如) 2017-10-06 2017 10 06
loadTree: function () {
// 定义数组变量 用于存储 年月日的值
var yyyy=[];
var mm=[];
var dd=[];
//1. 发起请求 获取后台数据
Scdp.doAction("assetpicinfo-leftmenu-query",null,function(result){
// 2. 是否请求成功
if(result.success){
// 3. 返回请求 后台数据 list 集合
var timeList=result.list;
var count=0;
var mmCount=0;
// 4.循环list集合
for (var i = 0; i < timeList.length; i++) {
// 5.1 判断年份是否有值 刚开始第一次循环肯定是没有值的
if(Scdp.ObjUtil.isNotEmpty(yyyy[count-1])){
//6. 判断你后台返回 的数据是否年份一样,如果年份一样就不存入 yyyy数组中
if(yyyy[count-1].text.replace("年","")!=timeList[i].yyyy){
//
count++;
mmCount=1;
mm=[];
dd=[];
dd.push({'id': timeList[i].snapTime.substr(0, 10), 'text': timeList[i].dd+'日'});
mm.push({'parent':timeList[i].yyyy,'text': timeList[i].mm+'月', 'children': dd});
yyyy.push({'text': timeList[i].yyyy+'年','children': mm});
}else{
// 7. 当年份不一样时 将月份存入当前年份下面
if(mm[mmCount-1].text.replace("月","")!=timeList[i].mm){
dd=[];
mmCount++;
mm.push({'parent':timeList[i].yyyy,'text': timeList[i].mm+'月', 'children': dd});
dd.push({'id': timeList[i].snapTime.substr(0, 10), 'text': timeList[i].dd+'日'});
}else{
dd.push({'id': timeList[i].snapTime.substr(0, 10), 'text': timeList[i].dd+'日'});
}
}
}else{
//5.2 所以这边初始化存一个值 到年-月-日 数组中
count++;
mmCount++;
dd.push({'id': timeList[i].snapTime.substr(0, 10), 'text': timeList[i].dd+'日'});
mm.push({'parent':timeList[i].yyyy,'text': timeList[i].mm+'月', 'children': dd});
yyyy.push({'text': timeList[i].yyyy+'年','children': mm});
}
}
// 树状结构加载load数据
$("ul[itemId='treeMenu']").tree({
data: yyyy
});
// 加载load数据后 全部折叠
$("ul[itemId='treeMenu']").tree('collapseAll');

}
}, false, true);
}


原文地址:https://www.cnblogs.com/wcnwcn/p/7675499.html