티스토리 뷰

Row data 선택 이벤트 처리

1. click 이벤트 처리 

gridOptions에 onRowClicked를 구현하면 된다. 

1
2
3
4
5
6
7
8
9
10
var gridOptions = {
    columnDefs: columnDefs,
    rowData: students,
    rowSelection: 'single'/* 'single' or 'multiple',*/
    enableRangeSelection: true,
    suppressRowClickSelection: false,
    onRowClicked : funtion(event){
        console.log('onRowClicked');
    }
};
cs

Html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>AG GRID CLICK EVENT</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js"></script>
  <script src="https://unpkg.com/ag-grid/dist/ag-grid.min.js"></script>
  <script src="/assets/js/agGridUtil.js"></script>
</head>
<body>
<div class="container">
    <h2>AG GRID CLICK EVENT</h2>
    <div id="selectedRows" style="height:30px"></div>
    <div class="grid-wrapper">
        <div style="width: 100%; height:580px" id="myGrid" class="ag-grid-div ag-theme-balham ag-basic"></div>
    </div>
</div>
  <script>
  var MainGrid = function(){
        var _this = this;
        /* grid 영역 정의 */
        this.gridDiv = "myGrid";
        /* grid 칼럼 정의 */
        this.getColumnDefs = function(){
            var columnDefs = [
                {field: "athlete", width: 150, suppressSizeToFit: true},
                {field: "age", width: 90, minWidth: 50, maxWidth: 100},
                {field: "country", width: 120},
                {field: "year", width: 90},
                {field: "date", width: 110},
                {field: "sport", width: 110},
                {field: "gold", width: 100},
                {field: "silver", width: 100},
                {field: "bronze", width: 100},
                {field: "total", width: 100}
            ];
            var gridOpt = CommonGrid.getDefaultGridOpt(columnDefs);
            gridOpt.onRowClicked = _this.onRowClicked;
            return gridOpt;
        };
        /* grid 옵션 가져오기 */
        this.gridOpts = null
        /* grid 실행 */
        this.makeGrid = function(rowData){
            _this.gridOpts = _this.getColumnDefs();
            CommonGrid.makeGridCommon(_this.gridDiv, _this.gridOpts, rowData)
        };
        this.onRowClicked = function(event) {
            document.querySelector('#selectedRows').innerHTML = JSON.stringify(event.node.data);
        }
    }
    var mainGrid = new MainGrid();
 
    // setup the grid after the page has finished loading
    document.addEventListener('DOMContentLoaded'function() {
        var httpRequest = new XMLHttpRequest();
        httpRequest.open('GET''./olympicWinnersSmall.json');
        httpRequest.send();
        httpRequest.onreadystatechange = function() {
            if (httpRequest.readyState === 4 && httpRequest.status === 200) {
                var httpResult = JSON.parse(httpRequest.responseText);
                mainGrid.makeGrid(httpResult);
            }
        };
    });  
  </script>
</body>
</html>
cs

2. 선택 이벤트 처리 및 선택 조건 예제 

- 선택된 Row의 데이터를 읽고 처리한다. 

- 한개 또는 복수개 선택

- 특정 데이터는 선택 안되게 차단

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var gridOptions = {
    columnDefs: columnDefs,
    rowData: students,
    rowSelection: 'single'/* 'single' or 'multiple',*/
    enableRangeSelection: true,
    suppressRowClickSelection: false,
    isRowSelectable : function(event){
        console.log('isRowSelectable');
        return true;
    },
    onSelectionChanged : function (event) {
        console.log('onSelectionChanged');
    }
};
cs



Html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>AG GRID SELECTION 샘플</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js"></script>
  <script src="https://unpkg.com/ag-grid/dist/ag-grid.min.js"></script>
  <script src="/assets/js/agGridUtil.js"></script>
</head>
<body>
<div class="container">
    <h2>AG GRID SELECTION 샘플</h2>
    <small>선택된 row의 정보를 읽고 처리한다.</small><br/>
    <small>multiple or single 가능</small><br/>
    <small>특정 row 선택 차단(Russia는 선택 안됨)</small><br/>
    <div id="selectedRows" style="height:30px"></div>
    <div class="grid-wrapper">
        <div style="width: 100%; height:580px" id="myGrid" class="ag-grid-div ag-theme-balham ag-basic"></div>
    </div>
</div>
  <script>
  var MainGrid = function(){
        var _this = this;
        /* grid 영역 정의 */
        this.gridDiv = "myGrid";
        /* grid 칼럼 정의 */
        this.getColumnDefs = function(){
            var columnDefs = [
                {headerName : "seq", width: 150, suppressSizeToFit: true, cellRenderer:_this.getRowIndex},
                {field: "athlete", width: 150, suppressSizeToFit: true},
                {field: "age", width: 90, minWidth: 50, maxWidth: 100},
                {field: "country", width: 120},
                {field: "year", width: 90},
                {field: "date", width: 110},
                {field: "sport", width: 110},
                {field: "gold", width: 100},
                {field: "silver", width: 100},
                {field: "bronze", width: 100},
                {field: "total", width: 100}
            ];
            var gridOpt = CommonGrid.getDefaultGridOpt(columnDefs);
            gridOpt.onSelectionChanged = _this.onSelectionChanged;
            gridOpt.rowSelection = 'multiple';
            gridOpt.isRowSelectable = function(rowNode){
                return (rowNode.data.country != "Russia")? true:false;
            }
            gridOpt.getRowNodeId = function(data) {
                return data.athlete+data.age+data.country; 
            };
            return gridOpt;
        };
        /* grid 옵션 가져오기 */
        this.gridOpts = null
        /* grid 실행 */
        this.makeGrid = function(rowData){
            _this.gridOpts = _this.getColumnDefs();
            CommonGrid.makeGridCommon(_this.gridDiv, _this.gridOpts, rowData)
        };
        this.onSelectionChanged = function() {
            var selectedRows = _this.gridOpts.api.getSelectedRows();
            var selectedRowsString = '';
            selectedRows.forEach( function(selectedRow, index) {
                if (index>5) {
                    return;
                }
                if (index!==0) {
                    selectedRowsString += ', ';
                }
                selectedRowsString += selectedRow.athlete;
            });
            if (selectedRows.length>=5) {
                selectedRowsString += (' - and ' + (selectedRows.length - 5+ ' others');
            }
            document.querySelector('#selectedRows').innerHTML = selectedRowsString;
        };
        this.getRowIndex = function(node){
            return node.rowIndex+1;
        }
    }
    var mainGrid = new MainGrid();
 
    // setup the grid after the page has finished loading
    document.addEventListener('DOMContentLoaded'function() {
        var httpRequest = new XMLHttpRequest();
        httpRequest.open('GET''./olympicWinnersSmall.json');
        httpRequest.send();
        httpRequest.onreadystatechange = function() {
            if (httpRequest.readyState === 4 && httpRequest.status === 200) {
                var httpResult = JSON.parse(httpRequest.responseText);
                mainGrid.makeGrid(httpResult);
            }
        };
    });  
  </script>
</body>
</html>
cs


Json data

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[
    {
        "athlete": "Michael Phelps",
        "age": 23,
        "country": "United States",
        "year": 2008,
        "date": "24/08/2008",
        "sport": "Swimming",
        "gold": 8,
        "silver": 0,
        "bronze": 0,
        "total": 8
    },
    {
        "athlete": "Michael Phelps",
        "age": 19,
        "country": "United States",
        "year": 2004,
        "date": "29/08/2004",
        "sport": "Swimming",
        "gold": 6,
        "silver": 0,
        "bronze": 2,
        "total": 8
    },
    {
        "athlete": "Michael Phelps",
        "age": 27,
        "country": "United States",
        "year": 2012,
        "date": "12/08/2012",
        "sport": "Swimming",
        "gold": 4,
        "silver": 2,
        "bronze": 0,
        "total": 6
    }
]
cs


파일 첨부


'UI(Front-End) > 그리드' 카테고리의 다른 글

AG-GRID 추가/수정/삭제 예제  (1) 2019.01.30
AG-GRID 필터와 정렬 예제  (0) 2019.01.30
AG-GRID Data 선택 이벤트 처리 예제  (0) 2019.01.30
AG-GRID cellRenderer 예제  (4) 2019.01.30
AG-GRID 기본 예제  (0) 2019.01.30
AG-GRID GRID OPTIONS 정의  (1) 2019.01.30
댓글
댓글쓰기 폼