Menu

Dynamic links based on object type

Help
Whiskogs
2007-12-11
2012-10-09
  • Whiskogs

    Whiskogs - 2007-12-11

    Hi,

    Bit of a newbie to this library, so sorry if this has been covered before.

    I'm using a <display:table> in which the first <display:column> is giving me a headache, somehow: the row object instance contains a sub-object that contains another sub-object, which can be of type A or B (distiller or blender, to make things clear). I want to link to that object's page, which, obvously has a different url structure depending on its nature (/distiller or /blender, innit); the only parameter is the id of said object. Also, I want the column to be sortable.
    Now, I've had to rely on something like:

    <display:column sortable="true">
    <% if (distiller) { %>
    <a href="/distiller?id=..."><%=...%></a>
    <% } else if (blender) {
    <a href="/blender?id=..."><%=...%></a>
    <% } %>
    </display:column>

    Of course, it messes up with the sorting, since it doesn't sort the content, but the urls themselves... Result to be seen on http://www.whiskogs.com/user/collection?id=1
    I've also tried:

    <display:column property="manufacturerName" href="manufacturerLink" paramId="id" paramProperty="manufacturerId" sortable="true" />

    where manufacturerXxx are decorators, with no luck: the name is displayed perfectly and sortable alright, but the href is always the same (I believe the latest to be translated) and the paramProperty attribute doesn't seem to work with decorators at all (gives, say, /blender?id:). I've tried various mixes of the above, including with the paramName attribute, but never succeeded in displaying all the names correctly, sortable and the links pointing to where they should. I'm a bit lost and would greatly appreciate help.

    Thanks in advance.

     
    • Ed Webb

      Ed Webb - 2007-12-11

      Have you tried using a ColumnDecorator?

      Add the decorator attribute to the <display:column> :

      <display:column property="manufacturerName" decorator="my.decorator.ManufacturerName" sortable="true" />

      and create a class like below :

      public class ManufacturerName implements ColumnDecorator {

      public String decorate(Object columnValue) {
      Manufacturer mnf = (Manufacturer)columnValue;
      if (mnf.getType().equals(DISTILLER) {
      return "<a href=\"/distiller?id=" + mnf.getId() + "\">" + mnf.getName() + "</a>";
      } else {
      return "<a href=\"/blender?id=" + mnf.getId() + "\">" + mnf.getName() + "</a>";
      }
      }
      }

      If I've understood the workings of the decorators correctly then the sort will be on the object's manufacturerName property but the display will be the hyperlinked name.

      Ed!

       
      • Whiskogs

        Whiskogs - 2007-12-11

        Hi Ed,

        Thanks for your attempt, but unfortunately, it will not do the trick (I tried, btw): a ColumnDecorator only receives the property attribute as argument, in my case, the name, which is insufficient to build the url. It also has no access to the pageContext, so I don't think there is a way I could figure out how to build the proper link from a ColumnDecorator.

        In any case, my above snippets and explanation were incomplete and unclear. Here is what I tried with decorators:

        <display:table id="user-collection" name="sessionScope.bean.collection" defaultsort="1" decorator="somepackage.ManufacturerDecorator">

        <display:column title="Name" property="manufacturerName" href="manufacturerLink" paramId="id" paramProperty="manufacturerId" sortable="true" />

        and the decorator:

        public class ManufacturerDecorator extends TableDecorator {
        public String getManufacturerName() {
        ... (works well)
        }
        public String getManufacturerLink() {
        ... (doesn't work -- isn't called, the generated link is /manufacturerLink?id=)
        }
        public String getManufacturerId() {
        ... (doesn't work -- isn't called and I get a "error looking up property manufacturerId for object")
        }
        }

        As I said, it seems the table decorator is only called for the property attribute, whereas I'd like it called for href and paramId as well.
        Hope someone can shed light on this.

         
        • Ed Webb

          Ed Webb - 2007-12-12

          If you need access to the pageContext and the row's object then you have to use a TableDecorator.

          Your displaytag:column becomes:

          <display:column title="Name" property="manufacturerNameLink" sortProperty="manufacturerName" sortable="true" />

          You then create a method in your TableDecorator called:

          public String getManufacturerNameLink() {
          MyObject obj = (MyObject)this.getCurrentRowObject();
          PageContext ctx = this.getPageContext();
          StringBuffer sb = new StringBuffer();

          ... create the hyperlinked manufacturer's name using the StringBuffer ...

          return sb.toString();
          }

          This ensures that the column is sorted by the undecorated manufacturerName property but is displayed using the method in your decorator.

          Ed!

          http://displaytag.sourceforge.net/11/tut_decorators.html

           
          • Whiskogs

            Whiskogs - 2007-12-12

            Oh dear! It was easier than that: I just needed that sortProperty attribute along with the table decorator! :-S
            In any case, thanks a lot for your help, Ed!

             
    • Whiskogs

      Whiskogs - 2007-12-11

      Hm, seems like DisplaytagColumnDecorator might be better suited, but dunno where to start with it...

       

Log in to post a comment.