1

How can i add disabled class with attributes to field based on CONDITION?

I tried as below methods,

1)

<t t-if="product.qty_available &lt;= 0">
    <attribute name="class">btn btn-primary btn-lg mt8 js_check_product a-submit disabled</attribute>
</t>

2)

<attribute name="class" t-if="product.qty_available &gt; 0">btn btn-primary btn-lg mt8 js_check_product a-submit disabled</attribute>

3)

<attribute name="class" >product.qty_available &gt; 0 and 'btn btn-primary btn-lg mt8 js_check_product a-submit' or 'btn btn-primary btn-lg mt8 js_check_product a-submit disabled'</attribute>

Can some one helping me to achieve this?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
02731a0558
  • 29
  • 1
  • 8

2 Answers2

3

try this one:

<attribute name="t-att-disabled" > "1 if product.qty_available else 0" </attribute>
Prakash Kumar
  • 2,554
  • 2
  • 18
  • 28
  • Thanks for the reply. – 02731a0558 May 27 '16 at 08:26
  • When i try with first one, irrespective of condition button got disabled. because in HTML page for any filed if it finds the disabled attribute, immediately it will disable, so, disabled=true or disable=false, not a matter it will disable. – 02731a0558 May 27 '16 at 08:29
  • when i try with second one, button not disabled at all both for qty >0 and qty <=0 – 02731a0558 May 27 '16 at 08:31
  • I forgot to mention that , i am trying to implement this within a Template – 02731a0558 May 27 '16 at 08:35
  • use the else '0' or else 'False' in the first one, – Prakash Kumar May 27 '16 at 09:24
  • no luck, same result. Button got disabled in both the conditions qty >0 and qty <=0 – 02731a0558 May 27 '16 at 09:30
  • after page gets loaded also, in html page getting field attribute like, disabled=" '1' if product.qty_available > 0 else '0' " its not getting replaced with '1' or '0' – 02731a0558 May 27 '16 at 09:33
  • i have update my answer "1 if product.qty_available else 0" – Prakash Kumar May 27 '16 at 09:40
  • Same like before. irrespective of condition button got disabled. HTML doesn't use boolean values for boolean attributes, surprisingly, some one explained here: http://stackoverflow.com/questions/32745276/explicitly-set-disabled-false-in-the-html-does-not-work – 02731a0558 May 27 '16 at 10:44
  • Thanks for spending time to answer the questions in Stackoverflow. For my previous questions instead of answers i got -ve reputation. Final attempt to me to try with position="replace", i will try with it and if succeeds i will post my answer. – 02731a0558 May 27 '16 at 10:46
0

Did with position="replace"

<xpath expr="//a[@id='add_to_cart']" position="replace">
      <a id="add_to_cart" t-attf-class="btn btn-primary btn-lg mt8 js_check_product a-submit #{product.qty_available &lt;= 0 and 'disabled'}" href="#" >Add to Cart</a>
  </xpath> 
02731a0558
  • 29
  • 1
  • 8