Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to generate rows in a table dynamically in velocity template file ?

Jayashree Shetty
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 12, 2014

I have an issue "ISSUE1" and it has 3 subtasks "SUBTASK1","SUBTASK2","SUBTASK3".

I am able to display the following data in the issue view page in the format given below:

Issue Type Estimate

ISSUE1 3h

SUBTASK3 2h

The first 2 subtasks dont get added since it gets oevrwriiten by the html code in the .vm file

--------------------------------------

<style>
thead {color:blue;}
#table1,#issuetype,#originalest,#td1
{
  border:1px solid black;
}

</style>
<table id="table1" class="aui"> 
<thead>
  <tr>
     <th id="issuetype">Issue Type</th>
     <th id="originalest">Original Estimate</th>
  </tr>
</thead>
<tbody>                
   <tr>
   <td id="td1" headers="issuetype">$issuename</td>
   <td id="td1" headers="originalest">$issestimate</td>            
   </tr> 
      <tr>
       <td id="td1" headers="issuetype">$sbiname</td>
       <td id="td1" headers="originalest">$sbiestimate</td>
      </tr>  
</tbody>
</table>

------------END-------------------------

How can i avoid this? I have kept a counter $count in the vm file to know the count of subtasks created through which i can increase the row size and add it. But somehow i am not able to understand how to write the logic. Can anyone help me to write the code for the velocity file?

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
RambanamP
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 12, 2014

your modified class should be as follows

public Map getContextMap1(User user, JiraHelper jiraHelper){
	    Map map = new HashMap();
	    List<IssueDetailsBean> list=new ArrayList<IssueDetailsBean>();
	    Issue currentIssue =(Issue)jiraHelper.getContextParams().get("issue");
	    String iss_name=currentIssue.getIssueTypeObject().getName();
	    Long iss_est = currentIssue.getOriginalEstimate();
	    IssueDetailsBean issDetailBean;
	    if(iss_name.equalsIgnoreCase("PBI")){
	        //map.put("issuename", iss_name);
	        //map.put("issestimate",iss_est/hours + "h");
	    if(currentIssue.getSubTaskObjects()!=null){
	        Collection<Issue> subtasks = currentIssue.getSubTaskObjects();
	        int size = subtasks.size();
	        Iterator<Issue> enumerator1 = subtasks.iterator();
	        while(enumerator1.hasNext()){
	            Issue item = enumerator1.next();	            
	            //String issueID=item.getIssueTypeId();
	            //map.put("sbiname", item.getSummary());
	            //map.put("sbiestimate" ,item.getOriginalEstimate()/hours + "h");  
	            issDetailBean=new IssueDetailsBean(iss_name,iss_est,item.getSummary(),item.getOriginalEstimate());//pass all varible values here
	    	    list.add(issDetailBean);
	            
	        }
	    }
	    
	}
	map.put("issueDetails",list);
	return map;
	}

write bean class as follows

public class IssueDetailsBean {
	String issNname;
	Long issueEst;
	String sbiName;
	Long sbiestimate;
	public IssueDetailsBean(String issNname, Long issueEst, String sbiName, Long sbiestimate) {
		super();
		this.issNname = issNname;
		this.issueEst = issueEst;
		this.sbiName = sbiName;
		this.sbiestimate = sbiestimate;
	}
	public String getIssNname() {
		return issNname;
	}
	public void setIssNname(String issNname) {
		this.issNname = issNname;
	}
	public Long getIssueEst() {
		return issueEst;
	}
	public void setIssueEst(Long issueEst) {
		this.issueEst = issueEst;
	}
	public String getSbiName() {
		return sbiName;
	}
	public void setSbiName(String sbiName) {
		this.sbiName = sbiName;
	}
	public Long getSbiestimate() {
		return sbiestimate;
	}
	public void setSbiestimate(Long sbiestimate) {
		this.sbiestimate = sbiestimate;
	}
}

in vm file you can access data as follows

#foreach( $details in $issueDetails ) 
 $details.issName
 ...
#end
Jayashree Shetty
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 12, 2014

i am not a java developer i have started using java for development in JIRA. I am unaware of java beans. I had a doubt. You have commented out assigning pbi and sbi values to the map at the initial state. So is it that this Bean will have to hold pbi variable and sbi variable?We add all pbi(issue) and subtasks values to the map at the end?

RambanamP
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 12, 2014

i updated my answer so you can use as it is!

Answer for your Q, yes the values will be stored in bean class object.

Jayashree Shetty
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 12, 2014

Thanks Prasad. I understood the bean class. i am not clear about accessing the vm file. Does this wont get overwritten? How can i access the PBI and all its subtasks without having the any subtask getting overwritten.

I can loop through each element and get the value but how will my table get to know how many data is present and to increment the row of teh table and display the data

RambanamP
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 12, 2014

the table row count will depends on number of objects present in list, you no need to bother about row count!!

access data as follows in vm

<tbody>
#foreach( $details in $issueDetails ) 
	<tr>
		<td id="td1" headers="issuetype">
			 $details.issName
		</td>
		<td id="td1" headers="originalest">
			 $details.issueEst
		</td>
	</tr>
	<tr>
		<td id="td1" headers="issuetype">
			 $details.sbiName
		</td>
		<td id="td1" headers="originalest">
			 $details.sbiestimate
		</td>
	</tr>
#end
</tbody>

Jayashree Shetty
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 12, 2014

Thanks a lot Prasad. I modified the above code a little bit since PBI used to get repeated each time we fetch a SBI.

<table id="table1" class="aui"> 
<thead>
  <tr>
     <th id="issuetype">Issue Type</th>
     <th id="originalest">Original Estimate (in Hours)</th>
  </tr>
</thead>
<tbody>
   <tr>
      <td id="td1" headers="issuetype">$details.issName</td>
      <td id="td1" headers="originalest">$details.issueEst</td>
    </tr> 

    #foreach($details in $issueDetails)     

   <tr>
   <td id="td1" headers="issuetype">$details.sbiName</td>
   <td id="td1" headers="originalest">$details.sbiestimate</td>
    </tr>
    #end
<tbody>
</table>

But now the PBI name and estimate isnt getting displayed. It displays this way

$details.issName$details.issueEst

Also on addition of each sbi the list goes on increasing. do we have scrollbar for a web-pael?

RambanamP
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 12, 2014

this is because of you are accessing those varibles outside of loop, if those varible shoud't be repeated then need to put some condition!!

Jayashree Shetty
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 12, 2014

Can we add scrollbar to web-panel? Since if i have some 20 subtasks the screen seems to be increasing in height. Instead of having scroll bar to the entire issue screen page can we add the scroll bar to the web panel itself?

RambanamP
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 13, 2014

you can do it, just google it like 'vertical scroll bar for html table'

RambanamP
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 13, 2014

if my answers helped then don't forgot to mark it as answers :)

Jayashree Shetty
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 13, 2014

Did that :) it worked :)

Added <div style="height:200px; overflow-y: scroll;"> before the start of the table tag

0 votes
Jayashree Shetty
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 13, 2014
Updated Code:

public Map getContextMap(User user, JiraHelper jiraHelper)

{

Map map = new HashMap();
List&lt;IssueDetailsBean&gt; list=new ArrayList&lt;IssueDetailsBean&gt;();


Issue currentIssue =(Issue)jiraHelper.getContextParams().get("issue");
String iss_name=currentIssue.getIssueTypeObject().getName();
Long iss_est = currentIssue.getOriginalEstimate();


if(iss_name.equalsIgnoreCase("PBI"))
{
if(currentIssue.getSubTaskObjects()!=null)
{
IssueDetailsBean issDetailBean= new IssueDetailsBean(iss_name,iss_est/hours);
list.add(issDetailBean);

Collection&lt;Issue&gt; subtasks = currentIssue.getSubTaskObjects();
Iterator&lt;Issue&gt; enumerator1 = subtasks.iterator();

while(enumerator1.hasNext())
{

Issue item = enumerator1.next();
IssueDetailsBean issueSbi =new IssueDetailsBean(item.getSummary(),item.getOriginalEstimate()/hours);
list.add(issueSbi);

}

}

}
 
map.put("issueDetails",list);
return map;

}

Velocity File:

&lt;table id="tableid" class="aui"&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th id="issuetype"&gt;Issue Type&lt;/th&gt;
&lt;th id="originalest"&gt;Original Estimate&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;

#foreach( $details in $issueDetails ) 
    &lt;tr&gt;
        &lt;td id="td1" headers="issuetype"&gt; $details.issName&lt;/td&gt;          
        &lt;td id="td1" headers="originalest"&gt;$details.issueEst&lt;/td&gt;
    &lt;/tr&gt;
#end
&lt;/tbody&gt;
&lt;/table&gt;

0 votes
RambanamP
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 12, 2014

have you gone through the following document?

https://developer.atlassian.com/display/JIRADEV/Web+Panel+Plugin+Module

in web panel module you need add "context-provider" element and implement all your logic in that class and then you can access those data on vm.

Jayashree Shetty
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 12, 2014

Hi Prasad yes i have . I do have the data in the context provider. As explained above i have a hashmap created.

@Override

public Map getContextMap(User user, JiraHelper jiraHelper)

{

Map map = new HashMap();

Issue currentIssue =(Issue)jiraHelper.getContextParams().get("issue");

String iss_name=currentIssue.getIssueTypeObject().getName();

Long iss_est = currentIssue.getOriginalEstimate();

if(iss_name.equalsIgnoreCase("PBI"))

{

map.put("issuename", iss_name);

map.put("issestimate",iss_est/hours + "h");

if(currentIssue.getSubTaskObjects()!=null)

{

Collection<Issue> subtasks = currentIssue.getSubTaskObjects();

int size = subtasks.size();

Iterator<Issue> enumerator1 = subtasks.iterator();

while(enumerator1.hasNext())

{

Issue item = enumerator1.next();

String issueID=item.getIssueTypeId();

map.put("sbiname", item.getSummary());

map.put("sbiestimate" ,item.getOriginalEstimate()/hours + "h");

}

}

}

return map;

}

This is returning the issue PBI with hours and its subtasks. But wha is happening is that data from the subtask gets overwritten everytime with the new value. So i need to retain this and display all the subtask values. So i dont know how can i change the veloclity file.

TAGS
AUG Leaders

Atlassian Community Events