项目中遇到的问题
程序员文章站
2022-04-18 09:28:58
...
1:Mvc中设置区域里的视图为起始页
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Manger", action = "Index", id = UrlParameter.Optional },
namespaces:new string[]{" Lzd.Mvc.UI.Areas.Admin.Controllers"}
).DataTokens.Add("Area","Admin");
2:ef中实现左连接
var list = string.IsNullOrWhiteSpace(Name) ? _tbDepartmentService.LoadEntities().ToList() : _tbDepartmentService.LoadEntities(p => p.DepartmentName == Name).ToList();
var newlist = (from t in list
join t1 in list
on t.ParentID equals t1.DepartmentID
into depart //将查询结果放入depart中
from t1 in depart.DefaultIfEmpty() //返回指定序列元素,如果为空返回null
select new
{
DID = t.DID,
DepartmentID = t.DepartmentID,
DepartmentName = t.DepartmentName,
ParentID = t.ParentID,
PName = t1==null ? "无" : t1.DepartmentName//必须判断是否为空 否则会报空异常
}).ToList();
3:bootstrap-table父子表
上面需要注意一点,在oInit.InitSubTable方法中,sidePagination: "server",//必须设置为server 否则获取不到数据
4:easyui treegrid
$("#tbList").treegrid({
toolbar: '#toolbar',
url: "@Url.Action("GetListByEasyUI")",
title: '部门列表',
idField: 'id',//标识id,
treeField: 'name',
rownumbers: true,
animate: true, //开启动画
fitColumns: true, //填充整个单元格
resizable: true,
columns: [[
{ field: 'ck', checkbox: true },
{ title: '部门名称', field: 'name', width: 200 },
{
title: '部门内码/用户内码', field: 'id', width: 300
},
{
title: '编号', field: 'did', width: 40
},
{
title: '部门内码', field: 'isOrang', width: 80, hidden: true
}
]],
onClickRow: function (r, v) { //点击一行时触发
if (r.isUser == true) {
//console.info($("#tbList").treegrid("getParent", {id:r.id}) );
console.info(r);
isdepeart = false;
UserArry = r;
GetRow = null;
return false;
}
UserArry = null;
if (r.isOrang && r.isUser == false) { //点击组织
OrangId = r.id;
isdepeart=false;
}
if (r.isOrang == false && r.isUser == false) { //点击部门
isdepeart=true;
OrangId = r.pid;
}
GetRow = r;
@*InitTree("leftTree", {}, "@Url.Action("GetUserByodID")", { orangID: r.pid, DepeartID: r.id }, 1);
$("#smodule").html("当前部门:<strong style='color:red'>" + r.name + "</strong>");*@
},
onLoadSuccess: function () {//加载成功时触发
//设置子节点关闭
//$('#tbList').treegrid('collapseAll');
}
});
参数是以[{id:'',name:'',children:[{}]}]的形式。
5:递归获取treegrid所需数据
/// <summary>
/// 组织部门用户treegrid模型类
/// </summary>
public class TreeGridModel {
public string id { get; set; }
/// <summary>
/// 用于标识是否是组织
/// </summary>
public bool isOrang { get; set; }
public string name { get; set; }
public string pid { get; set; }
public string did { get; set; }
/// <summary>
/// 用于标识是否是用户
/// </summary>
public bool isUser { get; set; }
public List<TreeGridModel> children { get; set; }
}
/// <summary>
/// 用户存储链接查询实体
/// </summary>
public class TgHelper {
public string id { get; set; }
public string did { get; set; }
public bool isOrang { get; set; }
public string name { get; set; }
public string pid { get; set; }
}
#region 递归获取组织部门树形表格
private static List<TgHelper> TgList = new List<TgHelper>();
public ActionResult GetListByEasyUI()
{
///查询组织部门并入到集合中
var list = _tbOrganizationService.LoadEntities(p => p.IsDelete == false).ToList().Select(p => new TgHelper
{
id = p.OrganizationID,
name = p.OrganizationName,
pid = p.ParentID,
isOrang = true,
did = p.OID.ToString()
}).Union(_tbDepartmentService.LoadEntities(p => p.IsDelete == false).ToList().Select(p => new TgHelper
{
id = p.DepartmentID,
name = p.DepartmentName,
pid = p.ParentID,
isOrang = false,
did = p.DID.ToString()
}));
TgList = list.ToList();
List<TreeGridModel> treelist = new List<TreeGridModel>();
var parentlist = TgList.Where(p => p.pid == "0").ToList();
//遍历
parentlist.ForEach(p =>
{
// 递归
TreeGridModel tr = new TreeGridModel() { id = p.id, isOrang = p.isOrang, name = p.name, children = this.OrDepaByPid(p.id), pid = p.pid, did = p.did };
treelist.Add(tr);
});
var obj = new
{
rows = treelist.ToArray(),
total = treelist.Count
};
return Json(obj, JsonRequestBehavior.AllowGet);
}
/// <summary>
/// 递归查询出组织或部门或用户
/// </summary>
/// <param name="parent"></param>
/// <returns></returns>
private List<TreeGridModel> OrDepaByPid(string parent)
{
List<TreeGridModel> tree = new List<TreeGridModel>();
var list = TgList.Where(p => p.pid == parent).ToList();
if (list.Count > 0)
{
list.ForEach(p =>
{
TreeGridModel t = new TreeGridModel();
t.id = p.id;
t.name = p.name;
t.isOrang = p.isOrang;
t.pid = p.pid;
t.did = p.did;
t.children = this.OrDepaByPid(p.id);
tree.Add(t);
});
}
else
{ //判断是组织还是部门 组织继续递归 部门添加用户
ItbUserOranDePeartService _tbudo=new tbUserOranDePeartService();
var newlist=TgList.Where(p=>p.id==parent).FirstOrDefault();
if (!newlist.isOrang) { //是部门
//添加用户
var ulist =( from d in _tbudo.LoadEntities(p => p.DepartmentID == newlist.id && p.OrganizationID == newlist.pid).ToList()
join u in _tbUserService.LoadEntities(p => p.IsSupplier == true).ToList()
on d.UserID equals u.UserID
select new {
id=u.UserID,
name=u.UserName,
pid=d.DepartmentID ,
did=u.UID.ToString()
}).ToList();
foreach (var p in ulist)
{
TreeGridModel t = new TreeGridModel();
//前台easyui根据id标识列,当id相同时无法点击 以随机数辨别
t.id = p.pid+ p.id;
t.name = p.name;
t.isOrang = false;
t.isUser = true;
t.pid = p.pid;
t.did = p.did;
tree.Add(t);
}
}
}
return tree;
}
#endregion
ajax上传图片
<form id="upsfz" enctype="multipart/form-data" >
<input type="hidden" name="ID" value="99999"/>
<input type="file" id="input08" name="file">
@*<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>*@
<button type="button" id="upok" class="btn btn-primary btn-sm" style=" position: absolute;
right: 19px;
bottom: -15px;">
确认上传
</button>
</form>
$("#input08").on("change", upload);
function upload() {
debugger;
var file = this.files[0];
console.info(this.files[0]);
if (file.type != 'image/jpeg' && file.type != 'image/jpg' && file.type != 'image/gif' && file.type != 'image/png') {
layer.msg("不是图片");
return false;
}
console.info($("#upsfz")[0]);
var formdata = new FormData($("#upsfz")[0]);
$.ajax({
url: '@Url.Action("UploadSfz")',
type: 'POST',
async: false,
cache: false,
contentType: false,
processData: false,
data: formdata,
success: function (data) {
console.info(data);
}
});
}
后台接收:
#region 上传图片
[ValidateInput(false)]
public JsonResult UploadSfz(HttpPostedFileBase file,string ID) {
return Json("s");
}
上一篇: [机器学习] Yellowbrick使用笔记4-目标可视化
下一篇: 项目中遇到的问题