5

I want to create dataitem in a list which looks like this: Required layout

but I am unable to render middle vbox section with 3 components. I am following this example : http://www.sencha.com/blog/dive-into-dataview-with-sencha-touch-2-beta-2/

This is how I am defining my data item:

Ext.define('MyTabApp.view.CartListItem', {
    extend  : 'Ext.dataview.component.DataItem',
    alias   : 'widget.cartlistitem',
    requires: [
               'Ext.Img'
    ],
    config  : {
        cls: 'cart-list-item',
        layout: {
            type: 'hbox',
            align: 'center'
        },
        image: true,
        details: {
            cls: 'x-details',
            flex: 3,
        },
        pricing: {
            cls: 'x-pricing',
            flex: 1
        },
        removeButton: {
            iconCls     : 'delete',
            iconMask    : true,
            ui          : 'astonvilla',
            style       : 'margin-left: 5px; margin-top:10px; padding:5px;'
        },
        moveButton: {
            iconCls     : 'reply',
            iconMask    : true,
            ui          : 'astonvilla',
            style       : 'margin-left: 5px; margin-top:10px; padding:5px;'
        },
        editButton: {
            iconCls     : 'compose',
            iconMask    : true,
            ui          : 'astonvilla',
            style       : 'margin-left: 5px; margin-top:10px; padding:5px;'
        },
        dataMap: {
            getImage: {
                setSrc          : 'itemImage'
            },

            getDetails: {
                setItemTitle    : 'title',
                setQuantity     : 'quantity'
            },

            getPricing: {
                setHtml         : 'totalPrice'
            },
        },
    },
    applyImage: function(config) {
        return Ext.factory(config, Ext.Img, this.getImage());
    },

    updateImage: function(newImage, oldImage) {
        if (newImage) {
            this.add(newImage);
        }

        if (oldImage) {
            this.remove(oldImage);
        }
    },

    applyDetails: function(config) {
        console.log("applyDetails");
        var details = Ext.factory(config, MyTabApp.view.CartListItemDetails, this.getDetails());
        console.log("applyDetails done");
        console.log(details);
        return details;
    },

    updateDetails: function(newDetails, oldDetails) {
        console.log("updateDetails");
        if (newDetails) {
            this.add(newDetails);
        }

        if (oldDetails) {
            this.remove(oldDetails);
        }
    },

    applyPricing: function(config) {
        return Ext.factory(config, Ext.Component, this.getPricing());
    },

    updatePricing: function(newPricing, oldPricing) {
        if (newPricing) {
            this.add(newPricing);
        }

        if (oldPricing) {
            this.remove(oldPricing);
        }
    },

    applyRemoveButton: function(config) {
        return Ext.factory(config, Ext.Button, this.getRemoveButton());
    },

    updateRemoveButton: function(newRemoveButton, oldRemoveButton) {
        if (oldRemoveButton) {
            this.remove(oldRemoveButton);
        }

        if (newRemoveButton) {
            // add an event listeners for the `tap` event onto the new button, and tell it to call the onNameButtonTap method
            // when it happens
            newRemoveButton.on('tap', this.onRemoveButtonTap, this);

            this.add(newRemoveButton);
        }
    },

    onRemoveButtonTap: function(button, e) {
        var record = this.getRecord();

        Ext.Msg.alert(
            record.get('title'), // the title of the alert
            "Id of this item is: " + record.get('itemId') // the message of the alert
        );
    },

    applyEditButton: function(config) {
        return Ext.factory(config, Ext.Button, this.getEditButton());
    },

    updateEditButton: function(newEditButton, oldEditButton) {
        if (oldEditButton) {
            this.remove(oldEditButton);
        }

        if (newEditButton) {
            // add an event listeners for the `tap` event onto the new button, and tell it to call the onNameButtonTap method
            // when it happens
            newEditButton.on('tap', this.onEditButtonTap, this);

            this.add(newEditButton);
        }
    },

    onEditButtonTap: function(button, e) {
        var record = this.getRecord();

        Ext.Msg.alert(
            record.get('title'), // the title of the alert
            "Id of this item is: " + record.get('itemId') // the message of the alert
        );
    },

    applyMoveButton: function(config) {
        return Ext.factory(config, Ext.Button, this.getMoveButton());
    },

    updateMoveButton: function(newMoveButton, oldMoveButton) {
        if (oldMoveButton) {
            this.remove(oldMoveButton);
        }

        if (newMoveButton) {
            // add an event listeners for the `tap` event onto the new button, and tell it to call the onNameButtonTap method
            // when it happens
            newMoveButton.on('tap', this.onMoveButtonTap, this);

            this.add(newMoveButton);
        }
    },

    onMoveButtonTap: function(button, e) {
        var record = this.getRecord();

        Ext.Msg.alert(
            record.get('title'), // the title of the alert
            "Id of this item is: " + record.get('itemId') // the message of the alert
        );
    }
});

And the middle section which I am calling details is defined as custom view with vbox layout is defined like this:

Ext.define('MyTabApp.view.CartListItemDetails', {
    extend  : 'Ext.Component',
    alias   : 'widget.cartlistitemdetails',
    config  : {
        cls     : 'x-details',
        flex    : 3,
        layout  : 'vbox',
        titleCell: null,
        qtyCell: null,
        items   : [{
            xtype   : 'panel',
            flex    : 1,
            itemId  : 'titleCell',
            html    : 'blahblah'
        },
        {
            xtype   : 'component',
            flex    : 1,
            itemId  : 'qtyCell',
            html    : 'blahblah'
        }],
    },
    setItemTitle    : function(title){
//      this.down('#titleCell').setHtml(title);
        console.log(this.getItems());
        this.getItems()[0].html = title;
    },
    setQuantity : function(qty){
//      this.down('#qtyCell').setHtml(qty);
        console.log(this.getItems());
        this.getItems()[0].html = qty;
    }
});

This is rendering list items with image, pricing & horizontally aligned buttons. Middle section which is custom component is not rendered. If I inspect element it is how html is generated:

<div class="x-details x-layout-box-item x-flexed x-stretched" id="ext-cartlistitemdetails-1" style="-webkit-box-flex: 3;"></div>

as you can see there is nothing inside this div and this is how it is rendered:

Actual list items

It seems I am close because setItemTitle method is getting called but still this.down('#titleCell').setHtml(title) or this.getItems()[0].html = title; is not working.

Titouan de Bailleul
  • 12,920
  • 11
  • 66
  • 121
ThinkFloyd
  • 4,981
  • 6
  • 36
  • 56
  • 1
    Not sure on all of it, but why are there only two items in `CartListItemDetails`? Also, why is the `titleCell` a `panel` and the `qtyCell` a `component`? But really, why not just make the `CartListItemDetails` a simple `panel` with html content that you style with CSS? – Jordan Kasper Mar 08 '13 at 16:11
  • I would also do it with an HTML template and so CSS magic. Then you'd have only an itemtap listener on the list. In the function called by the itemtap listener you can figure out the target of the tap event and therefore trigger whatever other function. – Titouan de Bailleul Mar 08 '13 at 20:39
  • @jakerella actually because of frustration I added/removed/changed lot of things because of which there are 2 items instead of 3 and also that `panel` & `container`. – ThinkFloyd Mar 09 '13 at 05:09
  • @TDeBailleul After all my tries I was planning to go to template route but still I'd love to know what I am doing wrong? There are cases like dynamically increasing horizontal lists where use of templates mess up sencha's calculation and as a result some portion is always hidden, so in such cases where I cannot use template I would love to learn dataitem approach – ThinkFloyd Mar 09 '13 at 05:13

1 Answers1

6

I was also trying to implement a complex dataitem layout.I'm using vbox panel inside hbox items and able to implement it.

Check it out https://github.com/tomalex0/senchatouch-complex-dataitem

Demo http://tomalex0.github.io/senchatouch-complex-dataitem/

Try it out and let me know if it works for you.

tomalex
  • 1,233
  • 6
  • 17
  • 40