1

The key/value widget applied on a Postgres hstore field conveniently shows, for a record, a sub-table containing the various keys and values. If there isn't enough space in the form, this sub-table is resized and some key/values are not visible.

How can one scroll this sub-table? I am currently using QGIS 3.4.15 LTR.

Hstore field using the key/value widget: enter image description here

Sub-table shows an elevator but is not scrollable: enter image description here

Long enough form allows showing more fields in the sub-table enter image description here

edit

Let's note that the scrolling works if the layer is being edited and is editable (i.e. it does not work with a read-only user)

enter image description here

  • Even in edit mode ? – J. Monticolo Feb 14 at 20:23
  • @J.Monticolo It works if the layer is edited + editable, but it does not if the user is a read-only user (please see edit) – JGH Feb 14 at 20:47
  • it's logic for me because in read only, the widget is not enabled and the scrolling is not possible. A workaround may be to place the tags fields in another tab ? – J. Monticolo Feb 14 at 21:09
  • what do you mean by "another tab"? – JGH Feb 14 at 21:15
  • Don't auto-generate your form, choose the drag & drop Designer mode and organize your fields (click on the green + for add a tab). See the doc – J. Monticolo Feb 14 at 21:18
2

A workaround may be to create, in a Drag & Drop Designer a new tab with the Key/Value widget, to access in edit mode if needed.

For read-only mode, add in the main tab, a HtmlWidget with the following code :

<script>
document.write(expression.evaluate(
"'<ul>' || " +
  "with_variable(" +
  "'hstore'," +
  "hstore_to_map(\"tags\")," +
  "array_to_string(" +
    "array_foreach(" +
      "map_akeys(@hstore)," +
      "'<li>' || @element || ': ' || map_get(@hstore, @element) || '</li>'" +
    "), '', ''" +
  ")" +
")" +
"|| '</ul>'"
));
</script>

This will display a Html frame with the tags values as a list like :

  • alt_name: Mount King
  • wikidata: Q59320
  • wikipedia: en:King Peak (Yukon)

EDIT

If you want a pretty table styled with mouse hovering, you can add CSS style :

<style>
table {
  font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

td, th {
  border: 1px solid #ddd;
  padding: 8px;
}

tr:nth-child(even){background-color: #f2f2f2;}

tr:hover {background-color: #ddd;}

th {
  padding-top: 12px;
  padding-bottom: 12px;
  text-align: left;
  background-color: #4CAF50;
  color: white;
}
</style>
<script>
document.write(expression.evaluate(
"'<table>' || " +
"'<tr><th>Tags</th>' || " +
  "with_variable(" +
  "'hstore'," +
  "hstore_to_map(\"tags\")," +
  "array_to_string(" +
    "array_foreach(" +
      "map_akeys(@hstore)," +
      "'<tr><td>' || @element || ': ' || map_get(@hstore, @element) || '</tr></td>'" +
    "), '', ''" +
  ")" +
")" +
"|| '</table>'"
));
</script>
  • Awesome!! For the benefit of others, let's note that HtmlWidget is not in 3.4 but in 3.10. Also I had to replace "hstore_to_map('\"tags"')," + with "\"tags\"," + as the field is already recognized as a map (and re-mapping it produces a Null output) – JGH Feb 15 at 0:19
  • The output size adapts very well to the form, and is scrollable if there isn't enough space, perfect! Thanks again! – JGH Feb 15 at 0:24
  • +1 pretty good catch! – ThingumaBob Feb 15 at 5:51
  • @JGH: (QGIS 3.8). I've added a table style example and I let hstore_to_map for fully answering the question title (who can do more, can do less). – J. Monticolo Feb 15 at 8:32
  • Thanks again! This new feature is really great! ... it will be a serious "competitor" to the virtual fields – JGH Feb 15 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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