<script type="text/javascript">
    var mygrade=<?php echo $gradelist; ?>//[{"grade":"1","title":"高一"},{"grade":"2","title":"高二"},{"grade":"3","title":"高三"}];
	/*$(function(){
		//var qq2=jQuery.load('<?php echo $this->createUrl('gradelist') ?>');
		//mygrade =qq2; //<?php echo $this->createUrl('gradelist') ?>;

});

	
	[
		{"grade":'1',"title":'高一'},
		{"grade":'2',"title":'高二'},
		{"grade":'3',"title":'高三'},
	];
	
	*/
	//var mygrade=0;
	//$.getJSON('<?php echo $this->createUrl('gradelist') ?>','',function(data){$("#qq").text(data);mygrade=data;});
	//$("#qq").load('<?php echo $this->createUrl('gradelist') ?>')	;	
	//alert($("#qq").text());
	$(function(){

		$('#tt').datagrid({  
			title:'班级列表',
			striped:true,
			url:"<?php echo $this->createUrl('qq') ?>",  
			//idField:"id",
			//checkbox:false,
			toolbar:[{
                    id:'btnAdd',
					iconCls: 'icon-add',
					text:'增加',
					handler: function(){
					
						var lastIndex = $('#tt').datagrid('getRows').length-1;
						$('#tt').datagrid('endEdit', lastIndex);
						$('#tt').datagrid('appendRow',{
							//id:'',
							//title:''
						});
						var lastIndex = $('#tt').datagrid('getRows').length-1;
						$('#tt').datagrid('beginEdit', lastIndex);
					
					}
					},'-',{
					id:'btnSave',
					iconCls: 'icon-save',
					text:'保存',
					disabled:true,
					handler: function(){
					
						//$('#btnsave').linkbutton('enable');
						var updateRows = $('#tt').datagrid('getChanges','updated');
						var insertRows = $('#tt').datagrid('getChanges','inserted');
						if(updateRows.length==0 && insertRows.length==0){
							$.messager.alert('系统提示','请先编辑字段或添加行！','warning');
							return false;
						}//这是更新
						if(updateRows.length!=0){
							var uprow = [];var newdata=[];
							/*for(var i=0;i<updateRows.length;i++){
								uprow.push(updateRows[i].department_name);//alert($('#tt').datagrid('getRowIndex',rows[i]));
							}*/
							for(var i=0;i<updateRows.length;i++){
								uprow.push(updateRows[i]);//alert($('#tt').datagrid('getRowIndex',rows[i]));
							}
							
							$.ajax({
								  type: 'POST',
								  url: '<?php echo $this->createUrl('addandupdate')?>',
								  data: {action:'updateall',qq:JSON.stringify(uprow)},
								  success: function(data){//alert(data);
									   //if(data=="yes"){
										$.messager.alert('系统提示',data,'info');
										$('#btnUndo').linkbutton('disable');
										$('#btnSave').linkbutton('disable');
										$('#tt').datagrid('reload');
									   //}else $.messager.alert('系统提示','更新失败!','info');
								   
								   },
								  dataType: "text"
								});							
						}//这是添加
						if(insertRows.length!=0){
							var isrow = [];var newdata=[];
							for(var i=0;i<insertRows.length;i++){
								isrow.push(insertRows[i]);//alert($('#tt').datagrid('getRowIndex',rows[i]));
							}
							
							$.ajax({
								  type: 'POST',
								  url: '<?php echo $this->createUrl('addandupdate')?>',
								  data: {action:'addall',qq:JSON.stringify(isrow)},
								  success: function(data){//alert(data);
									   //if(data=="yes"){
										$.messager.alert('系统提示',data,'info');
										$('#tt').datagrid('reload');
										$('#btnUndo').linkbutton('disable');
										$('#btnSave').linkbutton('disable');
									   //}else $.messager.alert('系统提示','更新失败!','info');
								   
								   },
								  dataType: "text"
								});							
						}			
                        $("#tt").datagrid("acceptChanges");
                        //alert(JSON.stringify(isrow));
					
					}
					},'-',{
					id:'btnRemove',
					iconCls: 'icon-remove',
					text:'删除',
					disabled:true,
					handler: function(){
						var rows = $('#tt').datagrid('getSelections');
						var uprow = [];
							for(var i=0;i<rows.length;i++){
								uprow.push(rows[i]);
							}
						//alert(JSON.stringify(uprow));
						$.messager.confirm('确认','确定要删除吗？',function(r){
						if (r){
							if(typeof(value)=='undefined'){
								$('#tt').datagrid('deleteRow',index);
							}else{
						$.ajax({
								  type: 'POST',
								  url: '<?php echo $this->createUrl('deleteall')?>',
								  data: {action:'deleteall',qq:JSON.stringify(uprow)},
								  success: function(data){//alert(data);
									   //if(data=="yes"){
										$.messager.alert('系统提示',data,'info');
										$('#tt').datagrid('reload');
										$('#btnRemove').linkbutton('disable');
										//$('#btnSave').linkbutton('disable');
									   //}else $.messager.alert('系统提示','更新失败!','info');
								   
								   },
								  dataType: "text"
								});
								}}})						
					}
					},'-',{
					id:'btnUndo',
					iconCls: 'icon-undo',
					text:'撤销',
					disabled:true,
					handler: function(){$("#tt").datagrid("rejectChanges");$('#btnUndo,#btnSave').linkbutton('disable');}
					}],
			
			columns:[[  
				{field:'ck',checkbox:'true'}, 
				{field:'id',title:'ID',width:50,align:'center'},  
				{field:'title',title:'班级',width:50,align:'center',editor:{type:'validatebox',options:{required:true}}},  
				{field:'grade',title:'所属年级',width:100,align:'center',
				
						formatter:function(value){
							for(var i=0; i<mygrade.length; i++){
								if (mygrade[i].grade == value) return mygrade[i].title;
							}
							return value;
						},
						editor:{
							type:'combobox',
							options:{
								valueField:'grade',
								textField:'title',
								data:mygrade,
								required:true
							}
						}
				
				},  
				{field:'action',title:'动作',width:70,align:'center',
						formatter:function(value,row,index){
							if (row.editing){
								var s = '<a href="#" onclick="saverow('+index+')">保存</a> ';
								var c = '<a href="#" onclick="cancelrow('+index+')">取消</a>';
								return s+c;
							} else {
								var e = '<a href="#" onclick="editrow('+row.id+','+index+')">编辑</a> ';
								var d = '<a href="#" onclick="deleterow('+row.id+','+index+')">删除</a>';
								return e+d;
							}
						}
				}
			]],
			onBeforeEdit:function(index,row){
				row.editing = true;
				updateActions();
			},
			onAfterEdit:function(index,row){
				row.editing = false;
				var updateRows = $('#tt').datagrid('getChanges','updated');
				var insertRows = $('#tt').datagrid('getChanges','inserted');
				if(updateRows.length>0 || insertRows.length>0){
					$('#btnUndo').linkbutton('enable');
					$('#btnSave').linkbutton('enable');
				}
				updateActions();
			},
			onCancelEdit:function(index,row){
				row.editing = false;
				updateActions();
			},
            onSelect:function(index,row){
                $('#btnRemove').linkbutton('enable');
                },
			onUnselect:function(rowIndex, rowData){
				var rows = $('#tt').datagrid('getSelections');
				if(rows.length==0) $('#btnRemove').linkbutton('disable');
				},
            onSelectAll:function(index,row){
                $('#btnRemove').linkbutton('enable');
                },
            onUnselectAll:function(index,row){
                $('#btnRemove').linkbutton('disable');
                },
		}); 
	});
	
	function updateActions(){
		var rowcount = $('#tt').datagrid('getRows').length;
		for(var i=0; i<rowcount; i++){
			$('#tt').datagrid('updateRow',{
				index:i,
				row:{action:''}
			});
		}
	}
	function editrow(value,index){
		$('#tt').datagrid('beginEdit', index);
	}
	function deleterow(value,index){
		$.messager.confirm('确认','确定要删除吗？',function(r){
			if (r){
				if(typeof(value)=='undefined'){
					$('#tt').datagrid('deleteRow',index);
				}else{
					$.ajax({
						type: 'POST',
						url: '<?php echo $this->createUrl("deleterow")?>',
						data: {id:value},
						success: function(data){if(data=="yes"){
						$.messager.alert('系统提示','删除成功!','info');
						$('#tt').datagrid('deleteRow',index);
						$('#tt').datagrid('reload');
						//updateActions();
						$("#tt").datagrid("acceptChanges");
					   }else $.messager.alert('系统提示','删除失败!'+'<br />'+data,'info');},
					  dataType: "text"
					});	
				}
			
			}
		});
	}

	
	function saverow(index){
		$('#tt').datagrid('endEdit', index);
	}
	function cancelrow(index){
		$('#tt').datagrid('cancelEdit', index);
	}
</script>
<table id="tt">  
</table>