简体   繁体   中英

Break, Continue in ExtJs

I am using nested loop in ExtJs application. I want to use break, continue in below code. I know we can use return false to break from loop but when doesn't work properly.

Suppose first (outer) loop will run from 1 to 10 and second(inner) loop will run from 1 to 5. Now if condition is true (1==1) then it should be added item (checkbox) in panel only one time. . After adding item it should be break and outer loop will start continue.

Note - Everytime there is different panel will be created (According to my complete application code), so if condition will be true then every panel has only one checkbox.

Let me know, if anything is not clear.

My code is not working in fiddler. I don't know why. According to below code and my understanding, two checkbox should be created on button click.

Process should be run -

  1. 1==1 will be true or if condition is satisfied after that inner loop will be stopped.
  2. 2==2 will be true and inner loop will be stopped.

      Ext.onReady(function () { var window = new Ext.Window({ id: 'grdWindow', width: 400, title: 'Grid Samples', items: [ { xtype: 'button', text: 'testing', handler: function () { var obj1 = [ { a: 1}, { a: 2}, { a: 3}, { a: 4} ]; var obj2 = [ { a: 1 }, { a: 2 } ]; var panel = Ext.getCmp('pnlTesting'); Ext.each(obj1, function (ob1) { Ext.each(obj2, function (ob2) { if (ob1.a == ob2.a) { panel.add({ items:[ { xtype: 'checkbox', } ] }); } }); return false; }); } }, { xtype: 'panel', id: 'pnlTesting', renderTo: Ext.getBody() } ] }).show(); }); 

You are breaking the outer loop right after handling the first item of obj1 . Item {a: 2} never gets checked against any obj2 item.

Instead, you should be breaking the inner loop, and only when checkbox is added:

Ext.each(obj1, function (ob1) {
    Ext.each(obj2, function (ob2) {
        if (ob1.a == ob2.a) {
            container.add({
                items:[
                    {
                        xtype: 'checkbox',
                    }
                ]
            });
            return false;
        }
    });
});

See fiddle: https://fiddle.sencha.com/#fiddle/sgb

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM