慣例:
我是溫浩然:
先說一下兩種的區別,上圖,改版之前。
後來的效果是:
很明顯的,後來的是分組織結構顯示的,這裡用到的技術,就是C標籤
先貼代碼:
<c:forEach items="${babyStages}" var="PagebabyStage" varStatus="status"><c:if test="${status.index%10==0}"><li data-projectid="${PagebabyStage.stageId}" class="open"><a href="#" class="dropdown-toggle"> <i class="icon-list"></i> <span class="menu-text">${status.index+1}---${status.index+10}</span></a><ul class="submenu" <c:if test="${pbstage.stageId==stageOne.stageId}">style="display: block;"</c:if> ><c:forEach items="${babyStages}" begin="${status.index}" end="${status.index+9}" var="pbstage" varStatus="cstatus"><li class="category <c:if test="${pbstage.stageId==stageOne.stageId}">active</c:if> project" data-categoryid="${pbstage.stageId}"><a href="<spring:url value='/card/showCard/${pbstage.stageId}'/>"> <i class="icon-double-angle-right"></i>${pbstage.name }</a></li></c:forEach></ul></li></c:if></c:forEach>
foreach中嵌套if其中再嵌套foreach。
後台傳到前台的是babyStages這個list集合,在前台中,通過c:foreach來進行迴圈解析,
就會生出第一個圖的效果。
如果想生出第二個圖的效果,就是上面的代碼了。
首先,需要迴圈產生這個列表,就是1-10,11-20,21-30……
用的是c:if進行判斷。其中,用到了c:foreach中的 varStatus屬性的index。這個屬性,可以百度一下c:foreach。有詳細介紹的,我給你推薦個網址:
http://luoke920.iteye.com/blog/258815
status.index是擷取當前迭代從0開始的索引
然後回到我這裡來。
然後說c:if
我這裡用到的c:if是迴圈條件,是想生出整10 的列表。
所以,c:if中的條件就是
<c:if test="${status.index%10==0}">迭代從0開始,也就是第一次迴圈,因為是從0開始的,其實是第一個。是第1-到第10個
當第二次迴圈,就是從10 ,其實就是11開始。
然後,進入迴圈之後呢,再讓他進行c:foreach迴圈,來產生下面的每一項
<c:forEach items="${babyStages}" begin="${status.index}" end="${status.index+9}" var="pbstage" varStatus="cstatus"><li class="category <c:if test="${pbstage.stageId==stageOne.stageId}">active</c:if> project" data-categoryid="${pbstage.stageId}"><a href="<spring:url value='/card/showCard/${pbstage.stageId}'/>"> <i class="icon-double-angle-right"></i>${pbstage.name }</a></li></c:forEach>這個迴圈,設定了迴圈的開始項和迴圈的結束項。
begin="${status.index}" end="${status.index+9}" 當第一次進入for迴圈中,是從迭代為0,實際為1開始迴圈,到迭代為9,資料第10個。
第二次進入for迴圈,迭代10,資料11,到,迭代19,資料20.
這樣來產生的。
這裡的begin 和 end 是,c:foreach的屬性。
剛開始的static.index,則是c:foreach中的 varStatus屬性的屬性。
不知道你們看懂了沒,如果沒看懂可以聯絡我,我QQ:1286238812,備忘就寫CSDN上看見的就行。我願意與你共用我的經驗。