~jri-openerp/openerp-web-training/openerp-web-training

« back to all changes in this revision

Viewing changes to web_framework.rst

  • Committer: niv-openerp
  • Date: 2013-06-27 10:06:49 UTC
  • Revision ID: nicolas.vanhoren@openerp.com-20130627100649-fsd025nkbszcwbyp
added qweb exercise

Show diffs side-by-side

added added

removed removed

Lines of Context:
697
697
 
698
698
Exercise
699
699
''''''''
 
700
 
 
701
.. topic:: Exercise - Usage of QWeb in Widgets
 
702
 
 
703
    Create a widget whose constructor contains two parameters aside from ``parent``: ``product_names`` and ``color``.
 
704
    ``product_names`` is a list of strings, each one being a name of product. ``color`` is a string containing a color
 
705
    in CSS color format (ie: ``#000000`` for black). That widget should display the given product names one under the
 
706
    other, each one in a separate box with a background color with the value of ``color`` and a border. You must use
 
707
    QWeb to render the HTML. This exercise will necessitate some CSS that you should put in
 
708
    ``oepetstore/static/src/css/petstore.css``. Display that widget in the ``HomePage`` widget with a list of five
 
709
    products and green as the background color for boxes.
 
710
 
 
711
.. only:: not nosolutions
 
712
 
 
713
    **Solution:**
 
714
 
 
715
    ::
 
716
 
 
717
        openerp.oepetstore = function(instance) {
 
718
            var _t = instance.web._t,
 
719
                _lt = instance.web._lt;
 
720
            var QWeb = instance.web.qweb;
 
721
 
 
722
            instance.oepetstore = {};
 
723
 
 
724
            instance.oepetstore.HomePage = instance.web.Widget.extend({
 
725
                start: function() {
 
726
                    var products = new instance.oepetstore.ProductsWidget(this, ["cpu", "mouse", "keyboard", "graphic card", "screen"], "#00FF00");
 
727
                    products.appendTo(this.$el);
 
728
                },
 
729
            });
 
730
 
 
731
            instance.oepetstore.ProductsWidget = instance.web.Widget.extend({
 
732
                template: "ProductsWidget",
 
733
                init: function(parent, products, color) {
 
734
                    this._super(parent);
 
735
                    this.products = products;
 
736
                    this.color = color;
 
737
                },
 
738
            });
 
739
            
 
740
            instance.web.client_actions.add('petstore.homepage', 'instance.oepetstore.HomePage');
 
741
        }
 
742
 
 
743
    ::
 
744
 
 
745
        <?xml version="1.0" encoding="UTF-8"?>
 
746
 
 
747
        <templates xml:space="preserve">
 
748
            <t t-name="ProductsWidget">
 
749
                <div>
 
750
                    <t t-foreach="widget.products" t-as="product">
 
751
                        <span class="oe_products_item" t-att-style="'background-color: ' + widget.color + ';'"><t t-esc="product"/></span><br/>
 
752
                    </t>
 
753
                </div>
 
754
            </t>
 
755
        </templates>
 
756
 
 
757
    ::
 
758
 
 
759
        .oe_products_item {
 
760
            display: inline-block;
 
761
            padding: 3px;
 
762
            margin: 5px;
 
763
            border: 1px solid black;
 
764
            border-radius: 3px;
 
765
        }
 
766
 
 
767
    .. image:: img/qweb.png
 
768
       :align: center
 
769
       :width: 70%
 
 
b'\\ No newline at end of file'