列表及導(dǎo)入導(dǎo)出

2019-04-04 17:40 更新

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é)校模型的定義可以參考第一個模塊

  1. public class SchoolListVM : BasePagedListVM<School, BaseSearcher>
  2. {
  3. protected override List<GridAction> InitGridAction()
  4. {
  5. return new List<GridAction>
  6. {
  7. this.MakeStandardAction("School", GridActionStandardTypesEnum.Create, "新建","", dialogWidth: 800),
  8. this.MakeStandardAction("School", GridActionStandardTypesEnum.Edit, "修改","", dialogWidth: 800),
  9. this.MakeStandardAction("School", GridActionStandardTypesEnum.Delete, "刪除", "",dialogWidth: 800),
  10. this.MakeStandardAction("School", GridActionStandardTypesEnum.Details, "詳細(xì)","", dialogWidth: 800),
  11. this.MakeStandardAction("School", GridActionStandardTypesEnum.BatchEdit, "批量修改","", dialogWidth: 800),
  12. this.MakeStandardAction("School", GridActionStandardTypesEnum.BatchDelete, "批量刪除","", dialogWidth: 800),
  13. this.MakeStandardAction("School", GridActionStandardTypesEnum.Import, "導(dǎo)入","", dialogWidth: 800),
  14. this.MakeStandardExportAction(null,false,ExportEnum.Excel)
  15. };
  16. }
  17. protected override IEnumerable<IGridColumn<School>> InitGridHeader()
  18. {
  19. return new List<GridColumn<School>>{
  20. this.MakeGridHeader(x => x.SchoolCode),
  21. this.MakeGridHeader(x => x.SchoolName),
  22. this.MakeGridHeader(x => x.SchoolType),
  23. this.MakeGridHeaderAction(width: 200)
  24. };
  25. }
  26. public override IOrderedQueryable<School> GetSearchQuery()
  27. {
  28. var query = DC.Set<School>().OrderBy(x => x.ID);
  29. return query;
  30. }
  31. }

上面代碼建立了一個簡單的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的步驟也是一目了然,請看下面的例子

  1. #region 搜索
  2. [ActionDescription("搜索")]
  3. public ActionResult Index()
  4. {
  5. var vm = CreateVM<SchoolListVM>();
  6. return PartialView(vm);
  7. }
  8. #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ù)

  1. public class SchoolSearcher : BaseSearcher
  2. {
  3. [Display(Name = "學(xué)校編碼")]
  4. public String SchoolCode { get; set; }
  5. [Display(Name = "學(xué)校名稱")]
  6. public String SchoolName { get; set; }
  7. [Display(Name = "學(xué)校類型")]
  8. public SchoolTypeEnum? SchoolType { get; set; }
  9. }

然后修改SchoolListVM,將它的定義和SchoolSearcher關(guān)聯(lián) public class SchoolListVM : BasePagedListVM<School, SchoolSearcher>

最后修改GetSearchQuery,使用Searcher的數(shù)據(jù)

  1. public override IOrderedQueryable<School> GetSearchQuery()
  2. {
  3. var query = DC.Set<School>()
  4. .CheckContain(Searcher.SchoolCode, x=>x.SchoolCode)
  5. .CheckContain(Searcher.SchoolName, x=>x.SchoolName)
  6. .CheckEqual(Searcher.SchoolType, x=>x.SchoolType)
  7. .OrderBy(x => x.ID);
  8. return query;
  9. }

CheckContain,CheckEqual等是框架提供的便捷函數(shù),用來替換where語句,并優(yōu)化生成的查詢語句

自定義輸出

實際情況中,我們并不總是輸出數(shù)據(jù)庫中的原始數(shù)據(jù),很多時候我們需要對原有數(shù)據(jù)進(jìn)行加工,轉(zhuǎn)換,或者顯示數(shù)據(jù)庫中并不存在的新列

請看下面的例子

  1. public class SchoolListVM : BasePagedListVM<School_View, SchoolSearcher>
  2. {
  3. protected override List<GridAction> InitGridAction()
  4. {
  5. return new List<GridAction>
  6. {
  7. this.MakeStandardAction("School", GridActionStandardTypesEnum.Create, "新建","", dialogWidth: 800),
  8. this.MakeStandardAction("School", GridActionStandardTypesEnum.Edit, "修改","", dialogWidth: 800),
  9. this.MakeStandardAction("School", GridActionStandardTypesEnum.Delete, "刪除", "",dialogWidth: 800),
  10. this.MakeStandardAction("School", GridActionStandardTypesEnum.Details, "詳細(xì)","", dialogWidth: 800),
  11. this.MakeStandardAction("School", GridActionStandardTypesEnum.BatchEdit, "批量修改","", dialogWidth: 800),
  12. this.MakeStandardAction("School", GridActionStandardTypesEnum.BatchDelete, "批量刪除","", dialogWidth: 800),
  13. this.MakeStandardAction("School", GridActionStandardTypesEnum.Import, "導(dǎo)入","", dialogWidth: 800),
  14. this.MakeStandardExportAction(null,false,ExportEnum.Excel)
  15. };
  16. }
  17. protected override IEnumerable<IGridColumn<School_View>> InitGridHeader()
  18. {
  19. return new List<GridColumn<School_View>>{
  20. this.MakeGridHeader(x => x.SchoolCode),
  21. this.MakeGridHeader(x => x.SchoolName),
  22. this.MakeGridHeader(x => x.SchoolType),
  23. this.MakeGridHeader(x => x.Total).SetFormat(TotalFormat),
  24. this.MakeGridHeaderAction(width: 200)
  25. };
  26. }
  27. private string TotalFormat(School_View entity, object val)
  28. {
  29. return entity.SchoolCode + "," + entity.SchoolName + "," + entity.SchoolType;
  30. }
  31. public override IOrderedQueryable<School_View> GetSearchQuery()
  32. {
  33. var query = DC.Set<School>()
  34. .CheckContain(Searcher.SchoolCode, x=>x.SchoolCode)
  35. .CheckContain(Searcher.SchoolName, x=>x.SchoolName)
  36. .CheckEqual(Searcher.SchoolType, x=>x.SchoolType)
  37. .Select(x => new School_View
  38. {
  39. ID = x.ID,
  40. SchoolCode = x.SchoolCode,
  41. SchoolName = x.SchoolName,
  42. SchoolType = x.SchoolType
  43. })
  44. .OrderBy(x => x.ID);
  45. return query;
  46. }
  47. }
  48. public class School_View : School{
  49. public string Total {get;set;}
  50. }

上面代碼中,我們新建了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,生成兩個按鈕,一個用來下載,一個用來彈出窗口直接顯示照片

  1. private List<ColumnFormatInfo> PhotoIdFormat(FrameworkUser_View entity, object val)
  2. {
  3. return new List<ColumnFormatInfo>
  4. {
  5. ColumnFormatInfo.MakeDownloadButton(ButtonTypesEnum.Button,entity.PhotoId),
  6. ColumnFormatInfo.MakeViewButton(ButtonTypesEnum.Button,entity.PhotoId,640,480),
  7. };
  8. }

表格中使用控件

在項目中,我們還經(jīng)常遇到使用grid來批量編輯數(shù)據(jù)的情況,

請看下面的例子

  1. public class StudentListVm : BasePagedListVM<Student,StudentSearcher>
  2. {
  3. public StudentListVm()
  4. {
  5. DetailGridPrix = "Students";
  6. }
  7. protected override List<GridAction> InitGridAction()
  8. {
  9. return new List<GridAction>
  10. {
  11. this.MakeStandardAction("Employee", GridActionStandardTypesEnum.AddRow, ""),
  12. this.MakeStandardAction("Employee", GridActionStandardTypesEnum.RemoveRow, ""),
  13. };
  14. }
  15. protected override IEnumerable<IGridColumn<Student>> InitGridHeader()
  16. {
  17. return new List<GridColumn<Student>>{
  18. this.MakeGridHeader(x => x.LoginName).SetEditType(EditTypeEnum.TextBox),
  19. this.MakeGridHeader(x => x.Name).SetEditType(EditTypeEnum.TextBox),
  20. this.MakeGridHeader(x => x.Sex).SetEditType(EditTypeEnum.ComboBox, typeof(Models.SexEnum).ToListItems(pleaseSelect:true)),
  21. this.MakeGridHeader(x => x.CellPhone).SetEditType(EditTypeEnum.TextBox),
  22. this.MakeGridHeader(x=>x.IsValid).SetEditType(EditTypeEnum.CheckBox)
  23. };
  24. }
  25. public override IOrderedQueryable<Student> GetSearchQuery()
  26. {
  27. List<Student> data = new List<Student>
  28. {
  29. new Student{ LoginName = "zhangsan", Name="張三", Sex= Models.SexEnum.Male, CellPhone="13012213483", ExcelIndex = 0, IsValid = true, ID = new Guid("6F5C2D15-4871-4083-B269-06F456A4F1B6")},
  30. new Student{ LoginName = "lisi", Name="李四", Sex= Models.SexEnum.Male, CellPhone="13075829654", ExcelIndex = 1, IsValid = false, ID = new Guid("9C7BC358-B8BD-4547-AFC1-11BF6F2B608B")},
  31. new Student{ LoginName = "wangwu", Name="王五", Sex= Models.SexEnum.Male, CellPhone="13098635100", ExcelIndex = 2, IsValid = true, ID = new Guid("3BF9217C-1ACF-4D80-9899-42CFDA4C8746")},
  32. new Student{ LoginName = "zhaoliu", Name="趙六", Sex= Models.SexEnum.Female, CellPhone="13035698123", ExcelIndex = 3, IsValid = false, ID = new Guid("0C7F6A24-A08D-46BD-86AC-6B6A391A9F04")},
  33. };
  34. var query = data.AsQueryable().Where(x=>
  35. (string.IsNullOrEmpty(Searcher.LoginName) || x.LoginName.Contains(Searcher.LoginName)) &&
  36. (string.IsNullOrEmpty(Searcher.Name) || x.Name.Contains(Searcher.Name)) &&
  37. (Searcher.Sex == null || x.Sex == Searcher.Sex)
  38. )
  39. .OrderBy(x => x.ExcelIndex);
  40. return query;
  41. }
  42. }

上面代碼中,我們首先指定了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


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號