You will need to rename the file from zipx to zip as source forge is
blocking all zip files.


---------- Forwarded message ----------
Date: Thu, 16 Sep 2004 09:44:18 +0100 (IST)
From: Dave Mulligan <[EMAIL PROTECTED]>
To: [EMAIL PROTECTED]

Hey guys,

I've been playing around with DisplayTag for the last two weeks and
have to say that it is really brilliant tool. Thanks to all the
developers for their hard work creating such a great tool.

Now, down to my question/solution.

I've had the need to highlight a row based on the content of the row.
I came across an example on the net that uses javascript to iterate
over the rows of a table and alter the style. This is very useful, but
doesn't solve my problem. I taught if I was able to set the id
attribute of each TR row, I would then be able to use javascript to
highligh the required rows. At the moment there is no way to do this
using DisplayTag.

i.e.
<table>
<tr id="row1">
  <td>col1</td> <td>col2</td>
</tr>
<tr id="row2">
  <td>col1</td> <td>col2</td>
</tr>
</table>

<script type="text/javascript">
 setStyle('row2', 'error');
</script>

My second idea was to set the CSS style to the TR row directly by
using the TableDecorator. This also is not currently supported by
DisplayTag.

Below is my solution to the above problems.

I altered org.displaytag.decorator.TableDecorator to include the below
two new methods.

    public String startRow()
    {
     return "";
    }

    public String getRowCss()
    {
     return "";
    }

And also altered org.displaytag.model.Row.getOpenTag()

    public String getOpenTag()
    {
        StringBuffer buffer = new StringBuffer(TagConstants.TAG_OPEN)
                                       .append(TagConstants.TAGNAME_ROW);

        String css = this.tableModel.getProperties().getCssRow(this.rowNumber);
        String rowCss = null;

        if (this.tableModel.getTableDecorator() != null)
        {
            String rowAttributes = 
this.tableModel.getTableDecorator().getRowAttributes();
            if (StringUtils.isNotBlank(rowAttributes))
            {
                buffer.append(" ").append(rowAttributes);
            }
            rowCss = this.tableModel.getTableDecorator().getRowCss();
        }

        if (StringUtils.isNotBlank(css) || StringUtils.isNotBlank(rowCss))
        {
            buffer.append(" ").append(TagConstants.ATTRIBUTE_CLASS).append("=\"");
            if (StringUtils.isNotBlank(css))
            {
                buffer.append(css);
                if (StringUtils.isNotBlank(rowCss))
                {
                    buffer.append(" ");
                }
            }
            if (StringUtils.isNotBlank(rowCss))
            {
                buffer.append(rowCss);
            }
            buffer.append("\"");
        }

        return buffer.append(TagConstants.TAG_CLOSE).toString();
    }

Now, by simply sub-classing TableDecorator I can achive solutions to
the above two problems.

public class MyCustomTableDecorator extends TableDecorator {

    public String getRowAttributes() {
      Object obj = getCurrentRowObject();

      if (MyBean.class.isAssignableFrom(obj.getClass())) {
        MyBean myBean = (MyBean)obj;
        return TagConstants.ATTRIBUTE_ID + "=\"" + myBean.getId() +"\"";
      }
      return "";
    }

    public String getRowCss() {
      Object obj = getCurrentRowObject();

      if (MyBean.class.isAssignableFrom(obj.getClass())) {
        MyBean myBean = (MyBean)obj;
        if(myBean.isError()) {
          return "error";
        }

      }
      return "";
    }

The output will look something like this

<table>
<tr id="1001" class="odd">
  <td>col1</td> <td>col2</td>
</tr>
<tr id="1002" class="even, error">
  <td>col1</td> <td>col2</td>
</tr>
</table>

I believe an even better solution would be to create a (non abstract)
default TableDecorator and move getCssRow() from TableProperties into
this default TableDecorator. The default TableDecorator would always
be used if no decorator is provided by the user.

Please find attached a zip file containing the modified files. Let me
know if you like/dislike my idea! It would be great if something like
this could be including in version 1.0.

Regards,
Dave.

Attachment: displaytag.zipx
Description: Zip archive

Reply via email to