"media" attribute in DisplayTag

Display tag is the open-source project used to display tables on the page which has many more functionalities than just displaying the table like it provides filters, pagination, export of the table, etc.

It provides many attributes to control the behavior of the table.
One such attribute is "media". This attribute is useful when the "export" is enabled by setting it to "true" on the Displaytag which allows the table to get exported in CSV, Excel, etc. These options are available in the footer of the table.

Suppose there is a need where we need to display one column on the page but after exporting the table that column should not be displayed in the excel file.

There is an attribute provided by the displaytag table.
media attribute is used to control what should be displayed on the JSP page and what should be exported in the file.

When we set the media attribute to 'html' on the <display:column> tag then it will display that column only on the HTML page and not in the exported file.
The exported file will not have this column.

But with the media attribute set to your export type  'excel' so that it only shows in the exported file and not on the JSP page.

Below are some combinations of media values that can be used

For example, I have a column "Address" and it contains the hyperlink of some site I want it to be displayed on the screen and when the user clicks on the link it gets redirected but I don't want it to get exported to the file

Then We can use the below snippet display:column entry just for viewing on the JSP page.

<display:column title="Address" property="link" media="html" />

Now suppose  I want 'Telephone Number' to be exported but should not display on JSP page,

The below snippet display:column entry for exporting in excel.

<display:column title="Mobile" property="mobile_no" media="excel" />

We can also use both values for the same column.

For example, I want to have a column that has hyperlink it and I want to display it on the screen but do not want it to be exported in the excel file then I will add it to display columns  like below

<display:column title="Address" sortable="true" property="address" href="address.jsp?add=1" media="html"/>

<display:column title="Address" property="address" media="excel" />

It won't be added to the table if the current request media is not supported.

Possible values of media attributes are 'html','excel', 'csv','xml','all'.Default attribute is set to 'all'. These values can be space separated.

Suppose we want to display a column on the JSP page as well as in excel, we can use the below snippet

<display:column title="Address" property="address" media="html, excel" />


The below snippet will export the mapped column in CSV, Excel, XML and pdf but will not be displayed on the JSP page

<display:column property="longDescription" media="csv excel xml pdf" title="Not On HTML"/>


media attribute can also be used on the <display:footer>tag as shown below. It will only display it on the JSP page

<display:footer media="html">
        <tr>
          <td colspan="4">Total count:</td>
          <td colspan="1">${fn:length(test)}</td>
        </tr>
</display:footer>

Comments

  1. You can set the media attribute to 'html' on the display:column tag to only show that column on the JSP page and not in the exported file. But with the media attribute set to your export type 'excel' so that it only shows in the exported file and not on the JSP page

    ReplyDelete

Post a Comment

Popular Posts