1. 程式人生 > >對jqgrid的某列繫結事件

對jqgrid的某列繫結事件

部分html程式碼:

<div class="main" style="margin-left:400px;">    
    <table id="list_grid" class="grid"></table>
    <div id="list_pager"></div>
</div>

jqgrid部分:

$("#stocklist_grid").jqGrid({
	url:'ajax.php?action=get_stock_list',
	datatype: "json",
	mtype: "POST",			
	colNames:['ID', 'Code', 'Name', 'Mark'],
	colModel:[
		{name:'id',index:'id',width:60,align:'center'},
		{name:'code',index:'code', wdith: 80, align:'center', editable:true, edittype:'textarea', editoptions:{rows:'10'}},
		{name:'name',index:'name', width: 80, align:'center'},
		{name:'mark',index:'mark', width: 220, align:'left', formatter:function(cellvalue, options, rowObj){
                    return "<span id='"+rowObj.id+"' class='mark_data' style='display:block; width:100%; cursor:pointer;'>"+cellvalue+"</span>";
		}}
	], 
關鍵在紅色部分,用formatter將mark列的加入span標籤(其實用什麼標籤不重要,重要的時要找到改元素節點)。

Js(Jquery):

$(function(){
	//on方法對動態元素繫結click事件
	$("#list_grid").on("click", ".mark_data", function(){
		//TODO...
            bnames = $(this).text();
		$("#dialog_select_blocks").dialog("open");
	});
});

由於jqgrid表格中內容是動態載入的,這裡要用on方法繫結事件。