extjs 从多个值插入多个url到输入字段

bwleehnv  于 2022-11-04  发布在  其他
关注(0)|答案(1)|浏览(210)

我有一个fetch get请求,它解析页面并获取一些参数来生成新的url并将其插入输入字段
我有来自value1value2value3的多个值
问题是我如何从这个参数中生成多个url并将其插入字段(字段应该动态生成)?
现在,带有窗体的窗口不会生成
如果我将break放入循环,代码将生成带有窗体窗口,但只显示一个url
由于这段代码应该是窗口与形式和三个输入字段与url

var url = 'google.com/page1'
fetch(url, {
        method: 'GET',
        credentials: 'include',
    })
    .then(function(response) {
        return response.text()
    })
    .then(function(html) {
            var parser = new DOMParser();
            var doc = parser.parseFromString(html, "text/html");
            var value4 = 'test4';
            var UrlArr = [];
            if (condition) {
                var name = doc.querySelectorAll("someClass");
                for (var i = 0; i < name.length; i++) {
                    var allNames = name[i].outerHTML;
                    var value1 = allNames.match('MyregExp1')[0];
                    var value2 = allNames.match('MyregExp2')[0];
                    var value3 = allNames.match('MYregExp3')[0];

                    var newUrl = `google.com/?${value1}&${value2}&${value3}&test=${value4}`;
                    //brake

                    console.log(value1);
                    // something1
                    // something2
                    // something3
                    // etc...

                    console.log(value2);
                    // something11
                    // something22
                    // something33
                    // etc...

                    console.log(value3);
                    // something111
                    // something222
                    // something333
                    // etc...

                    UrlArr.push(newUrl);
                    console.log(UrlArr);
                    //first-array
                    // [google.com/?something1&something2&$something3&test=test4]
                    // 0: google.com/?something1&something2&$something3&test=test4
                    // 1: google.com/?something11&something22&$something33&test=test4
                    // 2: google.com/?something111&something222&$something333&test=test4

                    // second-array
                    // [google.com/?something1&something2&$something3&test=test4,google.com/?something11&something22&$something33&test=test4 ]
                    // 0: google.com/?something1&something2&$something3&test=test4
                    // 1: google.com/?something11&something22&$something33&test=test4
                    // 2: google.com/?something111&something222&$something333&test=test4

                    // third-array
                    // [google.com/?something1&something2&$something3&test=test4,google.com/?something11&something22&$something33&test=test4,google.com/?something111&something222&$something333&test=test4]
                    // 0: google.com/?something1&something2&$something3&test=test4
                    // 1: google.com/?something11&something22&$something33&test=test4
                    // 2: google.com/?something111&something222&$something333&test=test4

                }
                if (allNames.length >= 2) {

                    var newForm = Ext.create('Ext.form.Panel', {
                        title: 'Form',
                        width: 250,
                        autoHeight: true,
                        autoScroll: true,
                        bodyPadding: 10,
                        id: 'Form',
                    });

                    function newRadio() {
                        var radio = new Ext.form.Radio({
                            height: 40,
                            labelWidth: 110,
                            width: 20,
                            name: 'url',
                        });
                        return radio;
                    }

                    function newTextArea(id) {
                        var textArea = new Ext.form.TextArea({
                            fieldLabel: i + '_url',
                            height: 30,
                            labelWidth: 110,
                            width: 330,
                            submitValue: false,
                            readOnly: true,
                            autoScroll: true,
                            id: id
                        });
                        return textArea;
                    }

                    for (i = 0; i < name.length; i++) {
                        var id = i;
                        id = 'field_' + id;
                        var field = newTextArea(id);
                        var radioGr = newRadio();
                        var cont = NewContainer();
                        field.setValue(UrlArr);
                        newForm.add(cont);
                    }

                    function NewContainer() {
                        var container = new Ext.container.Container({
                            xtype: 'container',
                            layout: 'hbox',
                            items: [radioGr, field]
                        });
                        return container;
                    }

                    var win = new Ext.Window({
                        title: "Window",
                        layout: 'fit',
                        modal: 'true',
                        height: 500,
                        width: 385,
                        items: [newForm],
                        buttons: [{
                            text: "Confirm",
                            handler: function() {
                                //something handler
                            }
                        }]
                    });
                    win.show();

                }
            }
kknvjkwl

kknvjkwl1#

您提供的代码中存在一些问题,例如:

  1. ContainerradiotextArea之前创建。
    1.您的要求是在字段中设置newUrl,但是已经有一个for循环在更新该变量,并且它在for循环的作用域中。因此,您可以在第一个for循环的上方定义另一个变量作为数组,并将url推入该数组。
    我已经修改了你的代码,参考下面,并为您的情况下提供了一个工作小提琴.
var url = 'google.com/page1'
fetch(url, {
        method: 'GET',
        credentials: 'include',
    })
    .then(function (response) {
        return response.text()
    })
    .then(function (html) {
        var parser = new DOMParser();
        var doc = parser.parseFromString(html, "text/html");
        var value4 = 'test4';
        var UrlArr = [];
        if (condition) {
            var name = doc.querySelectorAll("someClass");
            for (var i = 0; i < name.length; i++) {
                var allNames = name[i].outerHTML;
                var value1 = allNames.match('MyregExp1')[0];
                var value2 = allNames.match('MyregExp2')[0];
                var value3 = allNames.match('MYregExp3')[0];

                var newUrl = `google.com/?${value1}&${value2}&${value3}&test=${value4}`;

                console.log(value1);

                console.log(value2);

                console.log(value3);

                console.log(newUrl);
                UrlArr.push(newUrl);

            }
            if (allNames.length >= 2) {

                var newForm = Ext.create('Ext.form.Panel', {
                    title: 'Form',
                    width: 250,
                    autoHeight: true,
                    autoScroll: true,
                    bodyPadding: 10,
                    id: 'Form',
                });

                function newRadio() {
                    var radio = new Ext.form.Radio({
                        height: 40,
                        labelWidth: 110,
                        width: 20,
                        name: 'url',
                    });
                    return radio;
                }

                function newTextArea(id) {
                    var textArea = new Ext.form.TextArea({
                        fieldLabel: i + '_url',
                        height: 30,
                        labelWidth: 110,
                        width: 330,
                        submitValue: false,
                        readOnly: true,
                        autoScroll: true,
                        id: id
                    });
                    return textArea;
                }

                for (i = 0; i < name.length; i++) {
                    var id = i;
                    id = 'field_' + id;
                    var field = newTextArea(id);
                    var radioGr = newRadio();
                    var cont = NewContainer();
                    field.setValue(UrlArr[i]);
                    newForm.add(cont);
                }

                function NewContainer() {
                    var container = new Ext.container.Container({
                        xtype: 'container',
                        layout: 'hbox',
                        items: [radioGr, field]
                    });
                    return container;
                }

                var win = new Ext.Window({
                    title: "Window",
                    layout: 'fit',
                    modal: 'true',
                    height: 500,
                    width: 385,
                    items: [newForm],
                    buttons: [{
                        text: "Confirm",
                        handler: function () {}
                    }]
                });
                win.show();
            }
        }
    });

相关问题