BasePagedListVM是框架中負(fù)責(zé)分頁展示數(shù)據(jù)和導(dǎo)出的VM,它繼承自BaseVM
建立一個BasePagedListVM
建立BasePagedListVM主要有以下幾部
1. 定義繼承自BasePagedListVM的類,將關(guān)聯(lián)的Model作為泛型變量
2. 重寫GetSearchQuery函數(shù),將查詢語句和邏輯寫在這個函數(shù)里
3. 重寫InitGridHeader函數(shù),這個函數(shù)定義列表顯示的列
4. 重寫InitGridAction函數(shù),這個函數(shù)定義列表中需要出現(xiàn)的操作按鈕
下面以學(xué)校模型為例,展示如何編寫一個簡單的ListVM,學(xué)校模型的定義可以參考第一個模塊
- public class SchoolListVM : BasePagedListVM<School, BaseSearcher>
- {
- protected override List<GridAction> InitGridAction()
- {
- return new List<GridAction>
- {
- this.MakeStandardAction("School", GridActionStandardTypesEnum.Create, "新建","", dialogWidth: 800),
- this.MakeStandardAction("School", GridActionStandardTypesEnum.Edit, "修改","", dialogWidth: 800),
- this.MakeStandardAction("School", GridActionStandardTypesEnum.Delete, "刪除", "",dialogWidth: 800),
- this.MakeStandardAction("School", GridActionStandardTypesEnum.Details, "詳細(xì)","", dialogWidth: 800),
- this.MakeStandardAction("School", GridActionStandardTypesEnum.BatchEdit, "批量修改","", dialogWidth: 800),
- this.MakeStandardAction("School", GridActionStandardTypesEnum.BatchDelete, "批量刪除","", dialogWidth: 800),
- this.MakeStandardAction("School", GridActionStandardTypesEnum.Import, "導(dǎo)入","", dialogWidth: 800),
- this.MakeStandardExportAction(null,false,ExportEnum.Excel)
- };
- }
- protected override IEnumerable<IGridColumn<School>> InitGridHeader()
- {
- return new List<GridColumn<School>>{
- this.MakeGridHeader(x => x.SchoolCode),
- this.MakeGridHeader(x => x.SchoolName),
- this.MakeGridHeader(x => x.SchoolType),
- this.MakeGridHeaderAction(width: 200)
- };
- }
- public override IOrderedQueryable<School> GetSearchQuery()
- {
- var query = DC.Set<School>().OrderBy(x => x.ID);
- return query;
- }
- }
-
上面代碼建立了一個簡單的ListVM,它沒有任何搜索條件,默認(rèn)每頁20行查詢所有School的數(shù)據(jù),并使用輸出列表同樣的邏輯來完成導(dǎo)出Excel。同時它定義了新建,修改等按鈕
RecordsPerPage屬性可以設(shè)置每頁行數(shù),默認(rèn)是20行,NeedPage屬性可以設(shè)定是否要進(jìn)行分頁
MakeStandardExportAction的作用是在列表上生成一個導(dǎo)出按鈕
MakeGridHeaderAction的作用是在列表上增加一個操作列,操作列中出現(xiàn)的按鈕是在InitGridAction中定義的所有ShowInRow為true的動作。當(dāng)使用MakeStandardAction創(chuàng)建默認(rèn)動作時,修改,刪除和詳細(xì)都是每行顯示的
使用BasePagedListVM
在Controller中使用BasePagedListVM的步驟也是一目了然,請看下面的例子
- #region 搜索
- [ActionDescription("搜索")]
- public ActionResult Index()
- {
- var vm = CreateVM<SchoolListVM>();
- return PartialView(vm);
- }
- #endregion
-
必須使用CreateVM函數(shù)來創(chuàng)建ViewModel,而不要直接new。CreateVM函數(shù)會將當(dāng)前Controller的Session,ModelState等信息傳遞給VM,并進(jìn)行一些框架內(nèi)部的操作
Searcher
上面的例子并沒有任何搜索條件,真實場景中這種情況是很少的,要在列表中加入搜索條件,有兩個步驟
1. 定義一個繼承自BaseSearcher的類,將搜索條件作為類的屬性,并把ListVM類的第二個泛型變量設(shè)定為這個Searcher
2. 在GetSearchQuery函數(shù)中使用Searcher的數(shù)據(jù)來編寫查詢語句
比如建立一個下面的Searcher用于搜索學(xué)校數(shù)據(jù)
- public class SchoolSearcher : BaseSearcher
- {
- [Display(Name = "學(xué)校編碼")]
- public String SchoolCode { get; set; }
- [Display(Name = "學(xué)校名稱")]
- public String SchoolName { get; set; }
- [Display(Name = "學(xué)校類型")]
- public SchoolTypeEnum? SchoolType { get; set; }
- }
-
然后修改SchoolListVM,將它的定義和SchoolSearcher關(guān)聯(lián) public class SchoolListVM : BasePagedListVM<School, SchoolSearcher>
最后修改GetSearchQuery,使用Searcher的數(shù)據(jù)
- public override IOrderedQueryable<School> GetSearchQuery()
- {
- var query = DC.Set<School>()
- .CheckContain(Searcher.SchoolCode, x=>x.SchoolCode)
- .CheckContain(Searcher.SchoolName, x=>x.SchoolName)
- .CheckEqual(Searcher.SchoolType, x=>x.SchoolType)
- .OrderBy(x => x.ID);
- return query;
- }
-
CheckContain,CheckEqual等是框架提供的便捷函數(shù),用來替換where語句,并優(yōu)化生成的查詢語句
自定義輸出
實際情況中,我們并不總是輸出數(shù)據(jù)庫中的原始數(shù)據(jù),很多時候我們需要對原有數(shù)據(jù)進(jìn)行加工,轉(zhuǎn)換,或者顯示數(shù)據(jù)庫中并不存在的新列
請看下面的例子
- public class SchoolListVM : BasePagedListVM<School_View, SchoolSearcher>
- {
- protected override List<GridAction> InitGridAction()
- {
- return new List<GridAction>
- {
- this.MakeStandardAction("School", GridActionStandardTypesEnum.Create, "新建","", dialogWidth: 800),
- this.MakeStandardAction("School", GridActionStandardTypesEnum.Edit, "修改","", dialogWidth: 800),
- this.MakeStandardAction("School", GridActionStandardTypesEnum.Delete, "刪除", "",dialogWidth: 800),
- this.MakeStandardAction("School", GridActionStandardTypesEnum.Details, "詳細(xì)","", dialogWidth: 800),
- this.MakeStandardAction("School", GridActionStandardTypesEnum.BatchEdit, "批量修改","", dialogWidth: 800),
- this.MakeStandardAction("School", GridActionStandardTypesEnum.BatchDelete, "批量刪除","", dialogWidth: 800),
- this.MakeStandardAction("School", GridActionStandardTypesEnum.Import, "導(dǎo)入","", dialogWidth: 800),
- this.MakeStandardExportAction(null,false,ExportEnum.Excel)
- };
- }
- protected override IEnumerable<IGridColumn<School_View>> InitGridHeader()
- {
- return new List<GridColumn<School_View>>{
- this.MakeGridHeader(x => x.SchoolCode),
- this.MakeGridHeader(x => x.SchoolName),
- this.MakeGridHeader(x => x.SchoolType),
- this.MakeGridHeader(x => x.Total).SetFormat(TotalFormat),
- this.MakeGridHeaderAction(width: 200)
- };
- }
- private string TotalFormat(School_View entity, object val)
- {
- return entity.SchoolCode + "," + entity.SchoolName + "," + entity.SchoolType;
- }
- public override IOrderedQueryable<School_View> GetSearchQuery()
- {
- var query = DC.Set<School>()
- .CheckContain(Searcher.SchoolCode, x=>x.SchoolCode)
- .CheckContain(Searcher.SchoolName, x=>x.SchoolName)
- .CheckEqual(Searcher.SchoolType, x=>x.SchoolType)
- .Select(x => new School_View
- {
- ID = x.ID,
- SchoolCode = x.SchoolCode,
- SchoolName = x.SchoolName,
- SchoolType = x.SchoolType
- })
- .OrderBy(x => x.ID);
- return query;
- }
- }
- public class School_View : School{
- public string Total {get;set;}
- }
-
上面代碼中,我們新建了School_View類,用于擴(kuò)展School來顯示模型中并不存在的字段
然后我們通過在列上調(diào)用SetFormat方法來指定一個函數(shù)用于返回這列需要顯示的數(shù)據(jù)
SetFormat接受的方法可以直接返回string,也可以返回List<ColumnFormatInfo>
ColumnFormatInfo的作用主要是方便我們生成一些按鈕顯示在列表中,如果返回string的話,比如我們需要根據(jù)某列數(shù)據(jù)生成一個按鈕,點(diǎn)擊按鈕后彈出指定url的窗口,可能需要開發(fā)人員編寫大量js
ColumnFormatInfo提供了很多擴(kuò)展函數(shù)方便開發(fā)人員生成各種按鈕,比如MakeDialogButton,MakeScriptButton,MakeDownloadButton等
以下例子演示了一個SetFormat的函數(shù),它根據(jù)用戶的照片ID,生成兩個按鈕,一個用來下載,一個用來彈出窗口直接顯示照片
- private List<ColumnFormatInfo> PhotoIdFormat(FrameworkUser_View entity, object val)
- {
- return new List<ColumnFormatInfo>
- {
- ColumnFormatInfo.MakeDownloadButton(ButtonTypesEnum.Button,entity.PhotoId),
- ColumnFormatInfo.MakeViewButton(ButtonTypesEnum.Button,entity.PhotoId,640,480),
- };
- }
-
表格中使用控件
在項目中,我們還經(jīng)常遇到使用grid來批量編輯數(shù)據(jù)的情況,
請看下面的例子
- public class StudentListVm : BasePagedListVM<Student,StudentSearcher>
- {
- public StudentListVm()
- {
- DetailGridPrix = "Students";
- }
- protected override List<GridAction> InitGridAction()
- {
- return new List<GridAction>
- {
- this.MakeStandardAction("Employee", GridActionStandardTypesEnum.AddRow, ""),
- this.MakeStandardAction("Employee", GridActionStandardTypesEnum.RemoveRow, ""),
- };
- }
- protected override IEnumerable<IGridColumn<Student>> InitGridHeader()
- {
- return new List<GridColumn<Student>>{
- this.MakeGridHeader(x => x.LoginName).SetEditType(EditTypeEnum.TextBox),
- this.MakeGridHeader(x => x.Name).SetEditType(EditTypeEnum.TextBox),
- this.MakeGridHeader(x => x.Sex).SetEditType(EditTypeEnum.ComboBox, typeof(Models.SexEnum).ToListItems(pleaseSelect:true)),
- this.MakeGridHeader(x => x.CellPhone).SetEditType(EditTypeEnum.TextBox),
- this.MakeGridHeader(x=>x.IsValid).SetEditType(EditTypeEnum.CheckBox)
- };
- }
- public override IOrderedQueryable<Student> GetSearchQuery()
- {
- List<Student> data = new List<Student>
- {
- new Student{ LoginName = "zhangsan", Name="張三", Sex= Models.SexEnum.Male, CellPhone="13012213483", ExcelIndex = 0, IsValid = true, ID = new Guid("6F5C2D15-4871-4083-B269-06F456A4F1B6")},
- new Student{ LoginName = "lisi", Name="李四", Sex= Models.SexEnum.Male, CellPhone="13075829654", ExcelIndex = 1, IsValid = false, ID = new Guid("9C7BC358-B8BD-4547-AFC1-11BF6F2B608B")},
- new Student{ LoginName = "wangwu", Name="王五", Sex= Models.SexEnum.Male, CellPhone="13098635100", ExcelIndex = 2, IsValid = true, ID = new Guid("3BF9217C-1ACF-4D80-9899-42CFDA4C8746")},
- new Student{ LoginName = "zhaoliu", Name="趙六", Sex= Models.SexEnum.Female, CellPhone="13035698123", ExcelIndex = 3, IsValid = false, ID = new Guid("0C7F6A24-A08D-46BD-86AC-6B6A391A9F04")},
- };
- var query = data.AsQueryable().Where(x=>
- (string.IsNullOrEmpty(Searcher.LoginName) || x.LoginName.Contains(Searcher.LoginName)) &&
- (string.IsNullOrEmpty(Searcher.Name) || x.Name.Contains(Searcher.Name)) &&
- (Searcher.Sex == null || x.Sex == Searcher.Sex)
- )
- .OrderBy(x => x.ExcelIndex);
- return query;
- }
- }
-
上面代碼中,我們首先指定了DetailGridPrix字段,這個字段是告訴框架提交之后應(yīng)該將數(shù)據(jù)保存在哪里,比如我們設(shè)定DetailGridPrix="Students",那么提交的時候,框架會使用Students[0].Id,Students[0].Name,Students[1].Id,Students[1].Name這種形式來提交數(shù)據(jù)
然后在InitGridAction方法中,我們定義了兩個按鈕,分別是AddRow和RemoveRow類型。這兩個按鈕負(fù)責(zé)動態(tài)給Grid添加或者刪除行
最后再InitGridHeader方法中,我們使用SetEditType方法給每一列定義需要使用的控件
主要屬性
屬性 | 描述 |
---|---|
Searcher | 獲取關(guān)聯(lián)的搜索類 |
EntityList | 獲取搜索結(jié)果 |
RecordsPerPage | 分頁行數(shù),默認(rèn)時20條 |
NeedPage | 是否需要分頁,默認(rèn)為true |
SearcherMode | 搜索模式,用來判斷當(dāng)前搜索是普通列表,導(dǎo)出,批量操作等 |
屬性 | 描述 |
---|
Searcher | 獲取關(guān)聯(lián)的搜索類 |
EntityList | 獲取搜索結(jié)果 |
RecordsPerPage | 分頁行數(shù),默認(rèn)時20條 |
NeedPage | 是否需要分頁,默認(rèn)為true |
SearcherMode | 搜索模式,用來判斷當(dāng)前搜索是普通列表,導(dǎo)出,批量操作等 |
主要函數(shù)
函數(shù) | 描述 |
---|---|
InitGridAction() | 初始化列表需要顯示的操作按鈕 |
MakeStandardAction() | 根據(jù)默認(rèn)設(shè)置生成添加,修改,刪除,詳細(xì),導(dǎo)入的按鈕 |
MakeStandardExportAction() | 生成導(dǎo)出Excel的按鈕 |
InitGridHeader() | 初始化列表需要顯示的列 |
GetSearchQuery() | 獲取查詢語句 |
GetExportQuery() | 獲取導(dǎo)出時的查詢語句,如不重寫,默認(rèn)調(diào)用GetSearchQuery |
GetCheckedExportQuery() | 獲取勾選指定數(shù)據(jù)導(dǎo)出時的查詢語句,如不重寫,默認(rèn)調(diào)用GetSearchQuery |
GetSelectorQuery() | 獲取選擇器模式下的查詢語句,如不重寫,默認(rèn)調(diào)用GetSearchQuery |
GetBatchQuery | 獲取批量模式下的查詢語句,如不重寫,默認(rèn)調(diào)用GetSearchQuery |
函數(shù) | 描述 |
---|
InitGridAction() | 初始化列表需要顯示的操作按鈕 |
MakeStandardAction() | 根據(jù)默認(rèn)設(shè)置生成添加,修改,刪除,詳細(xì),導(dǎo)入的按鈕 |
MakeStandardExportAction() | 生成導(dǎo)出Excel的按鈕 |
InitGridHeader() | 初始化列表需要顯示的列 |
GetSearchQuery() | 獲取查詢語句 |
GetExportQuery() | 獲取導(dǎo)出時的查詢語句,如不重寫,默認(rèn)調(diào)用GetSearchQuery |
GetCheckedExportQuery() | 獲取勾選指定數(shù)據(jù)導(dǎo)出時的查詢語句,如不重寫,默認(rèn)調(diào)用GetSearchQuery |
GetSelectorQuery() | 獲取選擇器模式下的查詢語句,如不重寫,默認(rèn)調(diào)用GetSearchQuery |
GetBatchQuery | 獲取批量模式下的查詢語句,如不重寫,默認(rèn)調(diào)用GetSear |
更多建議: