# Use 'colspan' in view\_attributes to remove a field

**URL:** <https://discuss.tryton.org/t/use-colspan-in-view-attributes-to-remove-a-field/3944>\
**Category:** Developer\
**Created:** [March 16, 2021, 10:19pm UTC](https://discuss.tryton.org/t/use-colspan-in-view-attributes-to-remove-a-field/3944 "2021-03-16T22:19:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![edbo](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/e/f14d63/32.png) [@edbo](https://discuss.tryton.org/u/edbo)\
**Post date:** [March 16, 2021, 10:19pm UTC](https://discuss.tryton.org/t/use-colspan-in-view-attributes-to-remove-a-field/3944/1 "2021-03-16T22:19:04Z")

</div>

I’m struggling with the use of the `view_attributes`. I want to remove a field from a form when a certain condition is met. Otherwise the field should be visible.

In my XML I have:

```auto
<?xml version="1.0"?>
<form col="4">
  ......
  <label name="order"/> # Many2One
  <field name="order"/>
  <label name="location"/> # Selection field
  <field name="location"/>
  ......

```

Now I want to remove the field `location` from the form when `order` is empty. I tried:

```auto
@classmethod
def view_attributes(cls):
    return super(Order, cls).view_attributes() + [
    ('/form/field[@name="location"]', 'colspan',
            If(Bool(Eval('order', -1)), -1, 2),
    )]

```

Basically I want to say “If order is empty, set the colspan of the location to -1. Else do nothing”. I have tried several versions, but none of them are working. On the client side I get a message like:

```auto
ValueError: invalid literal for int() with base 10: '{" __class__": "If", "c": {" __class__": "Bool", "v": {" __class__": "Eval", "v": "order", "d": -1}}, "t": -1, "e": 2}'

```

I don’t want to make the field just `invisible` because I want to remove the empty space as well.

How can I achieve this?

---

<div class="post-metadata">

**Author:** ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)\
**Post date:** [March 16, 2021, 10:43pm UTC](https://discuss.tryton.org/t/use-colspan-in-view-attributes-to-remove-a-field/3944/2 "2021-03-16T22:43:13Z")

</div>

> [@edbo](#):
>
> How can I achieve this?

You can not. `colspan` can not be dynamic, it is set only once when the view XML is parsed.  
If you want to hide a field based on a statement, you should use the `states['invisible']` of the field.

---

<div class="post-metadata">

**Author:** ![edbo](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/e/f14d63/32.png) [@edbo](https://discuss.tryton.org/u/edbo)\
**Post date:** [March 16, 2021, 11:01pm UTC](https://discuss.tryton.org/t/use-colspan-in-view-attributes-to-remove-a-field/3944/3 "2021-03-16T23:01:20Z")

</div>

But `states['invisible']` will only hide the field and does not give the space back right?  
Would it be possible to display another field in that location? or will it be left empty?

---

<div class="post-metadata">

**Author:** ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)\
**Post date:** [March 16, 2021, 11:07pm UTC](https://discuss.tryton.org/t/use-colspan-in-view-attributes-to-remove-a-field/3944/4 "2021-03-16T23:07:44Z")

</div>

> [@edbo](#):
>
> But `states['invisible']` will only hide the field and does not give the space back right?

The views are based on a grid system.

> [@edbo](#):
>
> Would it be possible to display another field in that location?

You can use a group to put multiple tag under the same cell.

But I think you should embrace the Tryton design and not try to simulate another.

---

<div class="post-metadata">

**Author:** ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)\
**Post date:** [March 18, 2021, 9:46am UTC](https://discuss.tryton.org/t/use-colspan-in-view-attributes-to-remove-a-field/3944/5 "2021-03-18T09:46:12Z")

</div>

For completeness there is this proposal [Support colspan -1 & 0 (#7492) · Issues · Tryton / Tryton · GitLab](https://bugs.tryton.org/issue7492) but this will not make them dynamic.

---

<div class="post-metadata">

**Author:** ![edbo](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/e/f14d63/32.png) [@edbo](https://discuss.tryton.org/u/edbo)\
**Post date:** [March 18, 2021, 10:14am UTC](https://discuss.tryton.org/t/use-colspan-in-view-attributes-to-remove-a-field/3944/6 "2021-03-18T10:14:40Z")

</div>

Thanks, I read that one and came up with the idea to use the colspan in the `view_attributes`.

For even more completeness, I had to hide two fields which makes it one row in the grid. I did put that row just above the tabs. Using it this way and hiding the fields, removes the empty space. So no need to use `view_attributes`, this can be defined on the field itself.

```auto
<?xml version="1.0"?>
<form col="4">
  ......
  <label name="order"/> # Many2One
  <field name="order"/>
  <newline/>
  <label name="location"/> # Selection field
  <field name="location"/>
  <label name="user"/> # Selection field
  <field name="user"/>
  <notebook colspan="4">
    <page string="General" id="general">
      ........ # page stuff
    </page>
    ........ # do more paged stuff
  </notebook>
  ......

```

So when setting the `state` to `invisible` for `location` and `user` the notebook will consume that empty space.

---

<div class="post-metadata">

**Author:** ![system](https://discuss-cdn.tryton.org/uploads/default/original/1X/c6f8ec0a40525cdcd50058c734283450a4b3d38b.png) [@system](https://discuss.tryton.org/u/system)\
**Post date:** [April 17, 2021, 10:14am UTC](https://discuss.tryton.org/t/use-colspan-in-view-attributes-to-remove-a-field/3944/7 "2021-04-17T10:14:45Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
