2022年8月8日 星期一

登入使用者, 登入部門 , 1>c# User.Identity.Name 2>js loginInfo.depId - Login - EMPLYID

 1>*.cs  c#   -->  login 使用者id  , User.Identity.Name

using TLSWEB_AMM.Models;

//記錄系統所有登入使用者的 id  : User.Identity.Name

//舊寫法 : LoginUserModel.LoginUserId 

string Tmp_Sql = "  SELECT DEPID "
                           + " FROM HR_EMPLYM "
                           + " WHERE EMPLYID='" +  User.Identity.Name+ "')";
string n = funId + "_EMPID";
DataSet ds = setupPickDs(n, Tmp_Sql, new string[] { "AMM_UPHDAY" });



2>*. js   tlsbase.js --> loginInfo.depId != '856'  ,    loginInfo.id

       Ext.Loader.loadScript({    url: '../Scripts/tlsbase.js'  });

        np["ASPNO"] = "ASP" + DatetoStr(now, "Ymd");
        var Tmp_cur_DT = DatetoStr(now, "Y/m/d");  //呼叫 myfunc.js DatetoStr();
        
        var Tmp_MKER = loginInfo.id;
        var Tmp_MKNM = loginInfo.name;
        var Tmp_MKDT = DatetoStr(now, "Y/m/d");  
        Ext.getCmp("MKER").setValue(Tmp_MKER);
        Ext.getCmp("MKDT").setValue(Tmp_MKDT);

ps: loginInfo Object:
                id = stringArray[0],
                name = stringArray[1],
                usgid = usGid,
                email = stringArray[2],
                depId = stringArray[3],
                depName = stringArray[4],
                depMgr = stringArray[5],

if (loginInfo.depId != '856') 
   var Tmp_Sql = " SELECT  DEP ,LDMAN    "
                          + "  FROM   AMM_FACDEP  "
                          + " WHERE  EMPLYID=" + AA(loginInfo.id)
                          + "  AND       rownum=1 ";
        var Tmp_Str = SqlValue(Tmp_Sql);

2022年8月5日 星期五

V120103 – 匯入 – 匯入結果以 Cookie 傳回 - HttpCookie 用法說明

目的: 如何匯入檔案 且  傳回匯入結果

處理說明 : 1>如何匯入檔案
                   2>如何傳回匯入結果 



0>[挑選檔案]元件 & 匯入子畫面 
//[匯入]子畫面
var J_formFields_Sub_In = [
{
xtype: 'filefield',
id: 'sub_FName',
name: 'sub_FName',
fieldLabel: '檔案名稱',
labelWidth: 60,
msgTarget: 'side',
allowBlank: false,
flex: 1,
anchor: '100%',
buttonText: '選擇檔案',
listeners: {
'change': function (fb, v) {
var fileName = getFileName(v.replace(/C:\\fakepath\\/g, ''));
Ext.getDom(fb.inputId).value = fileName;
//Ext.getDom(fb.inputId).value = v;
}
}
},
];

  //[匯入]按鈕處理
    function xlsInBtn_click() {
        console.log(" 2 inside  xlsInBtn_click() ");
        var mySubForm = Ext.create('Ext.form.FormPanel', {
            autoScroll: true,
            fieldDefaults: {
                labelAlign: 'right',
            },
            layout: {
                type: 'vbox',
                align: 'stretch'
            },
            id: "mySubForm_2",
            form_id: "mySubForm_2",
            items: J_formFields_Sub_In,
            buttons: [
                {
                    text: "確定匯入", id: "mySubForm_2_btnok", handler: function () {
                        mySubForm_2_btnokClick();
                        //this.up("window").close();
                        //this.up("window").destroy();
                        //Ext.getCmp('btn_Show').fireHandler();                
                    }
                },
                {
                    text: "取消", id: "mySubForm_2_btncancel", handler: function () {
                        var Tmp_win = this.up("window");
                        if (Tmp_win == undefined) {
                            Ext.Msg.alert("關閉時未取到Window Object");
                        }
                        this.up("window").close();
                        this.up("window").destroy();
                    }
                }
            ]
        }
        );
        var win = getEditWindow_my("匯入", mySubForm, 1);
        win.width = 600;
        win.height = 100;
        win.show();
    };


1>匯入檔案: *.js
    //[確認匯入]鈕 , 處理
    function mySubForm_2_btnokClick() {
        //取得 [檔案上傳].檔名
        var btn_FileIn_Obj = Ext.getCmp('sub_FName');
        var Tmp_Str = "";
        var np = {
            FName: Ext.getDom(btn_FileIn_Obj.inputId).value
        };
        console.log("FName: ", np.FName);
        //submit會將fileUploadForm裡面input name送到後端    
        Ext.getCmp('mySubForm_2_btnok').up("form").submit({
            //standardSubmit: false,    //default:false
            url: '../api/V120103API/uploadFileToDB',
            method: 'POST',
            async: false,
            headers: { 'Content-type': 'multipart/form-data' },
            params: np,
})


2>*.cs
//[匯入] 鈕 , 匯入 XLS 檔案
        [HttpPost]
        //public HttpResponseMessage uploadFileToDB()
        public void uploadFileToDB()
        {

            HttpContext c = HttpContext.Current;
            NameValueCollection nvc = c.Request.Form;
            //匯入檔案名稱(含 *.xlsx)
            string FName = nvc["FName"];
            string FName0 =Path.GetFileNameWithoutExtension(FName) ;  //不含 extension
            string Tmp_FName = FName;
            Workbook wk = null;

            HttpRequest Request = HttpContext.Current.Request;
            var response = this.Request.CreateResponse();
            string file_Name = FName;
            string FILEDT = DateTime.Now.ToString("yyyy-MM-dd");
            string Tmp_RtnMsg = "";
            string Tmp_Rtn_JSON = "";   //Tmp_Rtn_JSON = "{success: false,FName:" + myfunc.AA(FName) + ",Rtn_Msg:" + myfunc.AA(Tmp_RtnMsg) + "}";
            string Tmp_Sql = "", Tmp_Str = "";
            int Tmp_CNT = 0;
            MemoryStream stream = new MemoryStream();
            HttpCookie MyCookie = new HttpCookie("Rtn_Msg");
            foreach (string cur_FName in Request.Files)
            {
                string fileType = Request.Files[cur_FName].ContentType;
                Stream file_Strm = Request.Files[cur_FName].InputStream;
                file_Name = Path.GetFileName(Request.Files[cur_FName].FileName);                
                int fileSize = Request.Files[cur_FName].ContentLength;
                byte[] fileRcrd = new byte[fileSize];
                //更新資料庫欄位值                
                int Tmp_curpos = 0;                
                bool Tmp_isOK = true;
                try
                {

                    wk = new Workbook();
                    wk.LoadFromStream(file_Strm);
                    Worksheet sheet1 = wk.Worksheets[0];//獲取第一個工作表
                                                        //共 10 欄
                                                        //string[] outFieldArray = { "計畫代碼", "父件件號", "件號", "件號中文名稱", "每機用量", "BOM量", "計量單位", "工廠", "類別","備註" };
                                                        //int RowsCount = sheet1.Rows.Count();
                    int RowsCount = sheet1.LastRow;
                    string Tmp_CLS,Tmp_ITM,  Tmp_DITM, Tmp_NM, Tmp_REPN, Tmp_RESN, Tmp_INPN, Tmp_INSN, Tmp_RMK;
                    string Tmp_PLNC, Tmp_REWK, Tmp_INSC, Tmp_STLCT, Tmp_NOTE, Tmp_FACWC;
                    string Tmp_RESULT;                    

                    //STEP1 檢核  XLS 欄位名稱資料是否正確                                            
                    //類別 排序項次 名稱 拆移件號 拆移序號 安裝件號 安裝序號 備考
                    // 拆移PlanningCard 修理REWORK 安裝InstallationCard 儲位 備註 拆移工廠
                    Tmp_CLS = sheet1.Range[myfunc.GetExcelPos(0, 0)].Value;                    
                    Tmp_DITM = sheet1.Range[myfunc.GetExcelPos(1, 0)].Value;
                    Tmp_NM = sheet1.Range[myfunc.GetExcelPos(2, 0)].Value;
                    Tmp_REPN = sheet1.Range[myfunc.GetExcelPos(3, 0)].Value;
                    Tmp_RESN = sheet1.Range[myfunc.GetExcelPos(4, 0)].Value;
                    Tmp_INPN = sheet1.Range[myfunc.GetExcelPos(5, 0)].Value;
                    Tmp_INSN = sheet1.Range[myfunc.GetExcelPos(6, 0)].Value;
                    Tmp_RMK = sheet1.Range[myfunc.GetExcelPos(7, 0)].Value;
                    // string Tmp_PLNC, Tmp_REWK, Tmp_INSC, Tmp_STLCT, Tmp_NOTE, Tmp_FACWC;
                    Tmp_PLNC = sheet1.Range[myfunc.GetExcelPos(8, 0)].Value;
                    Tmp_REWK = sheet1.Range[myfunc.GetExcelPos(9, 0)].Value;
                    Tmp_INSC = sheet1.Range[myfunc.GetExcelPos(10, 0)].Value;
                    Tmp_STLCT = sheet1.Range[myfunc.GetExcelPos(11, 0)].Value;
                    Tmp_NOTE = sheet1.Range[myfunc.GetExcelPos(12, 0)].Value;
                    Tmp_FACWC = sheet1.Range[myfunc.GetExcelPos(13, 0)].Value;
                    //新增[結果說明]欄位
                    Tmp_RESULT = "結果說明";
                    sheet1.Range[myfunc.GetExcelPos(14, 0)].Value=Tmp_RESULT;

                    //xls 欄位名稱有問題                    
                    //類別 排序項次 名稱 拆移件號 拆移序號 安裝件號 安裝序號 備考
                    // 拆移PlanningCard 修理REWORK 安裝InstallationCard 儲位 備註 拆移工廠
                    Tmp_RtnMsg = "匯入檔案欄位名稱必需如下:<br>";                     
                    if ((Tmp_CLS != "類別") || (Tmp_DITM != "排序項次") || (Tmp_NM != "名稱") || (Tmp_REPN != "拆移件號") || (Tmp_RESN != "拆移序號") ||
                          (Tmp_INPN != "安裝件號") || (Tmp_INSN != "安裝序號") || (!Tmp_RMK.Contains("備考")) ||
                          (Tmp_PLNC != "拆移PlanningCard") || (Tmp_REWK != "修理REWORK") || (Tmp_INSC != "安裝InstallationCard") || 
                          (Tmp_STLCT != "儲位") || (Tmp_NOTE != "備註") || (Tmp_FACWC != "拆移工場")
                          )
                    {
                        Tmp_RtnMsg = "匯入檔案欄位名稱必需如下:<br>"
                                                 +"'類別','排序項次','名稱','拆移件號','拆移序號',<br>"
                                                 +"'安裝件號','安裝序號','備考','拆移PlanningCard','修理REWORK',<br>"
                                                 +"'安裝InstallationCard','儲位','備註','拆移工場'<br>"
                                                 +"敬請檢核";
                        //Tmp_Rtn_JSON = "{success: false,FName:" + myfunc.AA(FName) + ",Rtn_Msg:" + myfunc.AA(Tmp_RtnMsg) + "}";
                        //response.Content = new StringContent(Tmp_Str);    // 回應內容
                        //return response;
                        //Tmp_Str = "已匯出完成!!  (" + Tmp_Cnt.ToString() + "筆) <br>"
                        //   + "檔案名稱(" + Tmp_FName + ")";
                        // 傳回結果字串      
                        MyCookie.Value=  HttpUtility.UrlEncode(Tmp_RtnMsg);
                        HttpContext.Current.Response.Cookies.Add(MyCookie);
                        HttpContext.Current.Response.End();
                        return ;
                    };

                    int Tmp_index;
                    Tmp_isOK = true;
                    //STEP2 檢核資料
                    for (int i = 0; i < RowsCount; i++)
                    {
                        Tmp_curpos = i + 1;
                        //if (Tmp_curpos == 290)
                        //{
                        //    Tmp_Str = " 第 290筆資料";
                        //};          
                        Tmp_RESULT = "";
                        Tmp_CLS = sheet1.Range[myfunc.GetExcelPos(0, i + 1)].Value;
                        if (myfunc.checkisnull(Tmp_CLS))
                        { break; }
                        Tmp_DITM = sheet1.Range[myfunc.GetExcelPos(1, i + 1)].Value;
                        Tmp_REPN = sheet1.Range[myfunc.GetExcelPos(3, i + 1)].Value;
                        Tmp_RMK = sheet1.Range[myfunc.GetExcelPos(7, i + 1)].Value;
                        if (Tmp_DITM.Length == 0)
                        { Tmp_RESULT = Tmp_RESULT + "排序項次不可空白,"; Tmp_isOK = false; }
                        if (Tmp_REPN.Length == 0)
                        { Tmp_RESULT = Tmp_RESULT + "拆移件號不可空白,"; Tmp_isOK = false; }
                    if ((Tmp_RMK!= "A") && (Tmp_RMK != "B") && (Tmp_RMK != "C") && (Tmp_RMK != "D") && (Tmp_RMK != "E") && (Tmp_RMK != "F"))
                        { Tmp_RESULT = Tmp_RESULT + "備考欄位值必需在 A~F 之間"; Tmp_isOK = false; }                        
                        if (Tmp_RESULT.Length > 0)
                            sheet1.Range[myfunc.GetExcelPos(14, i + 1)].Value = Tmp_RESULT;                            
                        else
                            sheet1.Range[myfunc.GetExcelPos(14, i + 1)].Value = "check ok";
                    }  // for i=0.. 檢查資料                    
                    
                    

                    //若資料不正確,則不寫入資料庫,只傳回檔案
                    if (Tmp_isOK == false)
                    {
                        Tmp_Str = "匯入檔案欄位值需修正!!<br>"
                                    + "請檢核如下檔案名稱(" + Tmp_FName + ")<br>";
                                    
                        MyCookie.Value = HttpUtility.UrlEncode(Tmp_Str);
                        // 若匯入資料有問題, 則不寫入資料庫 , 傳回結果檔案(*_結果.xlsx)
                        Tmp_FName = FName0 + "_結果.xlsx";
                        wk.SaveToStream(stream, FileFormat.Version2007);
                        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + HttpUtility.UrlEncode(Tmp_FName, System.Text.Encoding.UTF8) + "\"");
                        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
                        HttpContext.Current.Response.BinaryWrite(stream.ToArray());
                        HttpContext.Current.Response.Cookies.Add(MyCookie);
                        HttpContext.Current.Response.End();
                        return;
                    }

                    //STEP3 寫入資料庫

                    for (int i = 0; i < RowsCount; i++)
                    {
                        Tmp_curpos = i + 1;
                        //if (Tmp_curpos == 290)
                        //{
                        //    Tmp_Str = " 第 290筆資料";
                        //};                        
                        Tmp_CLS = sheet1.Range[myfunc.GetExcelPos(0, i + 1)].Value;
                        if (myfunc.checkisnull(Tmp_CLS))
                        { break; }
                        Tmp_DITM = sheet1.Range[myfunc.GetExcelPos(1, i + 1)].Value;
                        Tmp_NM = sheet1.Range[myfunc.GetExcelPos(2, i + 1)].Value;
                        Tmp_REPN = sheet1.Range[myfunc.GetExcelPos(3, i + 1)].Value;
                        Tmp_RESN = sheet1.Range[myfunc.GetExcelPos(4, i + 1)].Value;
                        Tmp_INPN = sheet1.Range[myfunc.GetExcelPos(5, i + 1)].Value;                        
                        Tmp_INSN = sheet1.Range[myfunc.GetExcelPos(6, i + 1)].Value;
                        //"'類別','排序項次','名稱','拆移件號','拆移序號','安裝件號','安裝序號','備考','拆移PlanningCard','修理REWORK','安裝InstallationCard','儲位','備註','拆移工廠'<br>"
                        Tmp_RMK = sheet1.Range[myfunc.GetExcelPos(7, i + 1)].Value;
                        Tmp_PLNC = sheet1.Range[myfunc.GetExcelPos(8, i + 1)].Value;
                        Tmp_REWK = sheet1.Range[myfunc.GetExcelPos(9, i+1)].Value;
                        Tmp_INSC = sheet1.Range[myfunc.GetExcelPos(10, i + 1)].Value;
                        Tmp_STLCT = sheet1.Range[myfunc.GetExcelPos(11, i + 1)].Value;
                        Tmp_NOTE = sheet1.Range[myfunc.GetExcelPos(12, i + 1)].Value;
                        Tmp_FACWC = sheet1.Range[myfunc.GetExcelPos(13, i + 1)].Value;

                        Tmp_Sql = " SELECT COUNT(*) "
                                        +"  FROM   AMM_FRO3F16STD "
                                       + "  WHERE  CLS=" + myfunc.AA(Tmp_CLS)
                                       +"  AND        DITM=" + myfunc.AA(Tmp_DITM);
                        Tmp_CNT=int.Parse(myfunc.SqlValue(Tmp_Sql).ToString());
                        if (Tmp_CNT==0)
                        {        
                            Tmp_ITM = GET_MAX_ITM(Tmp_CLS).ToString();
                            Tmp_Sql = " INSERT  INTO  AMM_FRO3F16STD "
                                        + "  (CLS,ITM,DITM,NM,REPN,RESN,INPN,INSN,RMK,PLNC,REWK,INSC,STLCT,NOTE,FACWC) "
                                        + "  VALUES "
                                        + "("
                                        + myfunc.AA(Tmp_CLS) + ","
                                        + myfunc.AA(Tmp_ITM) + ","
                                        + myfunc.AA(Tmp_DITM) + ","
                                        + myfunc.AA(Tmp_NM) + ","
                                        + myfunc.AA(Tmp_REPN) + ","
                                        + myfunc.AA(Tmp_RESN) + ","
                                        + myfunc.AA(Tmp_INPN) + ","
                                        + myfunc.AA(Tmp_INSN) + ","
                                        + myfunc.AA(Tmp_RMK) + ","
                                        + myfunc.AA(Tmp_PLNC) + ","
                                        + myfunc.AA(Tmp_REWK) + ","
                                        + myfunc.AA(Tmp_INSC) + ","
                                        + myfunc.AA(Tmp_STLCT) + ","
                                        + myfunc.AA(Tmp_NOTE) + ","
                                        + myfunc.AA(Tmp_FACWC) 
                                        + ") ";
                            myfunc.SqlExec(Tmp_Sql);
                            sheet1.Range[myfunc.GetExcelPos(14, i + 1)].Value = "新增 OK!!";
                        }
                        else
                        {
                            Tmp_Sql = " UPDATE  AMM_FRO3F16STD "
                                        + "  SET    NM="+ myfunc.AA(Tmp_NM) + ","
                                                  + "   REPN=" + myfunc.AA(Tmp_REPN) + ","
                                                  + "   RESN=" + myfunc.AA(Tmp_RESN) + ","
                                                  + "   INPN=" + myfunc.AA(Tmp_INPN) + ","
                                                  + "   INSN=" + myfunc.AA(Tmp_INSN) + ","
                                                  + "   RMK=" + myfunc.AA(Tmp_RMK) + ","
                                                  + "   PLNC=" + myfunc.AA(Tmp_PLNC) + ","
                                                  + "   REWK=" + myfunc.AA(Tmp_REWK) + ","
                                                  + "   INSC=" + myfunc.AA(Tmp_INSC) + ","
                                                  + "   STLCT=" + myfunc.AA(Tmp_STLCT) + ","
                                                  + "   NOTE=" + myfunc.AA(Tmp_NOTE) + ","
                                                  + "   FACWC=" + myfunc.AA(Tmp_FACWC)                                         
                                        + "  WHERE  CLS=" + myfunc.AA(Tmp_CLS)
                                        + "  AND       DITM=" + myfunc.AA(Tmp_DITM);
                            myfunc.SqlExec(Tmp_Sql);
                            sheet1.Range[myfunc.GetExcelPos(14, i + 1)].Value = "更新 OK!!";
                        }                        
                    };  //end of for i= , 寫入資料庫
                    Tmp_Str = "匯入成功!!";
                    MyCookie.Value = HttpUtility.UrlEncode(Tmp_Str);                    
                    Tmp_FName = FName0 + "_結果.xlsx";
                    wk.SaveToStream(stream, FileFormat.Version2007);
                    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + HttpUtility.UrlEncode(Tmp_FName, System.Text.Encoding.UTF8) + "\"");
                    HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
                    HttpContext.Current.Response.BinaryWrite(stream.ToArray());
                    HttpContext.Current.Response.Cookies.Add(MyCookie);
                    HttpContext.Current.Response.End();                    
                    //Tmp_Str = "{success: true,FName:" + myfunc.AA(FName) + ",Rtn_Msg:" + myfunc.AA(Tmp_RtnMsg) + "}";
                    //response.Content = new StringContent(Tmp_Str);    // 回應內容
                    //return response;
                }// try
                catch (Exception e)
                {                                                                                          //失敗
                    Tmp_Str = "批次匯入失敗!!<br>"
                                             + "第" + Tmp_curpos.ToString() + "筆資料<br>"                                             
                                             + e.Message + "<br>"
                                             +Tmp_Sql+"<br>";
                    MyCookie.Value = HttpUtility.UrlEncode(Tmp_Str);
                    HttpContext.Current.Response.Cookies.Add(MyCookie);
                    HttpContext.Current.Response.End();
                    //+ Tmp_Sql ;
                    //Tmp_Str = "{success: false,FName:" + myfunc.AA(FName) + ",Rtn_Msg:" + myfunc.AA(Tmp_RtnMsg) +",Tmp_Sql:"+ myfunc.AA(Tmp_Sql) +"}";
                    //Tmp_Str = "{success: false,FName:" + myfunc.AA(FName) + ",Rtn_Msg:" + myfunc.AA(Tmp_RtnMsg) + "}";
                    //response.Content = new StringContent(Tmp_Str);    // 回應內容                    
                    //return response;
                }
            }  //end of foreach (string cur_FName in Request.Files)            
            return;
        }  //  end of  uploadFileToDB()

2022年8月3日 星期三

V120103 - 判斷目前編輯的模式(1:新增, 2:編輯,0:瀏覽,..) buttonModel.modelType

1>TMSingleViewEdit.js

var buttonModel = function () {
    this.modelType = 0; //預設檢視模式
    this.getModelType = function () {
        return this.modelType;
    }
}

Ext.define('G_buttonModel', {
getModelType: function () {
return buttonModel.modelType;
},
setModelType: function (type_num) {
buttonModel.modelType = type_num;
}
});


2>V120103.js 

Ext.onReady(function () {
     var TMSingleViewEdit = Ext.create('TMSingleViewEdit');
     var gridstore = Ext.getCmp('grid_Single').store;
     var buttonModel = Ext.create('G_buttonModel'); 
                         :
               Ext.getCmp('btn_save').setFormValue = function () {
               //要判斷 buttonModel 的編輯模式時, 才即時去抓 modelType
               var modelType = buttonModel.getModelType();

               console.log("setFormValue modelType=", modelType);
               //若非[新增]模式,則不設定欄位值
               if (!(modelType == 1))    
                   return true;

        var isOk = true;
        var Tmp_MAX_ITM = "";       
        np = {};
        np["CLS"] = Ext.getCmp('CLS').getValue();        
        Ext.Ajax.request({
            method: "POST",
            url: '../../api/V120103API/GET_MAXITM',
            params: np,
            async: false,
            success: function (response, opts) {
                var Tmp_Obj = Ext.decode(response.responseText);       
                if (Tmp_Obj["success"]) {
                    Tmp_MAX_ITM = Tmp_Obj['MAX_ITM'];
                    Ext.getCmp("ITM").setValue(Tmp_MAX_ITM);
                }
            }, // end of  success 
            failure: function (response, opts) {
                isOk = false;
            }
        });  // end of Ext.Ajax               
       return isOk;

}

 

Ext.syncRequire('TM.TMSingleViewEdit') 和 Ext.Loader.loadScript({ url: '../GUITemplate/TMSingleViewEdit.js', }) 的不同



Ext.Loader.loadScript({

url: '../JsFunction/V120103_JSON.js',

onLoad: function () { 
//load class & depend.class 而已,不是所有 *.js,  不可存取該 *.js 的全域變數
Ext.syncRequire('TM.TMSingleViewEdit');
}
});


Ext.Loader.loadScript({ //load *.js , 可以存取該 *.js 的全域變數
url: '../GUITemplate/TMSingleViewEdit.js',
});


Ex1:  Ext.Loader.loadScript({    url: '../../GUITemplate/TMSingleViewEdit.js',});

1>TMSingleViewEdit.js

var unique="1";

function StrExtract(par_Str, index, par_sper) {
    return ExtractStr(par_Str, index, par_sper);
}


2>V120103.js

Ext.Loader.loadScript({
    url: '../GUITemplate/TMSingleViewEdit.js',
});

Ext.onReady(function () { 
    console.log("unique=", unique);  --> 可以取得 TMSingleViewEdit.js 的全域變數 unique
     :
}

Ex2:  Ext.syncRequire('TM.TMSingleViewEdit');

2>若 V120103.js 
    Ext.syncRequire('TM.TMSingleViewEdit');
    console.log("unique=", unique);  --> 無法取得 TMSingleViewEdit.js 的全域變數 unique

建構子 function() { this.modelType=0; this.getModelType=function{return this.modeltype;}

/*
 * 建構子會如此做動:
 *
 * function MyConstructor(){
 *   // 實際的函式主體碼在這裡
 *   // 在|this| 上創建屬性
 *   // 希望通過分配給他們,如:
 *   this.fum = "nom";
 *   // et cetera...
 *
 *   // 如果函式有返回狀態它將返回一個物件
 *   // 那個物件將是新表達式的結果。
 *   // 換句話來說,表達式的結果是現在綁定 |this| 的物件
 *   // (例如,最常見的常見情況).
 * }
function C(){

this.a = 37; } var o = new C();

console.log(o.a); // logs 37



//static var
var buttonModel = function () {
    this.modelType = 0; //預設檢視模式
    this.getModelType = function () {
        return this.modelType;
    }
}

V120402 – [借閱登記] – 新增 – 取得ITM=最大值+1,

目的: V120402 – [借閱登記] – 新增 – 取得ITM=最大值+1, 

處理說明:  1>新增 - 取得 ITM最大值+1,



1>*.js  取得最大值 +1

Tmp_ITM = get_NEXT_ITM(Tmp_TMNO);

//取得   TMNO 的最大借閱記錄
    function string  get_NEXT_ITM(Tmp_TMNO)
    {
        var isOk = true;
        var Tmp_MAX_ITM = "";
        var  np = {};
        np["TMNO"] = Tmp_TMNO;
        Ext.Ajax.request({
            method: "POST",
            url: '../../api/V120402API/GET_MAXITM',
            params: np,
            async: false,
            success: function (response, opts) {
                var Tmp_Obj = Ext.decode(response.responseText);
                //console.log("Tmp_Obj[success]=", Tmp_Obj["success"]);
                if (Tmp_Obj["success"]) {
                    Tmp_MAX_ITM = Tmp_Obj['MAX_ITM'].toString();
                    if (Tmp_MAX_ITM.length == 0)
                        Tmp_MAX_ITM = '1';
                    console.log("Tmp_MAX_ITM=", Tmp_MAX_ITM);
                    return Tmp_MAX_ITM;
                    //mywarnalert(Tmp_Str);
                    //console.log(" 1 success return false");                    
                }
            }, // end of  success 
            failure: function (response, opts) {
                isOk = false;
            }
        });  // end of Ext.Ajax               
    }  // end of  function string  get_NEXT_ITM(Tmp_TMNO)


2>*.cs  - 
//以 TMNO  至 AMM_TMDWGD  取得  MAX(AMM_TMDWGD.ITM)+1 
        [HttpPost]
        public HttpResponseMessage GET_MAXITM()
        {
            var c = HttpContext.Current;
            NameValueCollection nvc = c.Request.Form;
            string Tmp_TMNO = nvc["TMNO"];

            OracleConnection conn = new OracleConnection(DBService.ConnectionString(DBLINK));//
            OracleCommand cmd = new OracleCommand();
            OracleDataReader reader;
            var response = this.Request.CreateResponse();

            conn.Open();
            conn.ClientInfo = User.Identity.Name;
            conn.ModuleName = BaseSYS + "_" + BaseMODID;
            conn.ActionName = ActionName;
            cmd.Connection = conn;

            string Tmp_Str = "";
            string Tmp_Sql = "  SELECT MAX(ITM)+1  as MAX_ITM"
                                       + "  FROM   AMM_TMDWGD "
                                       + "  WHERE TMNO= " + myfunc.AA(Tmp_TMNO);
            cmd.CommandText = Tmp_Sql;
            reader = cmd.ExecuteReader();
            string Tmp_MAX_ITM = "";
            if (reader.Read())
            {
                Tmp_MAX_ITM = reader["MAX_ITM"].ToString();
            }
            Tmp_Str = "{success: true,MAX_ITM:" + myfunc.AA(Tmp_MAX_ITM) + ","
                                        + " }";

            response.Content = new StringContent(Tmp_Str);    // 回應內容
            return response;
            //return Tmp_MAX_ITM;
        }  // end of get_MAX_ITM


//V120201 , *.cs 直接取得最大值, 不傳回前端
//取得  AMM_COMQ.APNO  最大值
        [HttpPost]
public string GET_NEXT_APNO()
  {
          string Tmp_Sql;
          string Tmp_DStr = "Q" + DateTime.Now.ToString("yyyyMMdd"); ;
          string Tmp_MAX_APNO, Tmp_NEXT_APNO;
          Tmp_Sql="  SELECT   MAX(APNO) "
                        +"  FROM     AMM_COMQ "
                        +"  WHERE   APNO LIKE  "+myfunc.AA(Tmp_DStr+'%');      
          Tmp_MAX_APNO=myfunc.SqlValue(Tmp_Sql);
          if (myfunc.checkisnull(Tmp_MAX_APNO)){
             Tmp_NEXT_APNO=Tmp_DStr+"001";
          }
          else {
                    //取流水號(末3碼)+1  
       int Tmp_NextSerno = int.Parse(Tmp_MAX_APNO.Substring(Tmp_MAX_APNO.Length - 3, 3));
                   Tmp_NextSerno=Tmp_NextSerno+1;
                   Tmp_NEXT_APNO =Tmp_DStr+Tmp_NextSerno.ToString("000");
             }
        return Tmp_NEXT_APNO;
    }

2022年8月2日 星期二

Question: V120103 - [新增] --> [存檔]資料後, 重新儲存資料, 並 Focus 在該目前編輯的資料

目的: Grid [新增] --> [存檔]資料後, Locate 至原本的新增的那筆資料

處理說明: 1> Grid 有分頁處理, 若新增的那筆資料, 位置在其他頁,要如何處理
                  2> store 要 如何 Scroll 至該筆資料
                  3> store.findby  用法

--> 若 [存檔]後, store.reload() ,則新增的該筆資料可能不見,
      所以應該由前端 取得 MAXITM 後, 直接設定 store.欄位值, 不重新 reload();





1>store.reload()後, 找出剛才新增的那筆資料
//onclick : 會先執行 partent.click , 才執行 this.click
    Ext.getCmp('btn_save').on('click', function () {
        console.log("btn_save click my");        
        //gridstore.reloadGirdData();
        var cur_recs = Ext.getCmp("grid_Single").getSelectionModel().getSelection();
        var cur_rec = cur_recs[0];
        console.log("cur_rec=", cur_rec);
        var Tmp_CLS, Tmp_DITM,Tmp_REPN;
        var np = {};
        Tmp_CLS = cur_rec.data["CLS"].toString();
        Tmp_DITM = cur_rec.data["DITM"].toString();
        Tmp_REPN = cur_rec.data["REPN"].toString();
        np = {
            CLS: Tmp_CLS,
            DITM: Tmp_DITM,
            REPN: Tmp_REPN,
        }
        console.log("np=", np);
//gridstore.reload(); 必需在 onload event - 讀完資料後, 才 findBy , 找到的index才會準 
        gridstore.reload();
        //gridstore.find("DITM", Tmp_DITM);

        gridstore.on('load', function () {
            console.log("inside onload");
            //若新增的資料在另一分頁, 則 reload()後, 無法找到 
            // --> 所以存檔時,即時更新 store的欄位值 , 不重新 reload() & 不locate 剛才編輯的資料
            var Tmp_index = gridstore.findBy(function (record, id) {
                var Tmp_bol = true;
                //console.log("record.data[CLS].toString()", record.data["CLS"].toString());
                //console.log("record.data[DITM].toString()", record.data["DITM"].toString());
                //console.log("record.data[REPN].toString()", record.data["REPN"].toString());
                Tmp_bol = record.data["CLS"].toString() == Tmp_CLS
                    && record.data["DITM"].toString() == Tmp_DITM
                    && record.data["REPN"].toString() == Tmp_REPN;
                console.log("Tmp_bol=", Tmp_bol);
                return Tmp_bol;   // true : 有找到
            });

            console.log(" index =", Tmp_index);
            cur_rec = gridstore.getAt(Tmp_index);
            Ext.getCmp("grid_Single").getSelectionModel().select(cur_rec);
            console.log(" Ext.getCmp(grid_Single).getSelectionModel().select(cur_rec); " );
        });
        console.log("after  onload ");
        //datastore.ITM欄位值設為 MAXITM
        //mysuccessalert("btn_save click");
    });