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

2023年4月10日 星期一

V80204B -將前端 Array/table/Object 欄位值,傳送至後端處理 -JSON – 將目前 checkboxgroup欄位值(Array),傳送至後端,並處理

 目的: V80204B – 將目前 checkboxgroup欄位值(Array),傳送至後端,並處理

處理說明: 1>若非基本型態(string/int/)的欄位值(Array/table/Object),要傳至後端, 
                       均需先轉成 JSON string型態 ,才可當成np的參數值,傳送至後端
                        *.js                         
                        var Tmp_JSONStr = JSON.stringify(Ext.getCmp('chkboxgrp2').getValue()["rb"]);
                        np["ASN_ARY_JSONStr"]=Tmp_JSONStr;

                  2> 後端接收時,必需先將 JSON string 型態, 再還回原型態(Array/table/Object)
                        *.cs
                        string Tmp_ASN_ARY_JSONStr = nvc["ASN_ARY_JSONStr"];  
                        JArray Tmp_ASN_ARY =      
                     (JArray)JsonConvert.DeserializeObject(Tmp_ASN_ARY_JSONStr, (typeof(JArray)));  


1>*.js

var selectedValues = Ext.getCmp('chkboxgrp2').items;
console.log(selectedValues);
var Tmp_ASNAry = []; //var Tmp_Obj={};
//chkboxgrp2 均設為 true
for (var i = 0; i < selectedValues.length; i++) {
    selectedValues.items[i].setValue(true);
};

var np = {};
//取得 chkboxgrp2 的勾選欄位值
//將 Array 轉成 JSON字串, 以便傳至後端
    var Tmp_JSONStr = JSON.stringify(Ext.getCmp('chkboxgrp2').getValue()["rb"]);
    np["ASN_ARY_JSONStr"]=Tmp_JSONStr;
    console.log("np['ASN_ARY_JSONStr']: ", np["ASN_ARY_JSONStr"]);

2>*.cs    
  string Tmp_ASN_ARY_JSONStr = nvc["ASN_ARY_JSONStr"]; 
  JArray Tmp_ASN_ARY = (JArray)JsonConvert.DeserializeObject(Tmp_ASN_ARY_JSONStr, (typeof(JArray)));
 string Tmp_ASN_ARY_STR = string.Join(",", Tmp_ASN_ARY);
// "A012,B025" -->  'A012','B025'
 Tmp_ASN_ARY_STR = myfunc.AddQuoteStr(Tmp_ASN_ARY_STR); 








PS:
       1>將checkboxgroup2 的項目均設為 勾選(checked)
                 {
                    xtype: 'checkboxgroup',
                    id: 'chkboxgrp1',            // columns: 3,
                    columns: [.35, .35, .35],
                    items: [
                               { boxLabel: 'Item 1', name: 'rb', inputValue: '1' },
                               { boxLabel: 'Item 2', name: 'rb', inputValue: '2' },
                               { boxLabel: 'Item 3', name: 'rb', inputValue: '3' },
                             ]
                  },



}, //end of 待挑選機號

                  2>將checkboxgroup2的項目傳送至後端處理                       



2022年10月31日 星期一

Array 及 Object 的宣告, 及新增元素, 取得元素值處理

 目的: Array 及  Object 的宣告, 及新增元素 取值處理

處理說明:  1>Array  及 Object 的宣告
                       var Tmp_Ary=[];      //宣告 Array   
                       var Tmp_Obj={};     //宣告 Object

                   2>Array 及 Object 設定值
                       var Tmp_len=0;
                       for (var i=1; i<=5;i++)
                       { 
                           Tmp_len=Tmp_Ary.push(i) //傳回目前 Tmp_Ary.length
                           //Tmp_Ary[Tmp_len-1]=整數,並非Object ,所以無法設定property(newSet)
                           Tmp_Ary[Tmp_len - 1].newSet = i;    //undefined
                        } 
                       
                        Tmp_Obj = {
                             name: "蔡聰進",
                             gender: "男",
                        };             
                        Tmp_Obj.newSet=1;  //新增  Object 的屬性,直接 Assing 即可 

                        Tmp_len=Tmp_Ary.push(Tmp_Obj) ;
                        //Tmp_Ary[Tmp_len-1] 為 Object ,所以可以設定 property(newSet2) , 取值也會正常
                        Tmp_Ary[Tmp_len-1].newSet2=2;  

                   3>Array 及 Object 取值 
                       var Tmp_Ary=[1,2,3,4,5] ;
                       var  Tmp_Obj={
                               name: "蔡聰進",
                               gender: "男",
                               newSet: 1,
                               }
                      var Tmp_Str="";
                       //取得  Array 元素值_1  for 
                      for (var i=0; i<=Tmp_Ary.length-1;i++)
                     { 
                         Tmp_Str="Tmp_Ary[i]="+Tmp_Ary[i];
                        console.log(Tmp_Str) ;
                      }
                      //取得  Object property值  for 
                     for (var prop in Tmp_Obj)
                     {
                        Tmp_Str="Tmp_Obj["+prop +"]="+Tmp_Obj[prop];
                        console.log(Tmp_Str) ;
                      }

                      //取得  Array元素值_2  Ext.each
                      Ext.each(Tmp_Ary, functon(item,index){
                        Tmp_Str="Tmp_Ary["+index+"]="+item;
                        console.log(Tmp_Str) ;
                      });


實際案例:
                    var Tmp_Ary = [];
                    var Tmp_Obj = {};
                    var cur_recs = Ext.getCmp('grid_Single').getView().getSelectionModel().getSelection();
                    var cur_rec;
                    if (cur_recs.length > 0) {
                        cur_rec = cur_recs[0];
                        Tmp_Ary.push(cur_rec);
                        cur_rec.newSet = 0;
                    }
                    console.log("cur_rec.newSet:", cur_rec.newSet);
            console.log("Tmp_Ary[Tmp_Ary.length-1].newSet:", Tmp_Ary[Tmp_Ary.length - 1].newSet);
                    console.log("0 Tmp_Ary.length:", Tmp_Ary.length);
                    //mysuccessalert("cur_rec.newSet:" + cur_rec.newSet.toString());                    
                    var Tmp_len;
                    var Tmp_Str = "";

                   //0.5
                    for (var i = 1; i <= 5; i++) {                     
                        Tmp_len = Tmp_Ary.push(i);
                        console.log("0.5 Tmp_len:", Tmp_len);
                        Tmp_Ary[Tmp_len - 1].newSet = i;
                        //因 Tmp_Ary[Tmp_len - 1]=整數,非 Object , 所以,newSet undefined
                        console.log("0.5  Tmp_Ary[Tmp_len - 1].newSet:", Tmp_Ary[Tmp_len - 1].newSet);
                    }
                    for (var i = 1; i <= 5; i++) {
                        var Tmp_Obj1 = {};
                        Tmp_Obj1.value = i;
                        Tmp_len = Tmp_Ary.push(Tmp_Obj1);
                        console.log("1 Tmp_len:", Tmp_len);
                        //因 Tmp_Ary[Tmp_len - 1]= Object , 所以,newSet 可存值
                        Tmp_Ary[Tmp_len - 1].newSet = i;    
                        console.log("1  Tmp_Ary[Tmp_len - 1].newSet:", Tmp_Ary[Tmp_len - 1].newSet);
                    }
                    console.log("1 Tmp_Ary.length:", Tmp_Ary.length);
     
                    //1.5
                    //item 為 Array 元素
                    Ext.each(Tmp_Ary,
                        function (item, index) {
                            console.log("1.5 index:", index);
                            Tmp_Str = " 1.5  Tmp_Ary[index]=" + Tmp_Ary[index];
                            console.log(Tmp_Str);
                            Tmp_Str = " 1.5  Tmp_Ary[index].newSet=" + Tmp_Ary[index].newSet;
                            console.log(Tmp_Str);
                    )  
 
                    //1.8     
                    for (var index in Tmp_Ary) {
                        Tmp_Str = " 1.8  index=" + index;
                        console.log(Tmp_Str);
                        Tmp_Str = " 1.8  Tmp_Ary[index].newSet=" + Tmp_Ary[index].newSet;
                        console.log(Tmp_Str);
                    }
                    
                    //1.9  
                    Tmp_Str = " 1.9  Tmp_Ary.length=" + Tmp_Ary.length;
                    console.log(Tmp_Str);                     
                    for (var index = 0; index < Tmp_Ary.length ; index++) {
                        Tmp_Str = " 1.9  index: " + index;
                        console.log(Tmp_Str);
                        Tmp_Str = " 1.9  Tmp_Ary[index].newSet=" + Tmp_Ary[index].newSet;
                        console.log(Tmp_Str);
                    }
                    Tmp_Obj = {
                        name: "蔡聰進",
                        gender: "男",
                    };                 
                    Tmp_Obj.newSet = 1;

                    //2  
                    for (var item in Tmp_Obj) {
                        Tmp_Str = "2 Tmp_Obj[" + item + "] is " + Tmp_Obj[item];
                        console.log(Tmp_Str);
                    }

輸出結果:
0 Tmp_Ary.length: 1
0.5 Tmp_len: 2
0.5  Tmp_Ary[Tmp_len - 1].newSet: undefined
0.5 Tmp_len: 3
0.5  Tmp_Ary[Tmp_len - 1].newSet: undefined
0.5 Tmp_len: 4
0.5 Tmp_Ary[Tmp_len - 1].newSet: undefined
0.5 Tmp_len: 5
0.5  Tmp_Ary[Tmp_len - 1].newSet: undefined
0.5 Tmp_len: 6
0.5  Tmp_Ary[Tmp_len - 1].newSet: undefined

1 Tmp_len: 7
1  Tmp_Ary[Tmp_len - 1].newSet: 1
1 Tmp_len: 8
1  Tmp_Ary[Tmp_len - 1].newSet: 2
1 Tmp_len: 9
1  Tmp_Ary[Tmp_len - 1].newSet: 3
1 Tmp_len: 10
1  Tmp_Ary[Tmp_len - 1].newSet: 4
1 Tmp_len: 11
1  Tmp_Ary[Tmp_len - 1].newSet: 5
1 Tmp_Ary.length: 11

1.5 index: 0
1.5  Tmp_Ary[index]=[object Object]
1.5  Tmp_Ary[index].newSet=0
1.5 index: 1
1.5  Tmp_Ary[index]=1
1.5  Tmp_Ary[index].newSet=undefined
1.5 index: 2
1.5  Tmp_Ary[index]=2
1.5  Tmp_Ary[index].newSet=undefined
1.5 index: 3
1.5  Tmp_Ary[index]=3
1.5  Tmp_Ary[index].newSet=undefined
1.5 index: 4
1.5  Tmp_Ary[index]=4
1.5  Tmp_Ary[index].newSet=undefined
1.5 index: 5
1.5  Tmp_Ary[index]=5
1.5  Tmp_Ary[index].newSet=undefined
1.5 index: 6
1.5  Tmp_Ary[index]=[object Object]
1.5  Tmp_Ary[index].newSet=1
1.5 index: 7
1.5  Tmp_Ary[index]=[object Object]
1.5  Tmp_Ary[index].newSet=2
1.5 index: 8
1.5  Tmp_Ary[index]=[object Object]
1.5  Tmp_Ary[index].newSet=3
1.5 index: 9
1.5  Tmp_Ary[index]=[object Object]
1.5  Tmp_Ary[index].newSet=4
1.5 index: 10
1.5  Tmp_Ary[index]=[object Object]
1.5  Tmp_Ary[index].newSet=5

1.8  index=0
1.8  Tmp_Ary[index].newSet=0
1.8  index=1
1.8  Tmp_Ary[index].newSet=undefined
1.8  index=2
1.8  Tmp_Ary[index].newSet=undefined
1.8  index=3
1.8  Tmp_Ary[index].newSet=undefined
1.8  index=4
1.8  Tmp_Ary[index].newSet=undefined
1.8  index=5
1.8  Tmp_Ary[index].newSet=undefined
1.8  index=6
1.8  Tmp_Ary[index].newSet=1
1.8  index=7
1.8  Tmp_Ary[index].newSet=2
1.8  index=8
1.8  Tmp_Ary[index].newSet=3
1.8  index=9
1.8  Tmp_Ary[index].newSet=4
1.8  index=10
1.8  Tmp_Ary[index].newSet=5


1.9  Tmp_Ary.length=11
1.9  index: 0
1.9  Tmp_Ary[index].newSet=0
1.9  index: 1
1.9  Tmp_Ary[index].newSet=undefined
1.9  index: 2
1.9  Tmp_Ary[index].newSet=undefined
1.9  index: 3
1.9  Tmp_Ary[index].newSet=undefined
1.9  index: 4
1.9  Tmp_Ary[index].newSet=undefined
1.9  index: 5
1.9  Tmp_Ary[index].newSet=undefined
1.9  index: 6
1.9  Tmp_Ary[index].newSet=1
1.9  index: 7
1.9  Tmp_Ary[index].newSet=2
1.9  index: 8
1.9  Tmp_Ary[index].newSet=3
1.9  index: 9
1.9  Tmp_Ary[index].newSet=4
1.9  index: 10
1.9  Tmp_Ary[index].newSet=5

2 Tmp_Obj[name] is 蔡聰進
2 Tmp_Obj[gender] is 男
2 Tmp_Obj[newSet] is 1

2022年9月6日 星期二

Array 處理 - Array宣告 - 判斷元素是否在 Array內 - c# 判斷字串是否在長字串內

 目的: Array 的宣告 & 判斷 元素是否在 Array內

處理說明: 1>宣告 Array  :    
                        Opts1> let  Tmp_Ary=["A","B","C"];   //宣告 Tmp_Ary 共 3元素("A","B","C")
                        Opts2> let  Tmp_Ary= new Array(3);  //宣告 Tmp_Ary 共 3元素
                  2>判斷元素("A")是否在 Array內 , 利用 Array.indexOf("A") , 傳回 index 
                       if (Tmp_Ary.indexOf("A")== -1)
                  3>c# 字串判斷是否存在 , 若 Tmp_IRESAK 為"", 則傳回 0 , 非 -1
                        if ("1;2;3;4;5;6;7;8;".IndexOf(Tmp_IRESAK) == -1)
                            {
                                Tmp_RtnMsg1 = Tmp_RtnMsg1 + "[檢測結果代碼]欄位值不符!!"
                                                          +"(" +Tmp_IRESAK + ")<br>";
                            }
                        --> 所以必需先判斷 Tmp_IRESAK 是否為空字串
                               if (myfunc.checkisnull(Tmp_IRESAK.Trim()))
                                {
                                  Tmp_RtnMsg1 = Tmp_RtnMsg1 + "[檢測結果代碼]欄位值不可空白!!<br>";
                                  }

 PS: *.js  利用 "A" in Tmp_Ary 不準,  不用之   , 

        --> 但 *.cs   key in Keys 可


1>*.js  var  Tmp_Ary=["A","B","C"];

  let Tmp_Ary = ["A", "B", "C", "D"];

            console.log("1 par_WTLV in [A,B, C,D] :", ("A" in Tmp_Ary));            

            console.log("3 Tmp_Ary.indexOf(par_WTLV) :", Tmp_Ary.indexOf(par_WTLV));

            //用  Arrary 用  in  不準, 改用 indexOf

            if (Tmp_Ary.indexOf(par_WTLV)==-1)

                return;


2>*.cs  string[] Tmp_StrAry= new string[3] {"A","B","C"}

             string[]  Tmp_StrAry=new string[3] {"MITM","WITM","DDITM"};

             int[]   Tmp_intAry= new int[3]{21,22,25};

            Tmp_intAry[2]=23;

            int Tmp_int2=Tmp_intAry[2];


             Tmp_StrAry[0].Contains("A");

             Tmp_StrAry.Contains(obj.Index);


3>Array 也是 Object 的型態之一

    Ex: var Tmp_Ary=[];

           Tmp_Ary.push(cur_rec);   //method

           Tmp_Ary.length=1;          //property  

           Tmp_Ary.newSet=-1;   //宣告新的 property





2022年8月31日 星期三

JS Array 陣列宣告 - push,pop - unshift,shift

目的: 說明 陣列 (Array) 的宣告及使用

 陣列可以看作是一種特別的「物件」,同樣是零至多個元素的集合

陣列內可以是原始的資料類型、其他陣列、函式等等

陣列是個有順序性的集合,且只能透過 [] 來存取。

陣列的索引是由 0 開始計算的

一.陣列宣告的3個方法:

1>方法1:  new Array();

var a = new Array(); 
a[0] = "apple"; 
a[1] = "boy"; 
a[2] = "cat"; 
a.length; // 3

2>方法2:   []
var a = []; 
a[0] = "apple"; 
a[1] = "boy"; 
a[2] = "cat"; 
a.length; // 3

3>方法3:   ["apple","boy","cat"];
var a = ["apple", "boy", "cat"]; 
a.length; //3


二.陣列的新增/刪除

1>Array.legth 可以修改
var a = ["apple", "boy", "cat"]; 
a.length; // 3 
a.length = 1; 
console.log(a); // ["apple"] 
a.length = 3; 
console.log(a); // ["apple", undefined, undefined]

2>Array元素可以任意指定
var array = ['a', 'b', 'c']; 
array.length; // 3 
array[7] = 'z'; 
console.log(array); // ["a", "b", "c", undefined, undefined, undefined, undefined, "z"]

3>在陣列末端新增/移除元素時,可以透過 ARRAY.push() ; Array.pop()
var array = ['a', 'b', 'c']; 
array.length; // 3 
array.push('d'); 
console.log(array); // ['a', 'b', 'c', 'd']
array.pop();            //傳回 "d"
console.log(array); // ['a', 'b', 'c']

4>在陣列前端新增/移除元素時,可以透過 ARRAY.unshift() ; Array.shift()
var array=['a', 'b', 'c'];
var Tmp_el=array.shift();
console.log(array); // ['b','c']
Tmp_el=array.shift();
console.log(array); // ['c']
Tmp_el=array.unshift(4,"e");
console.log(array); // [4,"e",'c']

JS - - Object,Array的宣告 , Object 基本型別 及 物件型別 - Object 的建立 - 屬性的存取 - typeof 型別的判斷 - == ===


JavaScript 內建的型別主要可以分成基本型別 (Primitives) 與物件型別 (Object) 兩大類。 
基本型別又分成 string、number、boolean、null、undefined 幾種,
除了以上幾種之外,其他都可以歸類至物件型別 (Object)

一.物件的定義 - Object :
「An object is a collection of properties and has a single prototype object.」
一個物件可以是個零至多種屬性的集合,而屬性是鍵 (key) 與值 (value) 之間的關聯。 
一個屬性的「值」可以是某個基本型別,也可以是另一個物件,甚至可以是一個函數。
物件可以是瀏覽器預先定義好的,當然也可以是由自己定義物件的屬性與內容

1>Object 的宣告方式:
var person={}                                 //或是  var person=new Object();
person["NAME"]="蔡聰進";
person["GENDER"]="male";

2>Array的宣告方式:
var tempData=[];
tempData.push(rec);


1>建立 Object 的2種方法如下:
方法1> new Object() 方式,建立物件 , 再設定物件屬性
var person = new Object(); 
person.name = 'Kuro'; 
person.job = 'Front-end developer'; 
person.sayName = function() { alert( this.name ); };

方法2>  JSON 方式 , 建立物件
var person = { 
       name: 'Kuro', 
       job: 'Front-end developer', 
       sayName: function() { alert( this.name ); } 
};

 -->
直接用大括號 { },即可建立起一個新的物件,並且在建立物件的同時直接指定屬性至該物件內。
這種建立物件的方式稱為「物件實字 (Object literal)」,同時也是 JSON 格式的核心語法。



二.屬性的存取 , 建議用   Object["Name"]
存取屬性 的2種方法如下:
var person = { 
      name: 'Kuro', 
      job: 'Front-end developer', 
      sayName: function() { alert( this.name ); } ,
      "001" : "Hello",
}; 
1>由.Name存取
person.name;                   // 'Kuro' 
person.sayName();         // ƒ sayName()
person.001;                   // SyntaxError: Unexpected number 

2>由 [Name]存取
person["name"];             // 'Kuro' 
person["sayName"];      // ƒ sayName()
person["001"]              // Hello 

--> 屬性的新增及刪除
person["age"]=50;
console.log("person[age]]",person["age"]);  //50
delete person["age"];
console.log("person[age]]",person["age"]);  //undefined

--> 判斷屬性是否存在
方法1:  ==undefined
if (person["age"]!=undefined){
    console.log("property age exist");
}

方法2:  (age in person)  , true/false
if (age in person){
    console.log("property age exist");
}

方法3: person.hasOwnProperty('age')
if (person.hasOwnProperty('age')) {
    console.log("property age exist");
}

-->
if (person["name"]!="undefined"){
    console.log("1 property name exist");
};
if ("name" in person){
    console.log("2 property name exist");
};
     if (person.hasOwnProperty('name')) {
    console.log("3 property name exist");
};


三.型別的判斷 - typeof  -  typeof 運算子回傳的東西都是「字串」

typeof true; // 'boolean' 
typeof 'Kuro'; // 'string' 
typeof 123; // 'number' 
typeof NaN; // 'number' 
typeof { }; // 'object' 
typeof [ ]; // 'object' Array
typeof undefined; // 'undefined' 
typeof window.alert; // 'function' 
typeof null; // 'object'


--> typeof   Array   --> 得到 "object" , 非 array
      由 Array.isarray([]) 判斷  //true
       Array.isarray( [4,"a","b","c"] );  //true
       Array.isarray("abc");   //false


四.==   ===  比較
var a = 10; 
var b = "10"; 
console.log( a == b ); // true 
console.log( a === b ); // false

console.log(null == undefined) ; //true
console.log(null === undefined) ; //false

-->
在 JavaScript 這門程式語言中,大家會提倡盡量使用 === 來取代 == 的原因。
-->
!= 的版本會做自動轉型,而 !== 則不會做自動轉型。

2022年8月23日 星期二

V120402 - Array 的宣告 & 指派給變數

 目的:  Array 宣告  & 指定給變數

處理說明:  1>當您將新的陣列指派給這個變數時必須使用 new 運算子

                 2> Array的元素,必需以 {   }  , 非  [ ] 

  1>int[] array1 = new int[] { 1, 3, 5, 7, 9 };

2>string[] weekDays = new string[] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };


Ex1: V120402 , 動態指定 Array 變數

 Tmp_Sql = " SELECT   EMPLYID  as BRP, EMPLYNM,DEPID  "

                            + "  FROM      HR_EMPLYM ";

getKeyCode("BRP", ref ds, Tmp_Sql, "", "BRP_;BRP__", new int[] { 1,2 });

V120402 - - 取得 CaluField - keycode - 取得借閱人姓名 & 借閱人單位 - rename

 目的: 取得 CaluField 欄位值, 自訂 CaluField欄位名稱 - getKeyCode

處理說明: 1>getKeyCode  - 自訂 CaluField欄位名稱,及傳回欄位值 index


1>*.cs

//新增  CaluField 欄位  - [借閱人姓名][借閱人單位]

 Tmp_Sql = " SELECT   EMPLYID  as BRP, EMPLYNM,DEPID  "

                            + "  FROM      HR_EMPLYM ";

getKeyCode("BRP", ref ds, Tmp_Sql, "", "BRP_;BRP__", new int[] { 1,2 });


  //新增  CaluField 欄位  - [管制現況]            

            getKeyCode("STAT", ref ds);