public partial class Sys_UserService : ServiceBase里层Sys_UserService则是自定义接口,重写业务接口等。 因为同名partial类,所以编译的时候把这两个类合并编辑。因此overridePageGridData, ISys_UserService, IDependency { public Sys_UserService(ISys_UserRepository repository) : base(repository) { Init(repository); } public static ISys_UserService Instance { get { return AutofacContainerModule.GetService (); } } }
public class PageGridDatapublic object extra { get; set; }这就框架为我们提供的,可以在返回前,再返回一些额外的数据,比如返回其他表的信息,前台找到查询后的方法取出来。{ public int status { get; set; } public string msg { get; set; } public int total { get; set; } public List rows { get; set; } public object summary { get; set; } /// /// 可以在返回前,再返回一些额外的数据,比如返回其他表的信息,前台找到查询后的方法取出来 /// public object extra { get; set; } }
后台修改新建Sys_UserDto,作为Sys_User的自定义ViewModel。多一个IsOnline字段,显示用户是否在线。
public class Sys_UserDto:Sys_User
{
public string IsOnline { get; set; }
}
修改public override PageGridDataublic override PageGridDataGetPageData(PageDataOptions pageData) { int roleId = -1; //树形菜单传查询角色下所有用户 if (pageData.Value != null) { roleId = pageData.Value.ToString().GetInt(); } QueryRelativeExpression = (IQueryable queryable) => { if (roleId <= 0) { if (UserContext.Current.IsSuperAdmin) return queryable; roleId = UserContext.Current.RoleId; } //查看用户时,只能看下自己角色下的所有用户 List roleIds = Sys_RoleService .Instance .GetAllChildrenRoleId(roleId); roleIds.Add(roleId); //判断查询的角色是否越权 if (roleId != UserContext.Current.RoleId && !roleIds.Contains(roleId)) { roleId = -999; } return queryable.Where(x => roleIds.Contains(x.Role_Id)); }; base.OrderByExpression = x => new Dictionary
前台修改方式一:修改srcviewsystem Sys_User.vue文件,在const columns最后面添加字段。注意field要和Dto中的扩展字段一致。
{field:'IsOnline',title:'是否在线',type:'string',width:120,require:true,align:'left'}
这种方式如果使用代码生成器会被覆盖。
方式二(推荐):修改srcextensionsystemSys_User.js文件
在onInit的时候this.columns.splice(5,0,{field:'IsOnline',title:'是否在线',type:'string',width:120,require:true,align:'left'}),这样就可以指定在第5个列的位置 加一行数据
修改srcextensionsystemSys_User.js文件,在methods中添加searchAfter方法,改方法的意思就是添加数据表追加后台返回的extra中的数据。这就解释了后台使用了自定义Dto后,为什么要清空gridData.rows中的数据。如果不清空,表格中会渲染完全一样的两组数据,自定义Dto的数据被追加到代码生成器返回数据的下面。
searchAfter(rows,result){
rows.push(...result.extra);
return true;
}
这样我们就完成了,自定义dto(viewmodel)。
效果
VolCore框架演示地址:vol3.0版本 (volcore.xyz)http://www.volcore.xyz/#/login源码地址:Vue.NetCore: .NetCore+Vue2/Vue3+Element plus+uniapp前后端分离,全自动生成PC与移动端代码;支持移动端(uniapp)ios/android/h5/微信小程序。http://www.volcore.xyz/ (gitee.com)https://gitee.com/x_discoverer/Vue.NetCorecq-panda/Vue.NetCore: .NetCore+Vue2/Vue3+Element plus+uniapp前后端分离,全自动生成代码;支持移动端(uniapp)ios/android/h5/微信小程序。http://www.volcore.xyz/ (github.com)https://github.com/cq-panda/Vue.NetCore官方学习视频:CCtalk 实时互动教育平台-专业的知识分享与在线教育平台https://www.cctalk.com/m/group/90268531



