• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JavaScript design question

 
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm torn between taking the time to refactor this or just get it done and into production (I'm sure nobody else ever has that problem), but this code is turning really ugly and I'm not sure how to fix it.

My JSP generates a table that looks like this:



Each of those white cells must have the value compared to the goal and then the cell is colored green if the actual is equal to or less than the goal, or red if it's over goal.

I'm currently doing it with this function:

I know, I can make "missedgoal" the default and just turn the green ones, but this helps me debug. I'll do that when it's almost finished.

The function is called like so:

By now you can see the problem; this function has to be called for every cell. The repetition is killing me, but I can't think of a good way to do this with a loop.

How do I collect those elements and pass them into a loop? I considered making an object with those three properties but I'm not sure how to populate an array of objects that can be looped over.

The JSP looks like so. I'm using the hidden fields to store the unformatted values.

Any suggestions on how to tame this beast?
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could do the comparison server side and send the class name with the data. For example:
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

J. Kevin Robbins wrote:Any suggestions on how to tame this beast?


The first question that comes into my mind: is there a specific reason why you don't add the appropriate css class server side when generating the table?

If it really needs to be JavaScript, you can create a setTableColors() function which iterates over each tr element using something like
 
J. Kevin Robbins
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doh!

I suspected this might be a face palm. I think I got stuck looking for a JS solution and didn't back up enough to consider doing it on the server. I'll just add some additional bean properties and I'm good to go on the server side.

Thanks!
 
It was the best of times. It was the worst of times. It was a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic