0
<c:forEach var="expData" items="${expenseDataList}">
    <tr>
        <td>
            <div class="custom-control custom-checkbox">
                <label><aui:input
                            cssClass="custom-control-input expense select-all"
                            type="checkbox" data-amount="${expData.expenseAmount}" data-expenseid="${expData.expenseId}"
                            id="expenseCheckbox" name="expenseCheckbox" label="" /> 
                </label>
            </div>
        </td>
   ....                         
        <td>
            <c:forEach var="previewUrl1" items="${previewUrl}">
                <aui:button icon=" icon-download-alt"
                            style="border:none; background-color: #1E47C2; color:white"
                            data-previewurl="${previewUrl1}" cssClass="download"
                            name="downloadButton" id="downloadButton" />
            </c:forEach>
        </td>
    </tr>
</c:forEach>

I am using inside and It is creating 2 download button because I am iterating another near to download button.. I am getting the value inside previewUrl1 but it is creating 2 button as it is iterating twice because inside another

This is my portlet side code

long fileEntryId = 0L;
String previewURL = StringPool.BLANK;

List<String> s1=new ArrayList<String>();
try { 
    for (int i = 0; i < expenseDataList.size(); i++) {
         fileEntryId = expenseDataList.get(i).getFileEntryId();
         if (fileEntryId > 0) {
            FileEntry fileEntry = DLAppLocalServiceUtil.getFileEntry(fileEntryId);
            previewURL = DLUtil.getPreviewURL(fileEntry, fileEntry.getFileVersion(), themeDisplay, StringPool.BLANK);
        }           
        renderRequest.setAttribute("previewUrl", previewURL);
        s1.add(previewURL);

        LOG.info("File Entries"+fileEntryId);
        LOG.info("Preview URl " + previewURL);
    }
LOG.info(s1);
renderRequest.setAttribute("previewUrl", s1);
} catch (Exception e) {
    e.printStackTrace();
}
Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
Tirth Timaniya
  • 184
  • 1
  • 1
  • 16

1 Answers1

0

I don't fully understand your description of what happens. But in case there are multiple iterations of your inner loop, you're generating repeated id attributes in two cases: In your outer loop for the aui:input, in your inner loop for the aui:button. According to the HTML rules, the id must be unique, or you'll get undefined behavior (or every element but the first one being ignored)

Edit:

In your code, you use renderRequest.setAttribute("previewUrl", previewURL);, e.g. you set exactly one previewURL. Later, you change that attribute to a list, through renderRequest.setAttribute("previewUrl", s1); - don't do that: It makes your code unmaintainable and tricks readers (like me or others here) into thinking that previewURL is a single value when indeed it's list.

That being said: It seems that you have two lists with the same number of entries, and each element in one list refers to the element with the same index in the other list. And if you nest those lists, you'll naturally show all elements of list 2 for each element of list 1. Thus: Don't nest loops. You'll need only one loop, and access the corresponding element from the other loop directly by index.

I don't have JSTL in my muscle memory: Within the remaining (outer) forEach, keep track of the index (through varStatus, see this question on how to do it) and use this index to access the matching element in your second list.

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
  • Thank you for answering ...I am trying to iterating one c:foreach inside another c:foreach but due to that is creating 2 buttons but i want only 1 button.. – Tirth Timaniya May 23 '18 at 04:28