上一级下一级的排序

还可以再抽象,先暂时这样,方便看:

/**
     * 向下移动节点
     * @param property
     * @param model
     * @return
     */
    @RequestMapping(value = "/down", method = RequestMethod.POST)
    @ResponseBody
    public String down(Property property, ModelMap model) {
    	property = propertyService.findBySortCode(property.getSortCode());
    	String parentCode = property.getSortCode().substring(0, property.getSortCode().lastIndexOf("-"));
        List<Property> properties = propertyService.findChild(parentCode);
        int index = properties.indexOf(property);
        Property nextProperty = properties.get(index+1); 
        long sortId = property.getSortId();
        long nextSortId = nextProperty.getSortId();
        property.setSortId(nextSortId);
        nextProperty.setSortId(sortId);
        propertyService.update(property);
        propertyService.update(nextProperty);
        return "SUCCESS";
    }
    
    /**
     * 向上移动节点
     * @param property
     * @param model
     * @return
     */
    @RequestMapping(value = "/up", method = RequestMethod.POST)
    @ResponseBody
    public String up(Property property, ModelMap model) {
    	property = propertyService.findBySortCode(property.getSortCode());
    	String parentCode = property.getSortCode().substring(0, property.getSortCode().lastIndexOf("-"));
        List<Property> properties = propertyService.findChild(parentCode);
        int index = properties.indexOf(property);
        Property nextProperty = properties.get(index-1); 
        long sortId = property.getSortId();
        long nextSortId = nextProperty.getSortId();
        property.setSortId(nextSortId);
        nextProperty.setSortId(sortId);
        propertyService.update(property);
        propertyService.update(nextProperty);
        return "SUCCESS";
    }

application.js

//保存已加载的节点(避免重复查询)
var loadList=new Array();

index.js


/**
 * Created with JetBrains WebStorm.
 * User: Administrator
 * Date: 13-2-17
 * Time: 上午9:32
 * To change this template use File | Settings | File Templates.
 */


$(function(){
	loadData(0,false);
});


var inputIndex=1;

//添加知识节点(渲染)
function addPoint(codes){
    var parent=$("#child_"+codes);
    var style=$("#"+codes);
    var codeLength=codes.split("-");
    $(".add").remove();
    var space="";
    for (var int = 0; int < codeLength.length+2; int++) {
		space+="  ";
	}
    parent.after(
        "<tr data-path='p_"+codes+"' class='add'>"
            +"<td colspan='3'>"
            +space
            +"<input class='search_input' id='name_"+codes+"'/>"
            +"<input type='button' class='search_button' value='确定' onclick=savePoint('"+codes+"') />"
            +"</td>"
            +"</tr>"
    );
    showIcon(style);
}

//保存知识点(数据持久化)
function savePoint(codes){
    $.post(ROOT_PATH+"property/save",{name:obj.val(),sortCode:codes},function(data){
        if(data.indexOf("SUCCESS")!=-1){
            loadData(codes,true);
            $(".add").remove();
        }else{
            alert("添加失败!");
        }
    });
}

//加载子知识点,当bool==true时强制加载
function loadData(code,bool){
    var child=$("#child_"+code);
    var show=$("#show_"+code);
    var isLoad=true;
//    if(code!=0 && bool==false){
//        switchIcon(show);
//    }

    if(bool==false){
        for (var i=0;i<loadList.length;i++) {
            if(code==loadList[i]){
                isLoad=false;
            }
        }
    }
    if(isLoad){
        $("#loadData").load(ROOT_PATH+"property/child?sortCode="+code,function(data){
        	if(bool==true){
	        	$("[data-path^=p_" + code + "]").remove();
	        	//alert("[data-path^=p_" + code + "]");
	        	loadList=[];
        	}
        	child.after(data);
            loadList.push(code);
            bool==false;
        });
    }
}


function switchIcon(obj){
	if(obj.is(".arr")){
		//$("#^"+obj.attr("id")+"-").empty();
		loadData(obj.attr("id"),true);
		obj.removeClass("arr").addClass("arr2");
		$("[data-path=p_" + obj.attr("id") + "]").show();
	}else{
		$("[data-path=p_" + obj.attr("id") + "]").remove();
		//$("#"+obj.attr("id")).empty();
		obj.removeClass("arr2").addClass("arr");
		$("[data-path^=p_" + obj.attr("id") + "]").hide();
	}
}

function showIcon(obj){
	if(obj.is(".arr")){
		loadData(obj.attr("id"),false);
		obj.removeClass("arr").addClass("arr2");
		$("[data-path=p_" + obj.attr("id") + "]").show();
	}
}

function editPoint(code){
	$("[id^=s_node]").show();
	$("[id^=h_node]").hide();
	var s=$("#s_node_"+code);
	var h=$("#h_node_"+code);
	s.hide();
	h.show();
}

function updatePoint(code){
	var obj=$("#edit_"+code);
	$.post(ROOT_PATH+"property/update",{name:obj.val(),sortCode:code},function(data){
        if(data.indexOf("SUCCESS")!=-1){
        	code=code.substring(0,code.lastIndexOf("-"));
            loadData(code,true);
            $("h_node").hide();
        }else{
            alert("更新失败!");
        }
    });
}

function deletePoint(code){
	$.post(ROOT_PATH+"property/delete",{sortCode:code},function(data){
        if(data.indexOf("SUCCESS")!=-1){
        	code=code.substring(0,code.lastIndexOf("-"));
        	loadData(code,true);
        }else{
            alert("删除失败!");
        }
    });
}

function downPoint(code){
	$.post(ROOT_PATH+"property/down",{sortCode:code},function(data){
        if(data.indexOf("SUCCESS")!=-1){
        	code=code.substring(0,code.lastIndexOf("-"));
            loadData(code,true);
        }else{
            alert("向下移动失败!");
        }
    });
}

function upPoint(code){
	$.post(ROOT_PATH+"property/up",{sortCode:code},function(data){
        if(data.indexOf("SUCCESS")!=-1){
        	code=code.substring(0,code.lastIndexOf("-"));
            loadData(code,true);
        }else{
            alert("向上移动失败!");
        }
    });
}

<c:if test="${status.count==1&&fn:length(parents)>1 }">
                <a href="javascript:downPoint('${entry.sortCode }')">下移</a>
              </c:if>
              <c:if test="${status.count==fn:length(parents)&&fn:length(parents)>1 }">
                <a href="javascript:upPoint('${entry.sortCode }')">上移</a>
              </c:if>
              <c:if test="${status.count!=fn:length(parents)&&status.count!=1&&fn:length(parents)>1}">
                <a href="javascript:upPoint('${entry.sortCode }')">上移</a>
                <a href="javascript:downPoint('${entry.sortCode }')">下移</a>
              </c:if>


你可能感兴趣的:(上一级下一级的排序)