0

I want to create a text box containing HTML, to list all attributes belonging to the current atlas feature.

I think I can do this by hand, writing html which references each and every field. For a simple attribute table, with 3 or 4 fields, that will be quite easy. But for a bigger attribute table, with perhaps 20 fields, that process will be laborious and prone to error.

Is there any easy way to do this - or a workaround with the same result?

Another equivalent way to describe the problem is to say that I'd like to show the current atlas feature's attribute form on the atlas layout page. I don't think this can be done automatically.

I'm aware that I can display the attribute table, filtered to show only attributes for the current atlas feature. This (as above) works fine where there are limited attribute fields - but a table with 20 fields and longer strings (or similar) as values won't fit (running off the edge of the page).

1 Answer 1

0

Using the expression builder, you can quickly create an expression that puts all the field names and field values into a text box. We'll be combining elements of the following expressions:

  • This expression gives you the attribute value of the current atlas feature: attribute( @atlas_feature, 'fieldname' )
  • This expression combines it with a text string, so it displays as "[field name]": [field value]: concat( '"fieldname"' ,': ', attribute( @atlas_feature, "fieldname" ))
    • If you want your text formatted another way, alter this expression to suit your needs. Use your modified expression in step 2 (below).
  • To combine all the field names and values so they look like this:

    "[field1 name]": [field1 value]

    "[field2 name]": [field2 value]

    "[field3 name]": [field3 value]

    use this expression:

    concat( '"field1name"' ,': ', attribute( @atlas_feature,  "field1name" ), '\n', 
      '"field2name"' ,': ', attribute( @atlas_feature,  "field2name" ), '\n' ,
      '"field3name"' ,': ', attribute( @atlas_feature,  "field3name" ))
    

To quickly create the above expression for any combination of fields, you can strategically copy and paste sections of it.

  1. First, copy and paste concat( into the expression builder.

    enter image description here

  2. Next, copy and paste one line of the expression without field names, like this :

    '__', ': ', attribute( @atlas_feature, __ ), '\n',
    

    Hit enter to start a new line (this is optional, but it makes the expression easier for you to read and edit).

  3. Repeat step 2 until your expression has as many lines as you have attributes.

    enter image description here

  4. Double-click on the first underscore __ in the first line to highlight it. Open the "Fields and Values" section of the Expression Builder help. Double-click the first field name to replace the underscore with the field name. Repeat for the second underscore in the first line.

    enter image description here

  5. Repeat step 4 until all your field names are added to the expression.

  6. Remove the final linebreak and comma: , '\n'. Close the concat function with a right parenthesis: ).

    enter image description here

  7. Check the "output preview" at the bottom of the expression builder. If the preview looks good, go ahead and click OK.

    enter image description here

Note: Make sure the "render as HTML" box isn't checked. If the label is rendered as HTML, the line breaks won't display properly. Instead they show as spaces, and the text is all on one line, like this:

enter image description here

2
  • Thanks. Not automatic, but not a bad workaround so I've marked this as answered. I like that you've created the full string with the underscores first - replacing these with the field names. That will keep things tidier. The details I'm familiar with, but with lots of fields its too easy to lose track. I'm also wondering whether it might be practical to write a short python script to write this expression for us (either inside or outside QGIS)... I'd be surprised if nobody else has done this already... I can't be the only one working with large numbers of fields. Commented May 1, 2019 at 19:36
  • What I've done is created a workaround to avoid learning to code in Python. I'm sure it's possible to automate this method. Just add in a loop and you've got your pseudocode written.
    – csk
    Commented May 1, 2019 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.