<script>
$(function () {
	
	$('#arole').treegrid({
		url : '<?php echo $url?>base/role/view_nav',
		fit : true,
		fitColumns : true,
		striped : true,
		rownumbers : true,
		border : false,
		idField:'nav_id',    
	    treeField:'nav_name',
	    method:'get',
	    toolbar:'#arole_tb',	    
		columns : [[
			{
				field : 'nav_name',
				title : '名称',
				width : 150,
			},
			{
				field : 'role_alla',
				title : '全选',
				align:'center',
				width : 100,
				formatter: function(value,row,index){
					if (row.nav_fid!=0){
						return '<input  type="checkbox" onclick="rolealla(this,'+row.nav_id+')">';
					} 
				}						
			},
        <?php foreach ($button as $key=>$value): ?>               
			{
				field : '<?=$key?>',
				title : '<?=$value->button_name?>',
				align:'center',
				width : 100,
				formatter: function(value,row,index){
					if (row.nav_fid!=0){
						return '<input name="'+'<?=$value->button_id?>'+'" class="arolenav'+row.nav_id+'" type="checkbox">';
					} 
				}						
			},
        <?php endforeach; ?>         								
		]]
	});
	
	
	
	$('#arole_name').validatebox({
		required : true,
		validType : 'length[2,20]',
		missingMessage : '请输入角色名称',
		invalidMessage : '角色名称在 2-20 位',
	});

});

function rolealla(obj,id){
	if ($(obj).is(":checked")) {
		$('.arolenav'+id).prop("checked",true);
	}
	else {
		$('.arolenav'+id).removeAttr("checked");
	}
}

function arole(){

	var obj=$("input[class^='arolenav']");

    var date = {};    
	obj.each(function(){
		 if($(this).prop('checked')){
			 var key=$(this).attr('class');
			 date[key] = $(this).attr('name');
		}
    });
	$.each(date, function (i, item) {
		date[i]=[];
	})
	$.each(date, function (i, item) {
		obj.each(function(){
			 if($(this).prop('checked')){
				 var key=$(this).attr('class');
				 //date[key] = $(this).attr('name');
				 if(i==$(this).attr('class')){
					 date[i].push($(this).attr('name'));
				}
			}
	    })
	}) 

	var role_name=$('#arole_name').val();
	$.ajax({
		   type: "POST",
		   url: "<?php echo $url?>base/role/arole/"+role_name,
		   data: date,
			beforeSend : function () {
				$.messager.progress({
					text : '正在添加中...',
				});
			},		   
           success:function(data){
				$.messager.progress('close');   
                  var data = eval('(' + data + ')');     
                  if (data.success){    
               	 	 $.messager.alert('提示',data.message,'info');	  
                	 	$('#tabs').tabs('close','添加角色');
//                 	 	var currentTab = $('#tabs').tabs('getSelected');
//                       	 RefreshTab(currentTab);
                	 	$('#baserole').datagrid('reload');
                  }else{
                 	 	 $.messager.alert('提示',data.message,'info');   
                  }                   	
              }  		   
		});		  
}

</script>
<table id="arole"></table>

<div id="arole_tb" style="padding:2px 5px;">
    角色名: <input id="arole_name" name="role_name"  style="width:110px" >
    <a id="arolebutton" href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="return arole()">提交</a>
</div>



