Created
May 24, 2013 03:15
-
-
Save yicone/5641071 to your computer and use it in GitHub Desktop.
当初拍脑袋想出来的组合筛选排序+分页的实现。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// 生成_ListBar 的各项url | |
/// </summary> | |
/// <param name="name"></param> | |
/// <param name="value"></param> | |
/// <param name="baseUrl"></param> | |
/// <returns></returns> | |
private string PrepareUrl4ListBar(string name, string value, string baseUrl) | |
{ | |
string newUrl; | |
string patternFormat; | |
string pattern; | |
Regex r; | |
if (string.Equals(name, "travelType", StringComparison.InvariantCultureIgnoreCase)) | |
{ | |
pattern = @"(sijia|ziyou)"; | |
r = new Regex(pattern, RegexOptions.IgnoreCase); | |
string travelType = value == "14" ? "sijia" : "ziyou"; | |
if (r.IsMatch(baseUrl)) | |
{ | |
newUrl = r.Replace(baseUrl, travelType); | |
} | |
else | |
{ | |
if (baseUrl.Contains("?")) | |
{ | |
var segs = baseUrl.Split('?'); | |
newUrl = string.Format("{0}{1}/?{2}", segs[0], travelType, segs[1]); | |
} | |
else | |
{ | |
newUrl = string.Format("{0}{1}/", baseUrl, travelType); | |
} | |
} | |
} | |
else | |
{ | |
// todo: 城市编码根据DepartCity.All()生成 | |
patternFormat = @"{0}=(true|false|[\d]+|BJS|SHA|CAN|CTU|HKG|OTH|null)?"; | |
pattern = string.Format(patternFormat, name); | |
r = new Regex(pattern, RegexOptions.IgnoreCase); | |
string pair = string.Format("{0}={1}", name, value); | |
if (string.Equals(name, "pageIndex", StringComparison.InvariantCultureIgnoreCase) && value == "0") | |
{ | |
pair = ""; | |
} | |
if (r.IsMatch(baseUrl)) | |
{ | |
newUrl = r.Replace(baseUrl, pair); | |
} | |
else | |
{ | |
newUrl = string.Format("{0}{1}{2}", baseUrl, baseUrl.Contains("?") ? "&" : "?", pair); | |
} | |
} | |
#region 刷新分页组件 | |
patternFormat = @"{0}=(true|false|[\d]+)?&?"; | |
// 切换旅游型态时,刷新分页组件 | |
if (string.Equals(name, "travelType", StringComparison.OrdinalIgnoreCase)) | |
{ | |
pattern = string.Format(patternFormat, "pageIndex"); | |
r = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled); | |
newUrl = r.Replace(newUrl, ""); | |
} | |
// 切换出发城市时,刷新分页组件 | |
if (string.Equals(name, "departureCity", StringComparison.OrdinalIgnoreCase)) | |
{ | |
pattern = string.Format(patternFormat, "pageIndex"); | |
r = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled); | |
newUrl = r.Replace(newUrl, ""); | |
} | |
// 切换排序项时,刷新分页组件 | |
if (string.Equals(name, "sort", StringComparison.OrdinalIgnoreCase)) | |
{ | |
pattern = string.Format(patternFormat, "pageIndex"); | |
r = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled); | |
newUrl = r.Replace(newUrl, ""); | |
} | |
#endregion 刷新分页组件 | |
return newUrl; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PagerModel | |
{ | |
private bool _enableMore = true; | |
private bool _inReverserOrder = true; | |
private bool _textVisible = true; | |
public string BaseUrl { get; set; } | |
/// <summary> | |
/// 启用“更多” | |
/// </summary> | |
public bool EnableMore | |
{ | |
get { return _enableMore; } | |
set { _enableMore = value; } | |
} | |
public Func<string, string, string, string> GetUrl { get; set; } | |
/// <summary> | |
/// 以反序构造 | |
/// </summary> | |
public bool InReverserOrder | |
{ | |
get { return _inReverserOrder; } | |
set { _inReverserOrder = value; } | |
} | |
public int PageCount { get; set; } | |
public int PageIndex { get; set; } | |
public bool TextVisible | |
{ | |
get { return _textVisible; } | |
set { _textVisible = value; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1 | |
PagerModel.BaseUrl = Url.Action("action", "controller", new { /* 所有传入的参数 */ }); | |
PagerModel.GetUrl = PrepareUrl4ListBar; | |
// 2 | |
PagerModel.GetUrl = (name, value, baseUrl) => | |
{ | |
string url = string.Format("javascript:showPhoto({0});", value); | |
return url; | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment