<script type="text/javascript">
$.extend($.fn.datagrid.methods, {
	editCell: function(jq,param){
		return jq.each(function(){
			var opts = $(this).datagrid('options');
			var fields = $(this).datagrid('getColumnFields',true).concat($(this).datagrid('getColumnFields'));
			for(var i=0; i<fields.length; i++){
				var col = $(this).datagrid('getColumnOption', fields[i]);
				col.editor1 = col.editor;
				if (fields[i] != param.field){
					col.editor = null;
				}
			}
			$(this).datagrid('beginEdit', param.index);
			var ed = $(this).datagrid('getEditor', param);
			if (ed){
				if ($(ed.target).hasClass('textbox-f')){
					$(ed.target).textbox('textbox').focus();
				} else {
					$(ed.target).focus();
				}
			}
			for(var i=0; i<fields.length; i++){
				var col = $(this).datagrid('getColumnOption', fields[i]);
				col.editor = col.editor1;
			}
		});
	},
	enableCellEditing: function(jq){
		return jq.each(function(){
			var dg = $(this);
			var opts = dg.datagrid('options');
			opts.oldOnClickCell = opts.onClickCell;
			opts.onClickCell = function(index, field){
				if (opts.editIndex != undefined){
					if (dg.datagrid('validateRow', opts.editIndex)){
						dg.datagrid('endEdit', opts.editIndex);
						opts.editIndex = undefined;
					} else {
						return;
					}
				}
				dg.datagrid('selectRow', index).datagrid('editCell', {
					index: index,
					field: field
				});
				opts.editIndex = index;
				opts.oldOnClickCell.call(this, index, field);
			}
		});
	}
});
function setdg(){
	$('#dg').treegrid({
		// height:theight,
		striped:true,
		toolbar:"#toolbar",
		fit:true,
		fitColumns:true,
		singleSelect:true,
		rownumbers:true,
		pagination:false,
		idField: 'menu_id',
        treeField: 'menu_name',
		columns:[[
			{field:'menu_name',title:'Nama Menu',width:150},
			{field:'lihat',title:'Lihat',width:100,align:'center',editor:{type:'checkbox',options:{on:'1',off:'0'}}},
			{field:'tambah',title:'Tambah',width:100,align:'center',editor:{type:'checkbox',options:{on:'1',off:'0'}}},
			{field:'ubah',title:'Ubah',width:100,align:'center',editor:{type:'checkbox',options:{on:'1',off:'0'}}},
			{field:'hapus',title:'Hapus',width:100,align:'center',editor:{type:'checkbox',options:{on:'1',off:'0'}}}
		]],
		url: '<?=site_url($this->modul . "/gridHakAkses/$group_id");?> '
	});
}
function setValue(val){
	var rows = $('#dg').treegrid('getSelections');
	//console.log(rows);
	//FORM LIST BARANG
	lihat_val="";
	tambah_val="";
	ubah_val="";
	hapus_val="";
	j=1;
	for(var i=0; i<rows.length; i++){
		$('#dg').treegrid('update',{
			id: rows[i].menu_id,
			row: {
				lihat: val,
				tambah: val,
				ubah: val,
				hapus: val
			}
		});
	}
}
function simpan(){
	var rows = $('#dg').treegrid('getData');
	//console.log(rows);
	//FORM LIST BARANG
	group_id_val="";
	menu_id_val="";
	lihat_val="";
	tambah_val="";
	ubah_val="";
	hapus_val="";
	j=1;
	for(var i=0; i<rows.length; i++){
		group_id_val += rows[i].group_id + "`";
		menu_id_val += rows[i].menu_id + "`";
		lihat_val += rows[i].lihat + "`";
		tambah_val += rows[i].tambah + "`";
		ubah_val += rows[i].ubah + "`";
		hapus_val += rows[i].hapus + "`";
		for(var k=0; k<rows[i].children.length; k++){
			group_id_val += rows[i].children[k].group_id + "`";
			menu_id_val += rows[i].children[k].menu_id + "`";
			lihat_val += rows[i].children[k].lihat + "`";
			tambah_val += rows[i].children[k].tambah + "`";
			ubah_val += rows[i].children[k].ubah + "`";
			hapus_val += rows[i].children[k].hapus + "`";
			for(var l=0; l<rows[i].children[k].children.length; l++){
				group_id_val += rows[i].children[k].children[l].group_id + "`";
				menu_id_val += rows[i].children[k].children[l].menu_id + "`";
				lihat_val += rows[i].children[k].children[l].lihat + "`";
				tambah_val += rows[i].children[k].children[l].tambah + "`";
				ubah_val += rows[i].children[k].children[l].ubah + "`";
				hapus_val += rows[i].children[k].children[l].hapus + "`";
				for(var m=0; m<rows[i].children[k].children[l].children.length; m++){
					group_id_val += rows[i].children[k].children[l].children[m].group_id + "`";
					menu_id_val += rows[i].children[k].children[l].children[m].menu_id + "`";
					lihat_val += rows[i].children[k].children[l].children[m].lihat + "`";
					tambah_val += rows[i].children[k].children[l].children[m].tambah + "`";
					ubah_val += rows[i].children[k].children[l].children[m].ubah + "`";
					hapus_val += rows[i].children[k].children[l].children[m].hapus + "`";
				}
			}
		}
	}
	//AKHIR FORM LIST BARANG
		//alert(menu_id_val);
	$.post("<?=site_Url($this->modul . '/simpan')?>", {
	//FORM LIST DATA BARANG
	grup:group_id_val,code_menu:menu_id_val,lihat:lihat_val,tambah:tambah_val,ubah:ubah_val,hapus:hapus_val
	},
	function(result){
		var result = eval('('+result+')');
		if (result.success){
			$('#dg').datagrid('reload');	// reload the user data
			$.messager.alert('Info',result.msg);
			location.href="<?=site_url($this->modul . "/hakakses/$group_id/$group_name")?>";
		} else {
			$.messager.alert('Error',result.msg);
		}
	});
}
</script>