更新数据传输对象
打开应用层(即JD.CRS.Application)的CourseDtoGetAllCoursesInput.cs //Course数据传输对象(查询条件)
增加一行代码
public string Keyword { get; set; } //用于全文搜索
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using Abp.Application.Services.Dto; 2 using JD.CRS.Entitys; 3 4 namespace JD.CRS.Course.Dto 5 { 6 public class GetAllCoursesInput: PagedResultRequestDto 7 { 8 public StatusCode? Status { get; set; } 9 public string Keyword { get; set; } 10 } 11 }
更新应用服务
打开应用层(即JD.CRS.Application)的CourseCourseAppService.cs //Course应用服务
更新GetAll方法 //按Keyword查询Course
1 .WhereIf( 2 !input.Keyword.IsNullOrEmpty(), t => 3 t.Code.ToLower().Contains((input.Keyword ?? string.Empty).ToLower()) //按编号查询 4 || t.DepartmentCode.ToLower().Contains((input.Keyword ?? string.Empty).ToLower()) //按院系编号查询 5 || t.Name.ToLower().Contains((input.Keyword ?? string.Empty).ToLower()) //按名称查询 6 || t.Credits.ToString().Contains((input.Keyword ?? string.Empty).ToLower()) //按学分查询 7 || t.Remarks.ToLower().Contains((input.Keyword ?? string.Empty).ToLower()) //按备注查询 8 );
完整代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public override async Task<PagedResultDto<CourseDto>> GetAll(GetAllCoursesInput input) 2 { 3 var query = base.CreateFilteredQuery(input) 4 .WhereIf(input.Status.HasValue, t => t.Status == input.Status.Value) 5 .WhereIf( 6 !input.Keyword.IsNullOrEmpty(), t => 7 t.Code.ToLower().Contains((input.Keyword ?? string.Empty).ToLower()) //按编号查询 8 || t.DepartmentCode.ToLower().Contains((input.Keyword ?? string.Empty).ToLower()) //按院系编号查询 9 || t.Name.ToLower().Contains((input.Keyword ?? string.Empty).ToLower()) //按名称查询 10 || t.Credits.ToString().Contains((input.Keyword ?? string.Empty).ToLower()) //按学分查询 11 || t.Remarks.ToLower().Contains((input.Keyword ?? string.Empty).ToLower()) //按备注查询 12 ); 13 var coursecount = query.Count(); 14 var courselist = query.ToList(); 15 return new PagedResultDto<CourseDto>() 16 { 17 TotalCount = coursecount, 18 Items = ObjectMapper.Map<List<CourseDto>>(courselist) 19 }; 20 }
更新模型
打开展示层(即JD.CRS.Web.Mvc)的Models/Course/CourseListViewModel.cs //Course查询视图模型
增加一行代码
public string Keyword { get; set; }
更新控制器
打开展示层(即JD.CRS.Web.Mvc)的Controllers/CourseController.cs //Course控制器
更新Index方法 //按Keyword查询Course
增加一行代码
Keyword = input.Keyword
完整代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public async Task<ActionResult> Index(GetAllCoursesInput input) 2 { 3 IReadOnlyList<CourseDto> output = (await _courseAppService.GetAll(new GetAllCoursesInput { Status = input.Status, Keyword = input.Keyword })).Items; 4 var model = new CourseListViewModel(output) 5 { 6 Status = input.Status, 7 Keyword = input.Keyword 8 }; 9 return View(model); 10 }
更新视图
打开展示层(即JD.CRS.Web.Mvc)的Views/Course/Index.cshtml //Course查询视图
插入查询条件 //按Keyword查询Course
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <div class="header"> 2 <table> 3 <thead> 4 <tr> 5 <th class="col-sm-1">@L("Status")</th> 6 <th class="col-sm-1"> 7 @Html.DropDownListFor( 8 model => model.Status, 9 Model.GetStatusList(LocalizationManager), 10 new 11 { 12 @class = "form-control", 13 id = "Status" 14 }) 15 </th> 16 <th class="col-sm-1">@L("Keyword")</th> 17 <th class="col-sm-4"> 18 <input id="Keyword" name="Keyword" type="text" class="form-control" placeholder="Please enter the keyword..." value=@Model.Keyword /> 19 </th> 20 <th class="col-sm-4"></th> 21 <th class="col-sm-1"> 22 <button id="Search" class="form-control">@L("Search")</button> 23 </th> 24 </tr> 25 </thead> 26 </table> 27 </div>
更新脚本
打开展示层(即JD.CRS.Web.Mvc)的wwwrootview-resourcesViewsCourseIndex.js //用以存放Course查询相关脚本
插入查询条件 //按Stauts & Keyword 组合查询Course
1 //define variable 2 var _$status = $('#Status'); 3 var _$keyword = $('#Keyword'); 4 var _$search = $('#Search'); 5 //Search 6 _$search.click(function () { 7 location.href = '/Course?status=' + _$status.val() + '&keyword=' + _$keyword.val(); 8 });
预览效果