首页 > 解决方案 > Extjs checkbox and textarea - how to disable the checkbox on scroll behavior of the textarea?

问题描述

I have a text area that displays some text with a vertical scroll bar on the right and a checkbox below the text area as in the code below:

xtype: 'container',
items: [{
    {
        xtype: 'textarea',
        scrollY: true,
        screenX: true,
        height: 130,
        minHeight: 80,
        top: 10,
        width: 650,
        id: 'testDisplay',
        name: 'testDisplay',
        itemId: 'testDisplay',
        bind: {
            value: some text
        },
        listeners: {
            afterrender: function(cmp) {
                textAreaComp = me.down('#testDisplay');
                textAreaComp.setValue(someInfo);
            }
        },
    }, 
    {
        xtype: 'checkboxfield',
        name: 'testDisplayChkbox',
        id: 'testDisplayChkbox',
        itemId: 'testDisplayChkbox',
        checked: false,
        boxLabel: someLabel,
    }
}]

I am looking to disable the checkbox at first and then enable it after scrolling to the end of the text area located above the checkbox. How can this be achieved?

标签: javascriptcheckboxextjstextareasencha-touch-2

解决方案


There is no built-in listener in ExtJS for this. You have to lookup the textarea in DOM and attach an onscroll event. Then you can check whether scrolling reached the bottom and enable your checkbox. Try the following code and also look at this fiddle:

Ext.create({
    renderTo: Ext.getBody(),
    xtype: 'container',
    items: [{
        xtype: 'textareafield',
        value: 'Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?',
        afterRender: function () {
            // get checkbox
            const myCheckBox = this.up('container').getComponent('myCheckBox');
            // add onscroll listener to textarea
            this.el.down('textarea').dom.onscroll = function () {
                // check if scrolling reached bottom
                if (this.scrollHeight <= (this.scrollTop + this.offsetHeight)) {
                    myCheckBox.setDisabled(false);
                }
            };
        }
    }, {
        xtype: 'checkboxfield',
        itemId: 'myCheckBox',
        label: 'Lorem ipsum',
        checked: false,
        disabled: true
    }]
});

推荐阅读