運用c:foreach迴圈顯示

來源:互聯網
上載者:User

慣例:

我是溫浩然:

先說一下兩種的區別,上圖,改版之前。


後來的效果是:



很明顯的,後來的是分組織結構顯示的,這裡用到的技術,就是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上看見的就行。我願意與你共用我的經驗。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.