Search code examples
grails

MarkupBuilder - how to add table td attribute "title"


I am looking to add table td "title" attribute along with td content. is there any way to do this


Solution

  • I used groovy markup builder in the context of grails tags libs, I share a small custom tag with the requirement that you mention. See how the title attribute is added to the td tag

    import groovy.xml.*
    
    def table = {
        MarkupBuilder mb = new MarkupBuilder(out)
    
        mb.table {
            thead {
                tr {
                    th 'Col1'
                    th 'Col2'
                    th 'Col3'
                }
            }
    
            tbody {
                tr {
                    td(title: 'lorem ipsum') {
                        mb.yield 'Text1'
                    }
    
                    td 'Text1'
    
                    td 'Text1'
                }
    
                tr {
                    td(title: 'lorem ipsum') {
                        mb.yield 'Text2'
                    }
    
                    td 'Text2'
    
                    td 'Text2'
                }
            }
        }
    }
    

    In the view you can invoke the new tag in this way, assuming you use the namespace location

    <location:table/>
    

    This will draw the following table in the browser

    enter image description here

    It's certainly not pretty but it's made with markup builder and it has some td with the title attribute.

    This link is an excellent resource to understand tags libs in grails and contains examples using markup builder

    For how to use markup builder in a context other than grails tag libs, visit this post of who else if not Mr Haki

    I hope it is useful for you