上下移动 04.16
实例效果图:
本次代码演示图
1、表结构
2、业务逻辑主要在存储过程中
CREATE PROC up_move_0416 --用户自定义的存储过程一般以'up_'开头 @moveId INT,--操作的自身Id @isUp INT--是否是上下移【1:上移 , 2:下移】 AS BEGIN DECLARE @currfSort INT; SELECT @currfSort=sort FROM Move0416 WHERE id=@moveId; DECLARE @targetId INT =0,@targetSort int =0; IF @isUp=1 --如果是上移[找比当前sort值大的最小记录:top,order by sort desc] SELECT TOP 1 @targetId=id,@targetSort=sort FROM dbo.Move0416 WHERE sort>@currfSort ORDER BY sort DESC ELSE --反之下移[找比当前sort值小的最大记录:top,order by] SELECT TOP 1 @targetId=id,@targetSort=sort FROM dbo.Move0416 WHERE sort<@currfSort ORDER BY sort IF IsNull(@targetId,0)>0 BEGIN update Move0416 set Sort = @targetSort where Id = @moveId--更新当前记录 update Move0416 set Sort = @currfSort where Id = @targetId--更新目标记录 END END
3、实现mvc+linq
3.1 Model层
public static class MoveData { public static IList<BlogMove> GetEntities(this Table<BlogMove> source) { try { /** * 本身linq就有 getmodel,getlist方法 * 之所以在写是为了对异常进行处理,包括数据验证等 */ return source.ToList(); } catch (Exception) { return null; //记录错误日志 } } public static BlogMove GetModel(this Table<BlogMove> source, int moveId) { try { return source.Where(m => m.id == moveId).Single(); //return (from s in source // where s.id==moveId // select s).FirstOrDefault(); //return source.Single(n => n.Fid == id); } catch { return null; } } /// <summary> /// 新插入的sort(已有的最大sort+1) /// </summary> /// <param name="source"></param> /// <param name="moveId"></param> /// <returns></returns> public static int AddNewSort(this Table<BlogMove> source, int moveId) { int sort = 0; try { sort = source.Where(m => m.id == moveId).Max(m => (int)m.Sort); } catch (Exception) { } return ++sort; } }
3.2 Controller层
public class MoveController : Controller { BlogDBDataContext db = new BlogDBDataContext(); // // GET: /Move/ public JsonResult ListJSON() { var moveList=db.BlogMove.GetEntities(); return Json(moveList.OrderBy(m=> m.Sort)); } public ActionResult MoveSort() { int id = Convert.ToInt32(Request["id"]); bool isUp = Convert.ToBoolean(Request["isUp"]); db.up_move_0416(Convert.ToInt32(id), Convert.ToBoolean(isUp) ? 1 : 0); return Json(new Result()); } public ActionResult List() { return View(); } } public class Result { public bool Msg { get; private set; } public Result() { Msg = true; } }
3.3 View层
<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>List</title> <script src="http://www.cnblogs.com/Scripts/jquery-1.3.2.js" type="text/javascript"></script> <script type="text/javascript"> $(function(){ InitData(); }) function InitData(){ $("#tabList tr").remove(); $.getJSON("/Move/ListJSON",{},function(data){ $("#tabList").append("<tr><th>名称</th><th>排序</th><th>操作</th></tr>"); $.each(data, function (i, item) { var title=item.Title; var id=item.id; var sort=item.Sort; $("#tabList").append("<tr><td>"+title+"<td><td>"+sort+"</td><td>"+ "<a href='javascript::' onclick='javascript:Move("+id+",true)'>上移</a>|"+ "<a href='javascript::' onclick='javascript:Move("+id+",false)'>下移</a>" +"</td></tr>"); }); }); } function Move(id,f){ var params={id:id,isUp:f}; $.getJSON("/Move/MoveSort",params,function(data){ if(data.Msg){ InitData(); } }) } </script> </head> <body> <div > <table cellpadding="0" cellspacing="0" id="tabList"> </table> </div> </body> </html>
生活没有输赢,不要在乎别人如何评价你,开心就好。
QQ群:158138959