顯示具有 Grid 標籤的文章。 顯示所有文章
顯示具有 Grid 標籤的文章。 顯示所有文章

2023年4月10日 星期一

ExtJs 的Grid介紹與常用功能 - store -proxy() - reader - V120502 - Grid欄位修改

 目的: ExtJs 的Grid介紹與常用功能  

-   網址 :  https://wadehuanglearning.blogspot.com/2017/01/ext.html

處理說明: 1>Grid 主要分成兩部份:
                      1>>一個顯示欄位標題的Columns。
                     2>>一個儲存資料的Store 元件。
                 2>Columns 欄位定義:
                     1>>Columns用來Grid 的定義表格欄位,具有header與dataIndex屬性的物件陣列。 
                      2>>利用header 屬性來決定Grid 的欄位名稱。
                      3>>利用dataIndex 屬性來與Store 的資料相配對。
                      4>>Grid 的欄位顯示順序完全由此Columns 加入的順序決定。
                 3>Store 介紹: 用來存取表格的原始資料,
                      1>>可以是近端的二維陣列(Array ),或是遠端的JSON 或XML 資料。
                      2>>利用fields 屬性來與columns 欄位相配對。

                      3>>reader 用來解析原始資料,如原始資料格式為json,reader的type 就填json
                                     ,type 可以是 array, json, xml
                      5>>proxy : 資料來源 , type: 'memory',   //store.data  
                                                             type: 'ajax'       ,   //遠端資料庫 url ,np

一.簡單範例 - store資料來源: data(local)  , proxy.type:'memory'



var store = Ext.create('Ext.data.Store', {
    fields: ['name', 'email', 'phone'],  //利用fields 與Columns 的dataIndex 做配對。
    data: {'items': [             //原始資料
            {'name': 'Lisa', "email": "lisa@simpsons.com", "phone": "555-111-1224"},
            {'name': 'Bart', "email": "bart@simpsons.com", "phone": "555-222-1234"},
            {'name': 'Homer', "email": "home@simpsons.com", "phone": "555-222-1244"},
            {'name': 'Marge', "email": "marge@simpsons.com", "phone": "555-222-1254"}
        ]},
    proxy: {
        type: 'memory',    //若 store 資料來源為遠端,則proxy.type:'ajax' , url:'xx/xx/', getMethod: function () { return 'POST'; },
                           //若 store 資料來源為Data,則proxy.type:'memory' 
        reader: {
            type: 'json',  //reader用來解析原始資料,如原始資料格式為json,reader 的type 就填json 。 #可以是 array, json, xml
            root: 'items'  //告訴reader 要選擇data 中哪一個含有raw Data 的Array 的物件。
        }
    }
});

var columnModel = [     ////建立ColumnMode
    {text: '名稱', dataIndex: 'name'},
    {text: '電子信箱', dataIndex: 'email', flex: 1},
    {text: '電話', dataIndex: 'phone'}
];

Ext.create('Ext.grid.Panel', {    // 新增Grid
    title: 'Grid 範例',
    store: store,                 //將預先寫好的store 填入grid
    columns: columnModel,         //將預先寫好的columns 填入grid
    height: 200,
    width: 400,
    renderTo: Ext.getBody()
});




二. 產生子視窗 - 含Grid  - 利用 Template , 遠端資料庫


1>*.js
//產生子視窗 - 含 Grid
function PrnPackBtn_click() {
    var sub_Columns = [
        { header: "工號", dataIndex: "SAPNO", width: 150, TMType: "string" },
        { header: "件號", dataIndex: "PN", width: 150, TMType: "string",//editor: 'textfield',},
        {
            header: "數量", dataIndex: "QTY", width: 50, TMType: "string",            
            editor: {
                xtype: 'textfield',
                allowBlank: false   //不允許空白
            },
        },
        { header: "EO", dataIndex: "EOCND", width: 150, TMType: "string" },
        { header: "備註", dataIndex: "RMK", width: 200, TMType: "string", },
    ]

    var sub_Fields = [
        { name: "SAPNO" },
        { name: "PN" },
        { name: "QTY" },
        { name: "EOCND" },
        { name: "RMK" },
    ];
    //包籤列印 sub_Grid
    var sub_Grid = Ext.create('TMGrid', {
        grid_id: 'sub_Grid',
        columns: sub_Columns,
        autoScroll: true,
        flex: 1,        
        store: Ext.create('gridstore', { model: sub_Fields }),
        plugins: [
            Ext.create('Ext.grid.plugin.RowEditing', {   // 加入官方 CellEditing 差件
                clicksToEdit: 1                           // 按一下進行編輯,預設為按兩下
            })
        ],
    });

    //將子視窗 store 不分頁,  PageToolBar.Visible 設為 false , 不分頁
    Ext.getCmp('sub_Grid_ptb').setVisible(false);

2>TMGrid.js  : 定義 TMGrid   &  gridstore
Ext.define('gridstore', {
    extend: 'Ext.data.Store',
    pageSize: 30,
    //model: gridmodel,
    autoLoad: false,
    remoteFilter: true,
    //remoteSort: true,
    proxy: {
        type: "ajax",
        url: '',
        getMethod: function () { return 'POST'; },
        reader: {
            type: "json",
            root: 'T1',
            totalProperty: 'T1C[0].TOTAL'
        }
    },
    sorters: [],
    constructor: function (config) {
        Ext.apply(this, config); //將gridstore本身的屬性與input config結合
        if (typeof config.model != "undefined") {
            J_gridmodel = config.model;
        }
        Ext.define('gridmodel', {
            extend: 'Ext.data.Model',
            fields: J_gridmodel
        });
        this.model = gridmodel;
        this.callParent(config);
        this.getProxy().setModel(gridmodel, true);
    }
});





2022年8月10日 星期三

V120902 - 自動設定子畫面 - Grid的高度 , - 判斷元件是否已顯示在畫面(.el)

目的: 視窗大小調整時, 設定 Grid的度度

處理說明 :  1>Grid的高度 = Form.高度 - 過濾Panel.高度  - 按鈕Panel.高度;
                     2>若 Grid 已 Create, 但是尚未顯示在畫面,則無法 getHeight() 及 getWidth()
                          判斷元件是否已顯示在畫面 ,     typeof Ext.getCmp("grid_M").el
                          --> if  (typeof Ext.getCmp("grid_M").el != undefined)      
                                        Ext.getCmp("grid_M").getHeight();



 // [缺勤記錄維護]鈕  - 子畫面欄位

     var mySub2 = [
            {
                type: 'panel', bodyStyle: "background-color:transparent;", border: 0, layout: { type: 'vbox', align: 'stretch' }, padding: "5",
                items: [
                    {
                        xtype: 'panel',
                        id: 'sub2_panel1',
                        //title: 'sub2_panel1',
                        layout: { type: 'hbox', align: 'stretch' },
                        flex: 0,
                        border: 1,
                        items: [
                            {
                                xtype: 'panel',
                                id: 'sub2_panel11',
                                layout: { type: 'vbox', align: 'stretch' },
                                flex: 15,
                                border: 0,
                                items: [{
                                 xtype: "fieldcontainer", fieldLabel: "日期", labelWidth: 60, layout: "hbox", flex: 2,
 items: [{ xtype: "tmdatefield", id: "sub2_DT", name: "sub2_DT", width: 100, padding: "0 4 0 0", },]
                                }, // end of s_日期
                      
                                ]
                            }, //end of sub2_panel11 , flex:20
                      
                            }, //end of sub2_panel12 , flex:20
                            {
                                //xtype: 'toolbar',
                                xtype: 'panel',
                                id: 'sub2_panel13',
                                layout: { type: 'vbox', align: 'right' },
                                flex: 5,
                                border: 0,
                                items: [
                                    {
                                        xtype: 'button',
                                        id: 'sub2_btn_Show',
                                        flex: 2,
                                        border: 1,
                                        text: '資料顯示',  //sub2
                                        iconCls: 'icon-search',
                                        handler: function () {
                                            var np = {};
                                            np["sub2_DT"] = Ext.getCmp("sub2_DT").getValue();
                                            np["sub2_DEP"] = Ext.getCmp("sub2_DEP").getValue();
                                            np["sub2_LDMAN"] = Ext.getCmp("sub2_LDMAN").getValue();
                                            //console.log('np:', np);
             Ext.getCmp('sub2_Grid').store.getProxy().url = '../api/V120902API/getsub2Data';
              Ext.getCmp('sub2_Grid').store.getProxy().extraParams = np; //分頁OK,篩選條件OK
                                            Ext.getCmp('sub2_Grid').reloadGirdData();                                            
Ext.getCmp('mySubForm').getHeight());
Ext.getCmp('sub2_panel1').getHeight());
Ext.getCmp('sub2_panel2').getHeight());
Ext.getCmp('sub2_Grid').getHeight());
Ext.getCmp('sub2_Grid').getHeight());
                                        }
                                    },  // end of 顯示資料
                                    {
                                        xtype: 'button',
                                        id: 'sub2_btn_clear',
                                        flex: 2,
                                        border: 1,
                                        text: '清除條件',
                                        iconCls: 'icon-clear',
                                        handler: function () {
                                            var f = Query.getForm();
                                            f.reset();
                                        }
                                    },  // end of 清除條件                                   
                                ]  // end of items of [顯示資料][清除條件]
                            }   // end of panel13
                        ]
                    },  //end of panel1                    
                    {
                        xtype: 'panel',
                        id: 'sub2_panel2',
                        //title: 'sub2_panel2',
                        layout: { type: 'vbox', align: 'stretch' },
                        //flex: 60,
                        border: 1,
                        items: [sub2_Grid]
                    },
                ] // end of   layout: "vbox", padding: "5", items: [
            }  //end of  my_Sub2 , items[{
        ]      //end of  my_Sub2 , items[
        var win = getMyWindow("缺勤記錄維護", mySub2, mySub2_Btns);

        win.width = 700;
        win.height = 400;
        win.on('resize', function (me, width, height, eOpts) {
            //console.log("win.resize height=",height);
            var Tmp_okpanel_Height = Ext.getCmp('mySub2_OkBtn').getHeight() + 20;            
            var Tmp_rHeight = Ext.getCmp('mySubForm').getHeight() - Ext.getCmp('sub2_panel1').getHeight() - Tmp_okpanel_Height;            
            Ext.getCmp('sub2_panel2').setHeight(Tmp_rHeight);
            //console.log("win.resize Tmp_rHeight=", Tmp_rHeight);            
        });
        win.show();

2022年7月14日 星期四

Web改版 - V120902- CheckBox欄位編輯處理

 Web改版V120902 – 出勤現況維護

目的: Checkbox編輯欄位值

問題: 1>如何在 Grid    Columns 加入 CheckBox

                    2>如何在後端讀取 CheckBox欄位值



Q1>如何在 Grid    Columns 加入 CheckBox

Ans:     1>於Grid的 Columns加入一欄位 : xtype: 'checkColumn',  

             {header: '缺勤',
                dataIndex: 'NWK',
                xtype: 'checkcolumn',
                          :
                }

                   2> checkColumn    renderer 時,  傳回  <input    type='checkbox'      checked/>
                           依欄位值 , 設定   checked   或空白
                          renderer: function (value, metadata, record, rowIndex, colIndex, store) {
                           var tempVal = '';
                           if ((value === 'Y') || (value === true)) { tempVal = 'checked'; }
                                   var Tmp_Str = "<input  type='checkbox'  name=" + DatetoStr(record.get('DT')) + "_" +                                                                                                            record.get('DEP') + "_" + record.get('EMPLYID') + "   " + tempVal + " >";
                              return Tmp_Str;
                },

              3>按[確定]鈕,  每筆 store 處理   [NWK]欄位值
                      var Tmp_Grid = Ext.getCmp('sub2_Grid');
                       var Tmp_store = Tmp_Grid.store;
                      var Tmp_TotalCnt = Tmp_store.getTotalCount();
for (i = 0; i < Tmp_TotalCnt; i++) {
                console.log("更新第 i  筆", i);
                //依目前 store的資料, 更新 AMM_FACDEPD.NWK , PK( DT+DEP+EMPLYID)
                cur_rec = Tmp_store.getAt(i);
                Tmp_DT = cur_rec.data["DT"];
                Tmp_DEP = cur_rec.data["DEP"];
                Tmp_EMPLYID = cur_rec.data["EMPLYID"];
                Tmp_LDMAN = cur_rec.data["LDMAN"];
                Tmp_NWK = cur_rec.data["NWK"];
                         : 
                          }

PS1:     加入  checkbox   欄位

 var sub2_Columns = [
            { header: "員工編號", dataIndex: "EMPLYID", width: 150, TMType: "string" },
            { header: "姓名", dataIndex: "EMPLYID_", width: 100, TMType: "string" },

            { header: "備註", dataIndex: "NOTE", width: 200, TMType: "string", },
            {
                header: '缺勤',
                dataIndex: 'NWK',
                xtype: 'checkcolumn',
                width: 60,
                editor: {
                    xtype: 'checkbox',
                    cls: 'x-grid-checkheader-editor',
                    inputValue: 'Y',
                    uncheckedValue: 'N'
                },
                //A renderer function used to return HTML markup for a cell given the cell's data value.
                renderer: function (value, metadata, record, rowIndex, colIndex, store) {
                    var tempVal = '';
                    if ((value === 'Y') || (value === true)) { tempVal = 'checked'; }
                    var Tmp_Str = "<input  type='checkbox'  name=" + DatetoStr(record.get('DT')) + "_" + record.get('DEP') + "_" + record.get('EMPLYID') + "   " + tempVal + " >";
                    return Tmp_Str;
                },
            }, // end of  header='缺勤1'
        ];

PS2:    renderer 時, 依[NWK]欄位值 , 設定 checked


 Q2>如何在後端讀取 CheckBox欄位值

Ans:

public HttpResponseMessage UPD_AMM_FACDEPD()
 string Tmp_NWK = nvc["Tmp_NWK"];
   if ((Tmp_NWK=="true") || (Tmp_NWK == "Y"))
                { Tmp_NWK_VAL = "Y"; }
                else
                { Tmp_NWK_VAL = "N"; }

 Tmp_Sql = "  UPDATE  AMM_FACDEPD  "
                                + "  SET          NWK=" + myfunc.AA(Tmp_NWK_VAL)+","
                                                         +"  NOTE=" + myfunc.AA(Tmp_NOTE)
                                + "  WHERE   DT= " + myfunc.AA(Tmp_DT)
                                + "   AND       DEP= " + myfunc.AA(Tmp_DEP)
                                + "   AND       EMPLYID= " + myfunc.AA(Tmp_EMPLYID);