jQuery高级应用:优化Web应用程序的最后绝招(4)

来源:互联网 发布:手机帝国 人工智能 编辑:程序博客网 时间:2024/05/25 20:00

第二个示例Web应用程序

我将使用另一个小部件解决本文的最后3个问题,并且在深入研究其代码之前展示和解释它。这个401k小部件并不陌生,因为您已经在前面的文章见过它(参见参考资料部分获取这些文章的链接)。不过,这回有个微妙的不同之处,因为我在同一个页面上两次添加了这个小部件。它被添加到两个不同的表中。这将带来几个有趣的地方。图3显示了这个小部件:

第二个示例Web应用程序

在这个小部件中,我正在做几件事情。第一件是计算文本字段之和并确定它们是否为100。如果它们的和不为100,我将向用户显示一个错误,提示他们没有正确使用该小部件。第二,我在每个选项获取输入之后对选项进行排序。通过这种方式,百分比最高的投资分配将一直出现在表的顶部。这可以在图3中看到,它按百分比对选项进行排序。最后,为了让它更酷,我添加了一些条带。

用于生产这个小部件的HTML代码出奇地简单。清单8详细地显示了这个小部件。

  1. <p><tablewidthtablewidth=300class="percentSort"cellpadding=0cellspacing=0> 
  2. <tbody> 
  3. <tr><td>S&P500Index</td> 
  4. <td><inputtypeinputtype=text>%</td></tr> 
  5. <tr><td>Russell2000Index</td> 
  6. <td><inputtypeinputtype=text>%</td></tr> 
  7. <tr><td>MSCIInternationalIndex</td> 
  8. <td><inputtypeinputtype=text>%</td></tr> 
  9. <tr><td>MSCIEmergingMarketIndex</td> 
  10. <td><inputtypeinputtype=text>%</td></tr> 
  11. <tr><td>REITIndex</td> 
  12. <td><inputtypeinputtype=text>%</td></tr> 
  13. </tbody> 
  14. <tfoot> 
  15. </tfoot> 
  16. </table> 

用jQuery设置小部件

以上的一小段HTML直接引入了这部分内容,本小节关注如何在jQuery中设置小部件,以及所需的所有代码。要将事件附加到页面元素或在特定情况下需要添加类时,通常需要这样做。有时候还需要更进一步。这些小部件的所有设置代码都是jQuery代码。

我可以提供关于角色分离的理论,让HTML设计师和JavaScript程序员各自完成自己的工作,但是您们可能已经多次听到这种陈词。在这里我仅添加另一样东西,即“类修饰”,这是很多插件创作者都使用的。看看清单8中的HTML代码,仅通过将一个percentSort类添加到表,您就可以显著改变表的功能和外观。这是小部件设计的目标,让添加和删除小部件就像向小部件添加类一样简单。

让我们遵循我曾用jQuery设置小部件的几个步骤。通过查看这些步骤,您可以看到清单9中的设计模式是如何出现的。

  1. $(document).ready(function(){  
  2.  
  3. //thefirststepistofindallthetablesonthepagewith  
  4. //aclassofpercentSort.Theseareallthetableswewantto  
  5. //convertintoourwidget.  
  6. //Afterwefindthem,weneedtoloopthroughthemandtakesome  
  7. //actionsonthem  
  8. //Attheconclusionofthisblockofcode,eachtablethat'sgoingto  
  9. //beapercentSortwidgetwillhavebeentransformed  
  10.  
  11. $("table.percentSort").each(function(i){  
  12.  
  13. //eachtableneedsauniqueID,fornamespaceissues(discussedlater)  
  14. //wecansimplycreateauniqueIDfromtheloopcounter  
  15.  
  16. $(this).attr("id","percentSort-"+i);  
  17.  
  18. //withineachtable,let'shighlighteveryotherrowinthetable,to  
  19. //giveitthat"zebra"look  
  20.  
  21. $(this).find("tbody>tr").filter(":odd").addClass("highlight");  
  22.  
  23. //becauseeachtableneedstoshowthe"Total"totheuser,let'screateanew  
  24. //sectionofthetableinthefooter.We'lladdarowinthetablefooter  
  25. //todisplaythewords"Total"andaspanfortheupdatedcount.  
  26.  
  27. $("#"+$(this).attr("id")+"tfoot")  
  28. .append("<tr><td>Total</td><td> 
  29. <span></span>%</td></tr>");  
  30.  
  31. //finally,let'saddtheCLASSof"percentTotal"tothespanwejust  
  32. //createdabove.We'llusethisinformationlatertodisplay  
  33. //theupdatedtotals  
  34.  
  35. $("#"+$(this).attr("id")+"tfootspan").addClass("percentTotal");  
  36. });  
  37.  
  38. //nowthesecondstep,afterwe'vecompletedsettingupthetablesthemselves  
  39. //istosetuptheindividualtablerows.  
  40. //Wecansimilarlysortthrougheachofthem,takingtheappropriateactions  
  41. //oneachoftheminturn.  
  42. //Uponcompletionofthisblockofcode,eachrowineachtablewillbe  
  43. //transformedforourwidget  
  1. $("table.percentSorttbody>tr").each(function(i){  
  2.  
  3. //getthenamespace(tobediscussedinthenextsection)  
  4.  
  5. varNAMESPACE=$(this).parents("table.percentSort").attr("id");  
  6.  
  7. //attachauniqueIDtothisrow.Wecanusetheloopcounter  
  8. //toensuretheIDisuniqueonthepage(whichisamustoneverypage)  
  9.  
  10. $(this).attr("id","row"+i);  
  11.  
  12. //now,withinthisrowofthetable,weneedtofindthetextinput,because  
  13. //weneedtoattachaclasstothem.Weutilizethenamespace,andalso  
  14. //findthe:textwithinthetablerow,andthenattachthecorrectclass  
  15.  
  16. $("#"+$(this).attr("id")+":text").addClass("percent");  
  17.  
  18. //Finally,weattachauniqueIDtoeachofthetextinputs,andwedothisby  
  19. //makingitacombinationofthetablenameandtherowname.  
  20.  
  21. $("#"+$(this).attr("id")+".percent").  
  22. attr("id",NAMESPACE+"-"+$(this).attr("id"));  
  23. });  
  24. //Finally,becauseweknowweonlywantnumericalinputs,werestrictthetextentry  
  25. //tojustnumbers.Wemustdothisnow,becauseupuntilthispoint,thepage  
  26. //containednoelementswiththeCLASS"percent"  
  27. $(".percent").numeric();  

如您从这个例子中见到的一样,可以通过jQuery代码向HTML代码引入大量功能。这种类型的设计的好处是很明显的。同样,遵循角色分离、代码重用等也是非常有益的。您还将在小部件插件中看到这种类型的设计,因为它将简单的HTML代码转变成适用于插件的小部件。最重要的是,这也是您在这里需要完成的任务,即编写一个插件来将一个简单的表转变成排序和汇总表。

记住:尽量多使用jQuery代码进行设置,并且尽可能少使用HTML。