代码之家  ›  专栏  ›  技术社区  ›  Buwaneka Sudheera

将数据发送到API并将其检索到网格中

  •  0
  • Buwaneka Sudheera  · 技术社区  · 7 年前

    在我的项目中,我在前端使用extjs。后端的yii2。我创建了一个表单来从数据库中检索所选数据。

    enter image description here

    如您所见,我有两个日期字段和一个下拉列表。我有一组复选框。这是我的数据库表的屏幕截图。

    enter image description here

    我应该使用复选框选择所需的数据,并在“从”到“到”日期之间从数据库中获取它们。当我单击RUN按钮时,这些选定的数据应加载到下面的网格中。当我单击下载按钮时,这些选定的数据应该下载到csv文件中。但当我单击RUN按钮时,它会两次发送相同的API调用。一个API正确地获取数据,另一个API发送并错误地说“未定义的索引:来自”。在我看来,这就是代码。

               recordData: {
                  date: null,
                  from: null,
                  to: null,
                  rb1: null,
                  rb1: null,
                  rb2: null,
                  rb3: null,
                  rb4: null,
                  time: null,
                  rb5: null,
                  rb6: null,
                  rb7: null,
                  weight: 0,
                  status: 1
            }
      },
    
      initComponent: function () {
            var me = this;
            me.title = 'Reports',
                  me.store = Ext.create('store.Reports');
    
            Ext.apply(me, {
                  items: [{
                        xtype: 'form',
                        border: false,
                        padding: 10,
                        bodyStyle: { "background-color": "#e4e4e4" },
                        width: '100%',
    
                        store: me.store,
                        defaults: {
                              selectOnFocus: true,
                              labelWidth: 125
                        },
    
                        items: [{
                              xtype: 'datefield',
                              fieldLabel: 'From',
                              padding: '10 0 0 40',
                              name: 'from',
                              format: 'Y-m-d',
                              labelWidth: 150,
                              value: me.recordData.from,
                              displayField: 'from',
                              typeAhead: true,
                              queryMode: 'local',
                              emptyText: 'Select...'
                        }, {
                              xtype: 'datefield',
                              fieldLabel: 'To',
                              padding: '20 0 0 40',
                              name: 'to',
                              format: 'Y-m-d',
                              labelWidth: 150,
                              value: me.recordData.to,
                              displayField: 'to',
                              typeAhead: true,
                              queryMode: 'local',
                              emptyText: 'Select...'
                        }, {
                              xtype: 'combobox',
                              fieldLabel: 'Report Type',
                              padding: '20 0 0 40',
                              name: 'type',
                              labelWidth: 150,
                              store: [
                                    ['Order Report', 'Order Report']
    
                              ],
                              value: me.recordData.type,
                              displayField: 'type',
                              typeAhead: true,
                              queryMode: 'local',
                              emptyText: 'Select...'
                        }, {
                              width: '100%',
                              bodyStyle: { "background-color": "#e4e4e4" },
                              padding: '20 0 0 40',
                              bodyPadding: 10,
                              renderTo: Ext.getBody(),
                              items: [{
                                    xtype: 'checkboxgroup',
                                    fieldLabel: 'Customize Report',
                                    width: 700,
                                    labelWidth: 150,
                                    columns: 3,
                                    vertical: false,
                                    items: [
                                          { boxLabel: 'Order ID', name: 'rb1', inputValue: '1', itemId: 'check1' },
                                          { boxLabel: 'Connection Number', name: 'rb2', inputValue: '2', itemId: 'check2' },
                                          { boxLabel: 'Status', name: 'rb3', inputValue: '3', itemId: 'check3' },
                                          { boxLabel: 'Action', name: 'rb4', inputValue: '4', itemId: 'check4' },
                                          { boxLabel: 'LOB', name: 'rb5', inputValue: '5', itemId: 'check5' },
                                          { boxLabel: 'Channel', name: 'rb6', inputValue: '6', itemId: 'check6' },
                                          { boxLabel: 'Company Name', name: 'rb7', inputValue: '7', itemId: 'check7' }
                                    ]
                              }]
                        }, {
                              buttons: [{
                                    xtype: 'button',
                                    text: 'RUN',
                                    itemId: 'btnRun',
                                    handler: function (button, event) {
                                          //console.log("Working!", form);
                                          var form = button.up('form');
                                          //targetGridpanel = button.up();
                                          //console.log("Working!", targetGridpanel);
                                          //console.log("Working!", form);
                                          if (form.isDirty()) {
                                                var _vals = form.getValues();
    
                                                if (!form.isValid()) {
                                                      console.log("Not Working!");
                                                      Ext.Msg.show({
                                                            icon: Ext.MessageBox.ERROR,
                                                            buttons: Ext.Msg.OK,
                                                            title: me.action + ' Report',
                                                            msg: 'Fill mandatory fields'
                                                      });
                                                } else {
                                                      //console.log(_vals);
                                                      me.store.saveRecord(_vals, function () {
                                                      });
                                                      //me.store.load();
                                                      if (me.down('#check1').isDirty()) {
                                                            me.down('#rb1').show(true);
                                                      }
                                                      if (me.down('#check2').isDirty()) {
                                                            me.down('#rb2').show(true);
                                                      }
                                                      if (me.down('#check3').isDirty()) {
                                                            me.down('#rb3').show(true);
                                                      }
                                                      if (me.down('#check4').isDirty()) {
                                                            me.down('#rb4').show(true);
                                                      }
                                                      if (me.down('#check5').isDirty()) {
                                                            me.down('#rb5').show(true);
                                                            me.down('#time').show(true);
                                                      }
                                                      if (me.down('#check6').isDirty()) {
                                                            me.down('#rb6').show(true);
                                                      }
                                                      if (me.down('#check7').isDirty()) {
                                                            me.down('#rb7').show(true);
                                                      }
                                                }
                                          } else {
                                                console.log("Close!");
                                          }
                                    }
                              }]
                        }, {
                              xtype: 'gridpanel',
                              store: me.store,
                              flex: 1,
                              margin: '20 0 0 0',
                              //minHeight: 300,
                              height: 240,
                              viewConfig: {
                                    stripeRows: true
                              },
    
                              bbar: {
                                    xtype: 'pagingtoolbar',
                                    store: me.store,
                                    displayInfo: true,
                                    plugins: Ext.create('Ext.ux.ProgressBarPager')
                              },
    
                              columns: [{
                                    dataIndex: 'date',
                                    //itemId:'date',
                                    text: 'DATE',
                                    flex: 1,
                                    menuDisabled: false,
                              }, {
                                    dataIndex: 'rb1',
                                    itemId: 'rb1',
                                    text: 'ORDER ID',
                                    flex: 1,
                                    menuDisabled: false,
                                    hidden: true,
                              }, {
                                    dataIndex: 'rb2',
                                    itemId: 'rb2',
                                    text: 'CONNECTION NUMBER',
                                    menuDisabled: false,
                                    hidden: true,
                                    flex: 2
                              }, {
                                    dataIndex: 'rb3',
                                    itemId: 'rb3',
                                    text: 'STATUS',
                                    menuDisabled: false,
                                    hidden: true,
                                    flex: 1
                              }, {
                                    dataIndex: 'rb5',
                                    itemId: 'rb5',
                                    text: 'LOB',
                                    menuDisabled: false,
                                    hidden: true,
                                    flex: 1
                              }, {
                                    dataIndex: 'rb4',
                                    itemId: 'rb4',
                                    text: 'ACTION',
                                    menuDisabled: false,
                                    hidden: true,
                                    flex: 1
                              }, {
                                    dataIndex: 'time',
                                    itemId: 'time',
                                    text: 'ACTION TIME',
                                    menuDisabled: false,
                                    hidden: true,
                                    flex: 1
                              }, {
                                    dataIndex: 'rb6',
                                    itemId: 'rb6',
                                    text: 'CHANNEL',
                                    menuDisabled: false,
                                    hidden: true,
                                    flex: 1
                              }, {
                                    dataIndex: 'rb7',
                                    itemId: 'rb7',
                                    text: 'COMPANY NAME',
                                    menuDisabled: false,
                                    hidden: true,
                                    flex: 1.5
                              }]
                        }
                        , {
                              buttons: [{
                                    xtype: 'button',
                                    text: 'DOWNLOAD',
                                    itemId: 'download',
                                    //actionMethods: {'read': 'POST'},
                                    handler: function (button, event) {
                                          var self = button.up();
                                          var form = self.up('form');
                                          var vals = form.getValues();
                                          //console.log('Download', vals);
                                          if (vals.from && vals.to && vals.type && (vals.rb1 || vals.rb2 || vals.rb3 || vals.rb4 || vals.rb5 || vals.rb6 || vals.rb7)) {
                                                if (button) {
                                                      Ext.Msg.show({
                                                            icon: Ext.MessageBox.QUESTION,
                                                            buttons: Ext.Msg.YESNO,
                                                            title: 'Download Report',
                                                            msg: 'Do you want to download the <strong>selected</strong> report file?',
    
                                                            fn: function (buttonId, text, opt) {
    
                                                                  if ('yes' == buttonId) {
                                                                        //console.log(buttonId);
                                                                        var dummyFormId = 'py-form-' + (new Date()).getTime();
                                                                        //console.log(dummyFormId);
                                                                        var frm = document.createElement('form');
                                                                        frm.id = dummyFormId;
                                                                        frm.name = dummyFormId;
                                                                        //console.log(frm);
                                                                        frm.className = 'x-hidden';
                                                                        document.body.appendChild(frm);
    
                                                                        Ext.Ajax.request({
                                                                              url: utils.createUrl('api', 'report-download'),
                                                                              form: Ext.fly(dummyFormId),
                                                                              isUpload: true,
    
                                                                              params: {
                                                                                    from: vals.from,
                                                                                    to: vals.to,
                                                                                    type: vals.type,
                                                                                    rb1: vals.rb1,
                                                                                    rb2: vals.rb2,
                                                                                    rb3: vals.rb3,
                                                                                    rb4: vals.rb4,
                                                                                    rb5: vals.rb5,
                                                                                    rb6: vals.rb6,
                                                                                    rb7: vals.rb7
                                                                              },
    
                                                                              callback: function (opts, success, res) {
                                                                                    console.log('Hello');
                                                                                    //Ext.getBody().unmask();
                                                                                    //console.log(params);
                                                                                    try {
                                                                                          if (success) {
                                                                                                var response = Ext.decode(res.responseText);
                                                                                                if (!response.success) {
                                                                                                      throw response.data;
                                                                                                }
                                                                                          } else {
                                                                                                throw response.data;
                                                                                          }
                                                                                    } catch (ex) {
                                                                                          Ext.Msg.show({
                                                                                                icon: Ext.MessageBox.ERROR,
                                                                                                buttons: Ext.Msg.OK,
                                                                                                title: 'Download Report',
                                                                                                msg: 'No Data Found'
                                                                                          });
                                                                                    }
                                                                              },
    
                                                                              // fn: function () {
                                                                              //       console.log(arguments);
                                                                              // }
                                                                        });
                                                                  }
                                                            }
                                                      });
                                                }
                                          } else {
                                                Ext.Msg.show({
                                                      icon: Ext.MessageBox.ERROR,
                                                      buttons: Ext.Msg.OK,
                                                      title: 'Download Report',
                                                      msg: 'Please fill the form first'
                                                });
                                          }
                                    }
                              }
                        ]
    
                        }
                  ]
                  }],
    
            });
    
            me.callParent(arguments);
    

    我将这些数据发送到存储文件。这是存储文件代码。

    extend: 'Ext.data.Store',
    model: 'model.Report',
    
    storeId: 'reportStore',
    autoLoad: false,
    pageSize: Configs.grid.pageSize,
    
    saveRecord: function(data,fnCallBack) {
        var me = this;
        //var data = this.data;
        //autoLoad: true,
        //console.log(data);
        Ext.getBody().mask('Please wait...');
        Ext.Ajax.request({
            url: utils.createUrl('api', 'report-read'),
            params: data,
    
            callback: function(opts, success, res) {
                Ext.getBody().unmask();
    
                try {
                    if(success) {
                        var response = App.decodeHttpResp(res.responseText);
    
                        if(response.success) {
                            Ext.Msg.show({
                                icon: Ext.MessageBox.INFO,
                                buttons: Ext.Msg.OK,
                                title: 'Reports',
                                msg: 'Report saved successfully'
                            });
                        } else {
                            throw response.error;
                        }
                        me.load();
                    } else {
                        throw 'Unknown Reason';
                    }
                } catch (ex) {
                    Ext.Msg.show({
                        icon: Ext.MessageBox.ERROR,
                        buttons: Ext.Msg.OK,
                        title: 'Report',
                        msg: 'Failed to save data<br />' +
                             'Reason: ' + ex
                    });
                }
            }
        });
    }
    

    这是我的前端型号。

    extend: 'Ext.data.Model',
    
    fields: [
        { name: 'from', type: 'auto' },
        { name: 'to', type: 'auto' },
        { name: 'rb1', type: 'auto' },
        { name: 'rb2', type: 'auto' },
        { name: 'rb3', type: 'auto' },
        { name: 'rb4', type: 'auto' },
        { name: 'rb5', type: 'auto' },
        { name: 'time', type: 'auto' },
        { name: 'rb6', type: 'auto' },
        { name: 'rb7', type: 'auto' }
    ],
    
    
    proxy: {
        type: 'ajax',
        noCache: false,
    
        actionMethods: {'read': 'POST'},
    
        api: {
            read: utils.createUrl('api', 'report-read'),
            //create: utils.createUrl('api', 'user-update'),
            // update: utils.createUrl('api', 'user-update'),
            // destroy: utils.createUrl('api', 'user-delete')
        },
    
        reader: {
            type: 'json',
            root: 'data'
        },
    
        listeners: {
            exception: function(proxy, response, operation) {
                App.showHttpError('Reports', response);
                //console.log(this.fields);
            }
        }
    }
    

    使用这些文件,我将数据发送到控制器。这就是定义API的地方。

    这是我的控制器功能。

    public function actionReportRead(){
    
        $post = Yii::$app->request->post();
        $time = 0;
        $_vals = (new Order())->readReports(
            @$post['start'],
            @$post['limit'],
            $post['from'],
            $post['to'],
            @$post['rb1'],
            @$post['rb2'],
            @$post['rb3'],
            @$post['rb5'],
            @$post['rb4'],
            @$time,
            @$post['rb6'],
            @$post['rb7']
        );
        $this->setOutputData($_vals);
        $this->setOutputStatus(true);
    }
    

    这就是这方面的模式。

        public function readReports($start, $limit,$from,$to, $rb1, $rb2, $rb3, $rb5, $rb4, $time, $rb6, $rb7 )
    {
    
        if (!$start) { $start = 0; };
        if (!$limit) { $limit = Config::PAGE_SIZE; };
    
        //$q = (new ActiveQuery(self::className()));
        $q = self::find();
        //$q->where(['between', 'submitted_time', $from, $to ]);
        $q->alias('T')->andWhere(['BETWEEN', 'T.submitted_time', $from, $to ]);
    
        $q->limit($limit);
        $q->offset($start);
        $q->orderBy(['T.order_id' => SORT_ASC]);
    
        $data = [];
        $action = null;
        foreach ($q->all() as $_o) {
            if($_o->status == 2){
                $action = 'Data Entry Verified';
                $time = $_o->timeDataEntry;
            }else if($_o->status == 3){
                $action = 'QC Accepted';
                $time = $_o->timeQcPass;
            }else if($_o->status == 4){
                $action = 'Accepted';
                $time = $_o->timeVerify;
            }else if($_o->status == 1){
                $action = 'Verification Pending';
                $time = $_o->timeQcReject;
            }else if($_o->status == 0){
                $action = 'Rejected';
                $time = $_o->timeQcReject;
            }
    
            $userlist='SELECT name FROM Company WHERE id = '.$_o->company_id;
            $rsuserlist = Yii::$app->db->createCommand($userlist)->query();
            $row = $rsuserlist->read();
    
            $data[] = (object) array(
                'date'        =>$_o->submitted_time,
                'rb1'        =>$_o->order_id,
                'rb2'        =>$_o->conn,
                'rb3'        =>$_o->status,
                'rb5'        =>$_o->conn_type,
                'rb4'        =>$action,
                'time'        =>$time,
                'rb6'        =>$_o->channel,
                'rb7'        =>$row['name']
            );
    
        }
    
        $json=Json::encode($data);
        $this->logger->actLog($json);
        return $data;
    
    }
    

    正如我发现后端是好的。但我不太确定。我是extjs新手。所以,我尝试了很多方法,但都没有成功。数据没有加载到网格,API向我发送了错误,我之前提到过。请帮我解决这个问题。我还应该做些什么。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Buwaneka Sudheera    7 年前

    我找到了答案,我在回答我自己的问题。

    在这里,一个API可以正确获取所有数据。另一个无法获取“from”和“to”值。所以我使用了以下代码。

    me.store.getProxy().extraParams = {
        from: vals.from,
        to: vals.to
    };
    

    使用此方法,我可以将所有参数发送到其他API并消除该问题。现在,数据被毫无问题地提取到网格中。