[ Nexacro.Grid -  copy & paste ]

 

/*==+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 * Group : 10. Grid 영역 copy & paste

 **=++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/


/******************************************************************************************
 * 기능 : 그리드 Ctrl + C 입력시 그리시 Cell Text 복사
 * 인자 : obj:Grid
 *         e:KeyEventInfo
 * 리턴 :
 * 예문 :
 *****************************************************************************************/

function gfn_CopyGridCellText(obj:Grid, e:KeyEventInfo)

{

    if( e.ctrlKey && e.keycode == 67)

    {

        var selectedValue = obj.getCellText(obj.currentrow, obj.currentcell);

        // ClipBoard 초기화

        system.clearClipboard();

        //선택값 복사.

        system.setClipboard("CF_TEXT", selectedValue);

    }

}

/*++
개요: Grid에서 copy,paset,del 이 작동하게 한다
@name    esm_copy.xjs
@author  허원무
@since   2012.01.10
@remark  복사(ctrl+C) 붙여넣기(ctrl+V) 가능.
@remark  1) 한 cell복사해서 -> 다중셀 선택후 붙여넣기 가능
@remark  2) 다중 cell복사해서 -> 한cell선택후 붙여넣기 : 구역이 붙여넣기됨
@remark  3) 다중 cell선택후 Del키 -> 일괄삭제됨
*/
 

var GRID_VAL_DIV = "\t";

var CLIPBOARD_TYPE = "CF_UNICODETEXT"; // 클립보드에 저장시 데이터 타입

 

/**************************************************************************/
//  ESEST4120XM 복사, 붙여넣기 참조
//
//  copy and paste 사용시 필수 항목..
//  grid selecttype = area
//  grid 에 Dataset이 bind되있어야 함..
//
//  esm_fn_GridPasteVal 사용시  
//  마지막 파라메터로 값 체크할 함수명("func_test" X, func_test O)
//  함수 파라메터 1. grid에 bind된 Dataset
//                  2. Dataset columnID
//                  3. Dataset row index
//                  4. 복사된 값
/**************************************************************************/
 

/******************************************************************************/
/**   popup 메뉴 동적 생성       
 * ★ Function    : esm_fn_SetPopUpMenu
 * ★ 설명        : 그리드이 팝덥메뉴를 동적으로 생성한다.
 * ★ Parameter   : dsObj (바인드된 dataset)
 *                  e GridMouseEventInfo
                    clickEvent 메뉴를 눌렀을때 발생시킬 함수
/******************************************************************************/

function esm_fn_SetPopUpMenu(dsObj:Dataset, e:GridMouseEventInfo, clickEvent:Function)

{

    // 기존에 GridPopMenu id로 등록된 값이 있을 경우 remove 시킨다.

    if(this.all["esmPopMenu"]) {

        this.removeChild("esmPopMenu");

    }

    

    // 팝업메뉴 객체 생성 및 초기화(id, size)

    var popupMenu = new PopupMenu("esmPopMenu", 0, 0 ,0 ,0);

 

    popupMenu.innerdataset = dsObj;

    popupMenu.captioncolumn = "name";

    popupMenu.idcolumn = "id";

    popupMenu.levelcolumn = "lev";

    popupMenu.enablecolumn = "enable";

 

    this.addChild("esmPopMenu", popupMenu);

 

    popupMenu.onmenuclick.setHandler(clickEvent);

    popupMenu.show();

    popupMenu.trackPopup(e.screenX , e.screenY);

}

 

function esm_fn_GridCopyKeyEvent(gridObj:Grid, e:KeyEventInfo, enterFlag){

    var reBool = false;

    enterFlag = gfn_IsNull(enterFlag) ? "key" : enterFlag;

 

    if(e.keycode == 46 || (e.ctrlKey && (e.keycode == "67" || e.keycode == "86")))

    { gridObj.autoenter = "none"; }

    

    if(e.keycode == 46){

        reBool = true;

        esm_fn_DelGridArea(gridObj);

    }

    

    if(e.ctrlKey) {

        if(e.keycode == 67) {

            reBool = true;

            esm_fn_GridCopyVal(gridObj);

        }else if(e.keycode == 86){

            reBool = true;

            esm_fn_GridPasteVal(gridObj);   

        }

    }

 

    gridObj.autoenter = enterFlag;

    return reBool;

}

 

function esm_fn_DelGridArea(gridObj:Grid){

    if(gridObj.selecttype == "row"){ return; }

    var colId;

    var editType;

    var objDs = eval(gridObj.binddataset);

    

    var sRow = toNumber(gridObj.selectstartrow);

    var eRow = toNumber(gridObj.selectendrow);

    var sCol = toNumber(gridObj.selectstartcol);

    var eCol = toNumber(gridObj.selectendcol);

    

    for (var i = sRow; i <= eRow; i++)

    {

        for (var z = sCol; z <= eCol; z++)

        {

            /* ------------------------------------------------------

                삭제 항목에서 제외되는 경우

                    1) edittype이 null, none일 경우

                    2) 컬럼이 숨겨져 있을 경우(size <= 0 )

                    3) 공종(Title)

                    4) 표준내역중  내역명, 규격명, 단위명

            --------------------------------------------------------- */            

            editType = gridObj.getCellProperty("Body", z, "edittype");

            

            if(gfn_IsNull(editType) || editType == "none")                        { continue; }

          //if(!gfn_IsNull(objDs.getColumn(i, "title")))                          { continue; } 

            if(toNumber(gridObj.getFormatColProperty(z, "size")) <= 0)            { continue; }

            

            colId    = gridObj.getCellProperty("Body", z, "text").replace("bind:", "");

            

            if(!gfn_IsNull(objDs.getColumn(i, "brkdnCd"))){

                if(gfn_IsNull(colId))                                             { continue; }

                if(colId == "brkdnNm" || colId == "normNm" || colId == "unitNm")  { continue; }

            }

            

            objDs.setColumn(i, colId, "");

        }

    }

}

 

function fn_GetSelectCk(sRow, eRow, sCol, eCol){

    if(sRow <= -1 || eRow <= -1 || sCol <= -1 || eCol <= -1){ return false; }

    return true;

}

 

/******************************************************************************/
/**   grid value copy         
 * ★ Function    : esm_fn_GridCopyVal
 * ★ 설명        : 그리드에서 영역을 선택하여 복사한다.
 * ★ Parameter   : gridObj (그리드)
 *                  dstObj (바인드된 dataset)                    
/******************************************************************************/

function esm_fn_GridCopyVal(gridObj:Grid){

    if(gridObj.selecttype != "area") return;    

    // 임시 데이터셋에 넣을 값을 포멧형식에 맞쳐서 넣는 변수...

    var sVal;

    var result = "";   

    var sRow = toNumber(gridObj.selectstartrow);

    var eRow = toNumber(gridObj.selectendrow);

    var sCol = toNumber(gridObj.selectstartcol);

    var eCol = toNumber(gridObj.selectendcol);

    

    if(!fn_GetSelectCk(sRow, eRow, sCol, eCol)) return false;

    

    for (var i = sRow; i<=eRow; i++) {

        for (var z = sCol; z<=eCol; z++) {

            // 항목 보기 이벤트가발생할 경우 건너띄워야 한다.

            if(toNumber(gridObj.getFormatColProperty(z, "size")) > 0){

                if(gridObj.getCellProperty("Body", z, "edittype") == "combo")

                { result += esm_fn_GetDataCode("date", gridObj, z, gridObj.getCellValue(i,z)); }

                

                else

                { result += gridObj.getCellValue(i,z); }                

                

                if(z < gridObj.selectendcol) result += GRID_VAL_DIV;

            }

        }

        if (i < gridObj.selectendrow) {

            result += "\r\n";

        }

    }

    result += "\r\n";

    

    //20120927권창진(past시 누락되서 삭제) result = result.trimRight("\r\n");

    

    // 복사 전 이전에 사용된 clipboard 내용을 지운다.

    system.clearClipboard();

    //CF_CSV 타입으로 저장한다.

    system.setClipboard(CLIPBOARD_TYPE, result);

}

 

/******************************************************************************/
/**   grid value paste         
 * ★ Function    : esm_fn_tempDatasetCreate
 * ★ 설명        : 복사된 값을 임시 데이터셋에 저장한다.
/******************************************************************************/

function esm_fn_tempDatasetCreate(){

    // 임시 데이터 셋을 생성한다.

    var dsObj = new Dataset();

    

    //임시 데이터셋 삭제 후 추가

    if(this.all["ds_tempHWM"])

    { this.removeChild("ds_tempHWM"); }

    this.addChild("ds_tempHWM", dsObj);

 

    var sVal;

    var arrCol;

    var nRow = 0;

    var copyVal = system.getClipboard(CLIPBOARD_TYPE);

    

    if(copyVal == null)

    { return false; }

    

    copyVal = new String(copyVal);

    copyVal = copyVal.split("\r\n");

    

    var max = (copyVal.length == 1) ? copyVal.length : copyVal.length - 1;

 

    // 한줄이 하나의 row가 되므로 \n를 기준으로 row를 나눈다.

    for(var i = 0; i < max; i ++){

        // ','를 기점으로 column(temp_col + index)를 생성하고 값을 저장한다.

        arrCol = copyVal[i].split(GRID_VAL_DIV);

        for(var z = 0; z < arrCol.length; z++){

            // 컬럼 추가는 첫번째 row가 생성될 때에만 생성된다.

            if(i == 0){ dsObj.addColumn("tempCol" + eval(z),"String"); }

            

            // row는 한줄이 생성될 때에만 생성된다.

            // 마지막 컬럼값에 스페이스 영역이 들어와(test결과) trim를 사용

            if(z == 0){ nRow = dsObj.addRow(); }

 

            arrCol[z] = arrCol[z].trim();

            arrCol[z] = (arrCol[z] == "undefined") ? "" : arrCol[z];

            arrCol[z] = arrCol[z].replace("\"", "");

 

            dsObj.setColumn(nRow, "tempCol" + eval(z), arrCol[z]);

        }

    }

    ds_tempHWM.name = "tempDS";

    //trace(dsObj.saveXML());

    return true;

}

 

/******************************************************************************/
/**   grid value paste         
 * ★ Function    : esm_fn_GridPasteVal
 * ★ 설명        : 복사된 값을 붙여넣는다.
 * ★ Parameter   : gridObj (그리드)
 *                  dstObj (바인드된 dataset)      
                    setPasteFunc (붙여넣기 할 함수 - 개발자 부분)
/******************************************************************************/

function esm_fn_GridPasteVal(gridObj:Grid, preFunc){

    if(gridObj.selecttype != "area") return;

    // 임시 테이타셋에 저장한다.

    if(!esm_fn_tempDatasetCreate()) return false;

    

    var dsObj = gridObj.binddataset;

        dsObj = gfn_IsNull(dsObj) ? "noDataset" : eval(dsObj);

    

    if(dsObj == "noDataset"){

        if (gsLocale == LANG_MODE_KR) {    // 한글모드

            gfn_Alert("그리드의 Bind된 Dataset이 존재 하지 않습니다.");

        } else {

            gfn_Alert("There is not a dataset to be bound to the grid.");

        }

        

        return false;

    };

    

    // 임시 테이타셋을 변수에 넣고 정보를 삭제한다.

    var tempDS = ds_tempHWM;

    if(this.all["ds_tempHWM"]){

        this.removeChild("ds_tempHWM");

    }

 

    var flag;

    var arrCol;

    var colInfo;

    var sRow = toNumber(gridObj.selectstartrow);

    var eRow = toNumber(gridObj.selectendrow);

    var sCol = toNumber(gridObj.selectstartcol);

    var eCol = toNumber(gridObj.selectendcol);

    

    if(!fn_GetSelectCk(sRow, eRow, sCol, eCol)) return false;

        

    if(tempDS.getRowCount() == 1 && tempDS.getColCount() == 1){

        flag = "oneRow";

        colInfo = esm_fn_GetArrColID(gridObj, sCol, eCol, flag);

    }

    else if(tempDS.getRowCount() >= 1){

        flag = "moreRow";

        colInfo = esm_fn_GetArrColID(gridObj, sCol, tempDS.getColCount(), flag);

    }

 

    arrCol      = colInfo.getColId;

    arrColIndex = colInfo.getColIndex;

 

    if(flag == "moreRow")

    {

        for(var i = 0; i < tempDS.getRowCount(); i++){

            for(var z = 0; z < arrCol.length; z++){

                if(arrCol[z] == "_noCol") continue;

                if(!gfn_IsNull(preFunc))

                { if(!eval(preFunc(dsObj, arrCol[z], sRow, tempDS.getColumn(i, z)))) continue; }

                

                esm_fn_SetPasteData(gridObj, tempDS, dsObj, arrCol[z], arrColIndex[z], i, z, sRow);

            }

            sRow++;

        }

    }

    else

    {

        while(true){

            if(sRow > eRow) break;

            for(var i = 0; i < arrCol.length; i++){

            

                if(arrCol[i] == "_noCol") continue;

                if(!gfn_IsNull(preFunc))

                { if(!eval(preFunc(dsObj, arrCol[i], sRow, tempDS.getColumn(0, 0)))) continue; }

                

                esm_fn_SetPasteData(gridObj, tempDS, dsObj, arrCol[i], arrColIndex[i], 0, 0, sRow);

                

            }

            sRow++;

        }

    }

}

 

function esm_fn_SetNumberVal(nVal){

    var sCharAt;

    var resultVal = "";

 

    for(var i = 0; i < nVal.length; i++){

        sCharAt = toNumber( gfn_CharAt(nVal, i) );

 

        if(gfn_IsNumber(sCharAt) && !isNaN(sCharAt))

        { resultVal += sCharAt; }

    }

    

    return resultVal;

}

 

function esm_fn_SetPasteData(gridObj, tempDS, dsObj, colId, colIndex, tempDsRow, tempDsCol, nRow, endFlag){

    var nVal = tempDS.getColumn(tempDsRow, tempDsCol);

    

    if(dsObj.getColumnInfo(colId).type != "STRING"){

        nVal = nVal.replace(",", "");

 

        if(!gfn_IsNumber(nVal))

        { nVal = esm_fn_SetNumberVal(nVal); }

 

        tempDS.setColumn(tempDsRow, tempDsCol, nVal);

    }

    

    if(gridObj.getCellProperty("Body", colIndex, "edittype") == "combo"){

        nVal = tempDS.getColumn(tempDsRow, tempDsCol);

        nVal = esm_fn_GetDataCode("code", gridObj, colIndex, nVal);

        tempDS.setColumn(tempDsRow, tempDsCol, nVal);

    }

    

    dsObj.rowposition = nRow;

    dsObj.setColumn(nRow, colId, tempDS.getColumn(tempDsRow, tempDsCol));

    

    if(gridObj.getCellProperty("Body", colIndex, "edittype") == "combo"){

        nVal = tempDS.getColumn(tempDsRow, tempDsCol);

        nVal = esm_fn_GetDataCode("data", gridObj, colIndex, nVal);

        tempDS.setColumn(tempDsRow, tempDsCol, nVal);

    }   

}

 

function esm_fn_GetDataCode(flag, grdiObj:Grid, colIndex, sData){

    var rtnVal;

    var vDs  = eval(grdiObj.getCellProperty("Body", colIndex, "combodataset"));

    var code = grdiObj.getCellProperty("Body", colIndex, "combocodecol");

    var data = grdiObj.getCellProperty("Body", colIndex, "combodatacol");

    

    if(gfn_IsNull(vDs) || gfn_IsNull(code) || gfn_IsNull(data)) return null;

    

    if(flag == "code")

    { rtnVal = vDs.lookup(data, sData, code); }

    else

    { rtnVal = vDs.lookup(code, sData, data); }

    

    return gfn_IsNull(rtnVal) ? null : rtnVal;

}

 

//시작 컬럼부터 Dataset의 컬럼명을 가져 온다.

function esm_fn_GetArrColID(grdObj:Grid, nStartCol, nTotCol, flag)

{

    var totCol;

    var index = 0;  

    var colInfo = new Object();

    var result  = new Array();

    var result2 = new Array();

 

    if(flag == "moreRow") totCol = nTotCol;

    else{ totCol = nTotCol - nStartCol + 1; }                 

 

    for(var i = 0; i < totCol; i++){

        if(grdObj.getFormatColProperty(nStartCol, "size") > 0){

            if( grdObj.getCellProperty("Body", nStartCol, "edittype") == "none"

                || gfn_IsNull(grdObj.getCellProperty("Body", nStartCol, "edittype")))

            { result[index] = "_noCol"; }

            else

            { result[index] = grdObj.getCellProperty("Body", nStartCol, "text").replace("bind:", ""); }

            

            result2[index] = nStartCol;

            

            index++;

        }else{ i--; }

        nStartCol++;

        if(nStartCol >= grdObj.getFormatColCount()){ break; }

    }

    

    colInfo.getColId = result;

    colInfo.getColIndex = result2;

    

    return colInfo;

}

 

/******************************************************************************
 * ★ Function명 : esm_fn_grdCopyPaste
 * ★ 설명       : Grid에서 클립보드 Data Ctrl+C(복사), Ctrl+V(붙여넣기) 기능 처리
 * ★ Parameter
 *     1. poGrid: 대상 Grid
 *     2. psKey : KeyEventInfo (Keydown시 입력된 값)
 * ★ Return     : 없음
    - 스크립트 예)  esm_fn_grdCopyPaste(obj, e);
 ******************************************************************************/

function esm_fn_grdCopyPaste(poGrid:Grid, psKey:KeyEventInfo)

{

    var obj = poGrid;

    var e = psKey;

 

    if(e.ctrlKey) {

 

        if(e.keycode == 67) {       // Ctrl + C (복사)

            //Grid Binddataset

            var sGrdDsNm = obj.binddataset;

            var v_clip = "";

            var sSeperate = "   ";

            for (var i=obj.selectstartrow;i<=obj.selectendrow;i++) {

                for (var j=obj.selectstartcol;j<=obj.selectendcol;j++) {

                    if (j < obj.selectendcol) {

                        v_clip += obj.getCellValue(i,j) + sSeperate;

                    } else {

                        v_clip += obj.getCellValue(i,j);

                    }

                }

                if (i < obj.selectendrow) {

                    v_clip += "\r\n";

                }

            }

            v_clip += "\r\n";

            system.clearClipboard();

            system.setClipboard("CF_TEXT",v_clip);  

 

        } else if(e.keycode == 86) {      // Ctrl + V (붙여넣기)

 

            var bAddrow = true;

            if (!gfn_IsNull(esm_fn_grdCopyPaste.arguments[2])) {

                bAddrow = esm_fn_grdCopyPaste.arguments[2];

            }

            //Grid Binddataset

            var sGrdDsNm = obj.binddataset;

            //cell count

            var nGrdCellCnt = obj.getCellCount("body");

            //cell position

            var nGrdCellPos = obj.getCellPos();         

            //row position

            var nRowPos = eval(sGrdDsNm).rowposition;

            //aText2 index

            var k = 0;

            //Dataset rowcount

            var nDsRowCnt = eval(sGrdDsNm).getRowCount();

            

            var t_clip = system.getClipboard("CF_UNICODETEXT");

            var sText = new String(t_clip);

            var aText = new Array();

            var aText2 = new Array();

            aText = sText.split("\r\n");

            if (nDsRowCnt < (aText.length + nRowPos -1)) {

                if (bAddrow) {

                    

                } else {

                    return false;

                }           

            }           

            //복사한 Row만큼

            for (var i=0;i<aText.length;i++) {

                if (gfn_IsNull(aText[i])) {

                    return;

                }

                aText2 = aText[i].split("   ");

 

                //기존 dataset갯수보다 많은 경우

                if ( nDsRowCnt <= nRowPos) {

                    var nAddrow = eval(sGrdDsNm).addRow();

                }

                

                var nLoopCnt = (nGrdCellPos + aText2.length);

                if (nLoopCnt > nGrdCellCnt) {

                    nLoopCnt = nGrdCellCnt;

                }

                //Dataset setcolumn

                for (var j=nGrdCellPos;j<nLoopCnt;j++) {    

                    obj.setCellPos(j);

                    obj.showEditor(true);

                    obj.setEditText(aText2[k]);

                    k++;

                    obj.showEditor(false);

                }               

                nRowPos++;

                eval(sGrdDsNm).rowposition = nRowPos;

                k = 0;

            }

            return true;

        }

    }

}

 

☞  

 

☞  

 
 
 
 

  

'Nexacro-Function > Grid' 카테고리의 다른 글

Nexacro.Grid - treeView  (0) 2017.01.28
Nexacro.Grid - excel exort  (0) 2017.01.28
Nexacro.Grid - Conents Editor  (0) 2017.01.28
Nexacro.Grid - all Checked 처리  (0) 2017.01.28
Nexacro.Grid - grid header  (0) 2017.01.28
Posted by 농부지기
,