이벤트 부여

 그리드의 각종 마크업에 이벤트를 부여해서 인터렉션이 일어나게 하는 것은 웹 개발에서 기본적인 일 입니다.

이 챕터에서는 그리드 로우 또는 로우 안에 존재하는 마크업에 이벤트를 부여하는 두가지 방식을 설명합니다.

xml
<table id="grid-table-1">
</table>
<script id="grid-template-1" type="text/template">
	<table width="100%" height="350px">
		<thead>
			<tr>
				<th name="indx" align="center">순위</th>
				<th name="Isu">종목명</th>
				<th name="NowPrc" align="right">현재가</th>
				<th name="PrdayCmp" align="right">전일비</th>
				<th name="Updn" align="right">등락율(%)</th>
				<th name="buy" align="right">사기</th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td name="indx" bind="indx"></td>
				<td name="Isu" bind="Isu"></td>
				<td name="NowPrc" bind="NowPrc"></td>
				<td name="PrdayCmp" bind="PrdayCmp"></td>
				<td name="Updn" bind="Updn"></td>
				<td name="buy"><input type="button" class="buy-button"/></td>
			</tr>
		</tbody>
	</table>
</script>
 
<script>
var table1 = $('#grid-table-1');
var template1 = $('#grid-template-1');
var grid = webponent.grid.init(table1, template1);
</script>

위 그리드는 마지막 컬럼에 buy-button이라는 클래스를 가진 버튼이 있습니다. 이 버튼을 클릭했을때 인터랙션이 일어나도록 하려면 다음과 같은 방법이 있습니다.

rowRendered사용xml
// 그리드 초기화시 rowRendered 옵션 사용
var grid = webponent.grid.init(table1, template1, {
	rowRendered : function (row, data, index) {
 
		var button = $(row).find('.buy-button');
		button.on('click', function () {
			
			// Do some thing
		});
	}
});
 
// 혹은 rowRendered이번트 사용
grid.on('rowRendered', function (e, row, data, index) {

		var button = $(row).find('.buy-button');
		button.on('click', function () {

			// Do some thing
		});
	}
});

 위 방법은 그리드 초기화시 rowRenderd이벤트를 등록하는가, 아니면 초기화 후 이벤트를 등록하는 거의 동일한 방법 입니다. 그럼 다음 방법을 소개 하겠습니다.

이벤트 delegation 사용xml
$(grid.markup.main.body.tbody).on('click', '.buy-button', function () {
	
	var button = $(this);
 
	button.on('click', function () {

		// Do some thing
	});
});

 이 방법은 그리드 마크업 모델로부터 테이블 바디를 얻어온후 .buy-button요소에 이벤트를 delegation하는 방식 입니다. 처음 소개했던 방법은 로우의 개수만큼 이벤트가 부여되는 반면 이 방식은

한번의 이벤트 바인딩으로 그치게 됩니다. 이 방법은 로우의 개수가 많은 상황에서 성능상으로 유리하나, 로우 모델을 이용하기 위해 TR노드를 찾으려면 별도의 탐색이 필요하다는 단점이 있습니다.