Table Border Macro -- Variable Border Width

Hello!

I'm fairly new to Confluence and Velocity.
I was able to create a macro that removes table borders on a page.
Now I am attempting to create a macro that allows the user to set the table border between 0 and 2 pixels, unique for each table on the page! (The user will use a separate macro for each table).
At the moment, using a div and class, the last chunk CSS code affects every table on the page, as expected. So I would like to use an id instead, and generate the id to be unique using a time stamp.
However, the code is now ineffective.
Any thoughts?

CODE

## This macro takes a table and lets the user choose a border width limited to 0 (none), 1, or 2
## @param width:title=Width|type=enum|enumValues=0,1,2|desc=Set border width, limited to 0,1, or 2
##
#if (!$paramwidth)
#set ($border = "0px")
#else
#set($border = "${paramwidth}px")
#end
##
<style>
.tableWrapper th {
    border: $border;
    border-style: solid;
}
.tableWrapper td {
    border: $border;
    border-style: solid;
}
</style>
##
<div class=tableWrapper>
$body
</div>

PRODUCES

CODE

## This macro takes a table and lets the user choose a border width limited to 0 (none), 1, or 2
## @param width:title=Width|type=enum|enumValues=0,1,2|desc=Set border width, limited to 0,1, or 2
##
#if (!$paramwidth)
#set ($border = "0px")
#else
#set($border = "${paramwidth}px")
#end
#set($id = $content.getCurrentDate().getTime())
##
<style>
.$id th {
    border: $border;
    border-style: solid;
}
.$id td {
    border: $border;
    border-style: solid;
}
</style>
##
<div id=$id>
$body
</div>

PRODUCES

1 answer

1 accepted

3 votes
David Simpson Community Champion Jun 27, 2014

That's nearly there, but id attributes cannot be numbers, well perhaps now with HTML5. Instead try something like:

#set($id = "random-" + $action.dateFormatter.calendar.timeInMillis) 

Hat tip: @Joseph Clark-- https://answers.atlassian.com/questions/218260/random-number-with-velocity

If that doesn't work, add a data-border-width attribute to the wrapping div and then use JavaScript to apply that to the enclosed table cells -- classy, I know.

Thank you so so much!! This worked beautifully!

I had to remove the dash, and I didn't include the <span>, but it's perfect!

For anyone interested, here is the full solution:

## This macro takes a table and lets the user choose a border width limited to 0 (none), 1, or 2
## @param width:title=Width|type=enum|enumValues=0,1,2|desc=Set border width, limited to 0,1, or 2
##
#if (!$paramwidth)
#set ($border = "0px")
#else
#set($border = "${paramwidth}px")
#end
#set($id = "random" + $action.dateFormatter.calendar.timeInMillis)
##
&lt;style&gt;
.$id th {
    border: $border;
    border-style: solid;
}
.$id td {
    border: $border;
    border-style: solid;
}
&lt;/style&gt;
##
&lt;div class=$id&gt;
$body
&lt;/div&gt;

David Simpson Community Champion Jul 03, 2014

It looks like the editor added the nasty <span> -- I didn't write that. Glad you got the gist of it ;)

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Posted Jul 10, 2018 in Confluence

We want to see the templates you've created in Confluence!

Hi Community, Jessica here from the Confluence Product Marketing team!  July’s community challenge is all about sharing pictures  — and as an extension of our first post on what ...

742 views 21 12
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you