利用img和div标签实现图像下拉列表的模拟

dropdownlist本身不支持图像列表,那么我们利用jquery来实现下拉列表的模拟。

成都创新互联-专业网站定制、快速模板网站建设、高性价比郫都网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式郫都网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖郫都地区。费用合理售后完善,十多年实体公司更值得信赖。

[[3393]]

其实很简单,不用自定义控件之类,因为我们在mvc下不能使用asp.net服务器控件,所以我们只用了div和img标签实现这个功能

1.首先介绍一下数据库表结构,这是下拉列表用到的数据

用到的表

2.创建 company,department的录入窗体

如下图,因为不是重点,不再介绍,只要注意一点,company.id为自增int,注意属性窗口中auto Generated Value为true,Auto-Sync为OnInsert,这样linq才可没错

 
 
 
 
  1. [AcceptVerbs(HttpVerbs.Post)]
  2. publicActionResult CreateCompany(string name, string brief)
  3. {
  4. string strResult = "失败";
  5. ServiceDataContext db =newServiceDataContext();
  6. Company com =newCompany();
  7. com.Name = name;
  8. com.Brief = brief;
  9. try
  10. {
  11. db.Companies.InsertOnSubmit(com);
  12. db.SubmitChanges();
  13. strResult = "成功";
  14. }
  15. catch
  16. {
  17. strResult = "失败,名称重复?";
  18. }
  19. returnJson(strResult);
  20. }

3.设计下拉菜单(这才是我想说的重点)

   
   
   
   
  1. id="imgDefault" src='<%=ViewData["selectedUrl"]%>' class="imgCss" alt=""/>
  2. id="imgArrow" src="<%=ViewData["arrowUrl"]%>" class="DropdownCss" alt=""/>
  3. id="divList" class="scrolldivHidden">
  4. <%=ViewData["imageTable"]%> div>
  5. <%=Html.Hidden("head") %>
  6. td>

这里selectedUrl为选择的图像,有默认值,arrowUrl为下拉图标,ViewData["imageTable"]是下拉列表框

这里用到的css

   
   
   
   
  1. .Visible
  2. {
  3. visibility: visible;
  4. display: block;
  5. }
  6. .Hidden
  7. {
  8. visibility: hidden;
  9. display: none;
  10. }
  11. .imgCss
  12. {
  13. cursor: hand;
  14. width: 18px;
  15. height: 18px;
  16. border: 1px solid #c3c3c3;
  17. }
  18. .DropdownCss
  19. {
  20. cursor: hand;
  21. width: 9px;
  22. height: 9px;
  23. vertical-align: top;
  24. }
  25. .scrolldivHidden
  26. {
  27. position:absolute;
  28. width: 36px;
  29. height: 50px;
  30. z-index: 1;
  31. left: 0px;
  32. top: 0x;
  33. overflow-y: auto;
  34. overflow-x:hidden;
  35. display:none;
  36. }
  37. .scrolldivVisible
  38. {
  39. position:absolute;
  40. width: 36px;
  41. height: 50px;
  42. z-index: 1;
  43. left: 0px;
  44. top: 0x;
  45. overflow-y: auto;
  46. overflow-x:hidden;
  47. display:block;
  48. }

主要我们用到jquery来实现选择等功能

这里用到jquery一个选择框的插件

   
   
   
   
  1. /*
  2. * Manipulation for HTML SELECT with jQuery
  3. * Created by Baldwin (http://www.dnnsun.com)
  4. * version: 1.0 (02/03/2009)
  5. * @requires jQuery v1.2 or later
  6. */
  7. ; (function($) {
  8. /* Clear all options */
  9. $.fn.clearSelect =function() {
  10. returnthis.each(function() {
  11. if(this.tagName == 'SELECT')this.options.length = 0;
  12. });
  13. }
  14. /* Fill the options with the object array: [{'Text':'Hello','Value':'1'}]*/
  15. $.fn.fillSelect =function(data) {
  16. returnthis.clearSelect().each(function() {
  17. if(this.tagName == 'SELECT') {
  18. vardropdownList =this;
  19. if(data && data.length > 0) {
  20. $.each(data,function(index, optionData) {
  21. varoption =newOption(optionData.Text, optionData.Value);
  22. if($.browser.msie)
  23. dropdownList.add(option);
  24. else
  25. dropdownList.add(option,null);
  26. });
  27. dropdownList.disabled =false;
  28. }
  29. else
  30. dropdownList.disabled =true;
  31. }
  32. });
  33. }
  34. /* loading when applying Ajax fillSelect */
  35. $.fn.loadSelect =function(loadText) {
  36. vardata = [{ 'Text': loadText, 'Value': ''}];
  37. this.fillSelect(data);
  38. }
  39. /* selected the target option with value */
  40. $.fn.selected =function(value) {
  41. returnthis.each(function() {
  42. if(this.tagName == 'SELECT') {
  43. varoptions =this.options;
  44. if(options.length > 0) {
  45. $.each(options,function(index, optionData) {
  46. // once match then exist loop
  47. if(optionData.value == value) {
  48. optionData.selected =true;
  49. returnfalse;
  50. }
  51. });
  52. }
  53. }
  54. });
  55. }
  56. /* TODO:selected the target option with text */
  57. $.fn.selectedText =function(text) {
  58. returnthis.each(function() {
  59. if(this.tagName == 'SELECT') {
  60. varoptions =this.options;
  61. if(options.length > 0) {
  62. $.each(options,function(index, optionData) {
  63. // once match then exist loop
  64. if(optionData.text == text) {
  65. optionData.selected =true;
  66. returnfalse;
  67. }
  68. });
  69. }
  70. }
  71. });
  72. }
  73. /* returns the selected value */
  74. $.fn.getSelected =function() {
  75. return$(this).val();
  76. }
  77. /* return the text of selected option */
  78. $.fn.getSelectedText =function() {
  79. return$(this).children("[@selected]").text();
  80. }
  81. })(jQuery);
  82. functiongetDepartment(data) { $("#department").fillSelect(data); }
  83. $("#company").change(function() {
  84. $.getJSON("GetDepartment?company=" + $("#company").val(),null, getDepartment);
  85. });

实现了联动框

下面的代码实现了下拉和选择功能

   
   
   
   
  1. $("#imgArrow,#imgDefault").toggle(function() {
  2. $("#divList").css("left", $("#imgDefault").css("left"));
  3. $("#divList").slideToggle("fast");
  4. },function() {
  5. $("#divList").css("left", $("#imgDefault").css("left"));
  6. $("#divList").slideToggle("fast");
  7. });
  8. $("#head").val($("#imgDefault").attr("src"));
  9. $("img:.imgList").click(function() {
  10. $("#imgDefault").attr("src", $(this).attr("src"));
  11. $("#divList").slideToggle("fast");
  12. $("#head").val($("#imgDefault").attr("src"));
  13. }).hover(function() {
  14. $(this).removeClass().addClass("alpha");
  15. }, (function() {
  16. $(this).removeClass();
  17. }));
  18. $("body").click(function() {
  19. $("#divList").slideUp("fast");
  20. $("#imgDefault").hover(function() {
  21. $(this).addClass("alpha");
  22. },function() {
  23. $(this).removeClass();
  24. });
  25. });

还有我们用到的action

   
   
   
   
  1. publicActionResult CreateService()
  2. {
  3. ViewData["company"] = GetCompanyList();
  4. List blank=newList ();
  5. blank.Add(GetBlankDepartment());
  6. ViewData["department"] = blank.ToArray();
  7. ViewData["head"] = GetHeadList();
  8. ViewData["selectedUrl"] = Url.Content(Head_Folder + "Head/head0.gif");
  9. ViewData["arrowUrl"] = Url.Content(Head_Folder + "/dropdown.gif");
  10. ViewData["imageTable"] = GetImageTable();
  11. returnView();
  12. }
  13. [AcceptVerbs(HttpVerbs.Get)]
  14. publicActionResult GetDepartment(int? company)
  15. {
  16. SelectListItem[] items=null;
  17. if(company!=null)
  18. items = GetDepartmentList((int)company);
  19. else
  20. {
  21. SelectListItem i=GetBlankDepartment();
  22. items=newSelectListItem[]{i};
  23. }
  24. returnJson(items);
  25. }
  26. privateSelectListItem[] GetCompanyList()
  27. {
  28. ServiceDataContext db =newServiceDataContext();
  29. varitems = from tindb.Companies
  30. selectnew
  31. {
  32. ID = t.ID,
  33. Name = t.Name,
  34. Brief = t.Brief
  35. };
  36. List s =newList ();
  37. SelectListItem b =newSelectListItem();
  38. b.Text = "--please select company--";
  39. b.Value = "";
  40. s.Add(b);
  41. foreach (variinitems)
  42. {
  43. SelectListItem item =newSelectListItem();
  44. item.Text = i.Name;
  45. item.Value = i.ID.ToString();
  46. s.Add(item);
  47. }
  48. returns.ToArray();
  49. }
  50. privateSelectListItem[] GetDepartmentList(intnCompanyId)
  51. {
  52. ServiceDataContext db =newServiceDataContext();
  53. varitems = from tindb.Departments
  54. where t.Company==nCompanyId
  55. selectnew
  56. {
  57. ID = t.ID,
  58. Name = t.Name,
  59. Brief = t.Brief
  60. };
  61. List s =newList ();
  62. SelectListItem b =newSelectListItem();
  63. b.Text = "--please select department--";
  64. b.Value = "";
  65. s.Add(b);
  66. foreach (variinitems)
  67. {
  68. SelectListItem item =newSelectListItem();
  69. item.Text = i.Name;
  70. item.Value = i.ID.ToString();
  71. s.Add(item);
  72. }
  73. returns.ToArray();
  74. }
  75. privatestring[] GetHeadList()
  76. {
  77. List head =newList ();
  78. string strServer=Url.Content("~/Content/images/Head/");
  79. string strUrlFolder = Server.MapPath(Url.Content(Head_Folder+"Head/"));
  80. string[] fileEntries = Directory.GetFiles(strUrlFolder);
  81. FileInfo fi=null;
  82. foreach (string fileinfileEntries)
  83. {
  84. fi =newFileInfo(file);
  85. if(fi.Name.ToLower().IndexOf("head")==0 && !fi.FullName.ToLower().Contains("_offline"))
  86. {
  87. head.Add(strServer+fi.Name);
  88. }
  89. }
  90. returnhead.ToArray();
  91. }
  92. privatestring GetImageTable()
  93. {
  94. System.Text.StringBuilder sbResult =newSystem.Text.StringBuilder();
  95. string[] arrImgs = GetHeadList();
  96. foreach (string strImageinarrImgs)
  97. {
  98. string strFile=strImage.Substring(strImage.LastIndexOf("/")+1);
  99. sbResult.AppendLine(string.Format("",
  100. "img_"+strFile.Remove(strFile.LastIndexOf(".")),
  101. Url.Content(strImage)
  102. ));
  103. }
  104. string strImg=sbResult.ToString();
  105. strImg=strImg.Remove(strImg.Length-5,5);
  106. returnstrImg;
  107. }
  108. }

注意紫色处,我们加了cssimgList来供jquery选择

   
   
   
   
  1. $("img:.imgList").click(function() {
  2. $("#imgDefault").attr("src", $(this).attr("src"));
  3. $("#divList").slideToggle("fast");
  4. $("#head").val($("#imgDefault").attr("src"));
  5. }).hover(function() {
  6. $(this).removeClass().addClass("alpha");
  7. }, (function() {
  8. $(this).removeClass();
  9. }));

好了,可以试试效果了。

名称栏目:利用img和div标签实现图像下拉列表的模拟
路径分享:http://www.shufengxianlan.com/qtweb/news42/301492.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联