티스토리 뷰

UI(Front-End)/그리드

AG-GRID 칼럼 동적 HIDE/SHOW 예제

까오기 까오기 2019. 1. 30. 16:12

AG-GRID 칼럼 동적 HIDE/SHOW 예제

칼럼을 동적으로 보여주거나 감추는 예제


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
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>AG GRID Column Hide 예제</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <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://code.jquery.com/jquery-2.2.4.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 Column Hide 예제</h2>
    <div class="row">
      <div class="col-lg-12">
        <div class="checkbox">
          <label>
          <input type="checkbox" onclick="showColumn(this,'age')"><span>Age</span>
          </label>
        </div>
        <div class="checkbox">
          <label>
          <input type="checkbox" onclick="showColumn(this,'country')" checked=true><span>Country</span>
          </label>
        </div>
        <div class="checkbox">
          <label>
          <input type="checkbox" onclick="showColumn(this,'year')" checked=true><span>Year</span>
          </label>
        </div>
        <div class="checkbox">
          <label>
          <input type="checkbox" onclick="showColumn(this,'date')" checked=true><span>Date</span>
          </label>
        </div>
      </div>
    </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:"Athlete", field: "athlete", width: 150, minWidth:120, pinned:'left', suppressSizeToFit: true, suppressMenu: true, sort: 'asc'},
        {headerName:"Age", field: "age", hide:true, width: 90, minWidth: 50, maxWidth: 100, pinned:'left', suppressMenu: true, suppressSorting : true},
        {headerName:"Country", field: "country", width: 120},
        {headerName:"Year", field: "year", width: 90},
        {headerName:"Date", field: "date", width: 110},
        {headerName:"Sport", field: "sport", width: 110},
        {headerName:"Gold Medal", field: "gold", width: 100},
        {headerName:"Silver Medal", field: "silver", width: 100},
        {headerName:"Bronze Medal", field: "bronze", width: 100},
        {headerName:"Total", field: "total", width: 100}
    ];
        var gridOpt = CommonGrid.getDefaultGridOpt(columnDefs);
        //gridOpt.onRowClicked = onRowClicked;
        return gridOpt;
    };
    /* grid 옵션 가져오기 */
    this.gridOpts = null
    /* grid 실행 */
    this.makeGrid = function(rowData){
        _this.gridOpts = _this.getColumnDefs();
        CommonGrid.makeGridCommon(_this.gridDiv, _this.gridOpts, rowData)
    };
}
 
    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);
            }
        };
    });
    function showColumn(ev, colId) {
        mainGrid.gridOpts.columnApi.setColumnVisible(colId, ev.checked);
    }
</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 칼럼 동적 HIDE/SHOW 예제  (0) 2019.01.30
AG-GRID Datepicker 예제  (0) 2019.01.30
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
댓글
댓글쓰기 폼