<script type="text/javascript">
	$(function(){
		$('#tt').datagrid({  
			title:'此分类下单选题列表列表',
			striped:true,
			pagination:true,
			//queryParams:{pageSize:'10',page:'10'},
			remoteSort:true,
			sortName: 'id',
			sortOrder: 'asc',
			url:"<?php echo $this->createUrl('qq',array('charpter_id'=>$charpter_id)); ?>",  
			//idField:"id",
			//checkbox:false,
			fitColumns:true,
			toolbar:"#tb",			
			columns:[[  
				{field:'ck',checkbox:'true'}, 
				{field:'id',title:'ID',width:50,align:'center',sortable:true},  
				{field:'title',title:'标题',width:200,align:'center',sortable:true,editor:{type:'validatebox',options:{required:true}}}, 
				{field:'knowledgepoint_id',title:'所属知识点',width:150,align:'center',sortable:false,editor:{type:'validatebox',options:{required:true}}}, 
				{field:'difficulty',title:'难度',width:50,align:'center',sortable:true,editor:{type:'validatebox',options:{required:true}}}, 
				{field:'frequency',title:'频率',width:50,align:'center',sortable:true,editor:{type:'validatebox',options:{required:true}}}, 
				{field:'forbidden',title:'禁用',width:50,align:'center',sortable:true,editor:{type:'checkbox',options:{"0":'P',"1":'Q'}}}, //这里的editor没用。可以取消
				{field:'created',title:'创建日期',width:150,align:'center',sortable:true,editor:{type:'validatebox',options:{required:true}}}, 
				{field:'modified',title:'修改日期',width:150,align:'center',sortable:true,editor:{type:'validatebox',options:{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;
							}
						}
				}
			]],
            onSelect:function(index,row){
                $('#btnRemove').linkbutton('enable');
				$('#btnOn').linkbutton('enable');
				$('#btnOff').linkbutton('enable');
                },
			onUnselect:function(rowIndex, rowData){
				var rows = $('#tt').datagrid('getSelections');
				if(rows.length==0) {
					$('#btnRemove').linkbutton('disable');
					$('#btnOn').linkbutton('disable');
					$('#btnOff').linkbutton('disable');
				}
				},
            onSelectAll:function(index,row){
                $('#btnRemove').linkbutton('enable');
				$('#btnOn').linkbutton('enable');
				$('#btnOff').linkbutton('enable');
                },
            onUnselectAll:function(index,row){
                $('#btnRemove').linkbutton('disable');
				$('#btnOn').linkbutton('disable');
				$('#btnOff').linkbutton('disable');
                } 			
		}); 
	});
	function editrow(value,index){
		//alert(value);
		//$('#ff').form('load',value);
		$('#w').window('open');
		//$('#ff').form('load','<?php echo $this->createUrl('editrow').'/id/';?>'+value);
		var url='<?php echo $this->createUrl('update').'/id/';?>'+value;
		$('#myform').html('<iframe scrolling="auto" frameborder="0" src="'+url+'" style="width:100%;height:100%;"></iframe>');
		//$('#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);
	}
	function close1(){
		$('#w').window('close');
	}
	function open1(){
		$('#w').window('open');
	}
	$(function(){
		$("#w").window({
			onClose:function(){$("#tt").datagrid("reload");}
		});
	})
	function tt_add(){
		$('#w').window('open');
		var url='<?php echo $this->createUrl('create',array('charpter_id'=>$charpter_id));?>';
		$('#myform').html('<iframe scrolling="auto" frameborder="0" src="'+url+'" style="width:100%;height:100%;"></iframe>');
	}
	function tt_remove(){
		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){


						$.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');
									$('#btnOn').linkbutton('disable');
									$('#btnOff').linkbutton('disable');
									//$('#btnSave').linkbutton('disable');
								   //}else $.messager.alert('系统提示','更新失败!','info');
							   
							   },
							  dataType: "text"
						});	

				
				}
			});
	}
	function tt_on(){
		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){


						$.ajax({
							  type: 'POST',
							  url: '<?php echo $this->createUrl('onoff')?>',
							  data: {action:'addon',qq:JSON.stringify(uprow)},
							  success: function(data){//alert(data);
								   //if(data=="yes"){
									$.messager.alert('系统提示',data,'info');
									$('#tt').datagrid('reload');
									$('#btnRemove').linkbutton('disable');
									$('#btnOn').linkbutton('disable');
									$('#btnOff').linkbutton('disable');
									//$('#btnSave').linkbutton('disable');
								   //}else $.messager.alert('系统提示','更新失败!','info');
							   
							   },
							  dataType: "text"
						});	

				
				}
			});
	}
	function tt_off(){
		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){


						$.ajax({
							  type: 'POST',
							  url: '<?php echo $this->createUrl('onoff')?>',
							  data: {action:'addoff',qq:JSON.stringify(uprow)},
							  success: function(data){//alert(data);
								   //if(data=="yes"){
									$.messager.alert('系统提示',data,'info');
									$('#tt').datagrid('reload');
									$('#btnRemove').linkbutton('disable');
									$('#btnOn').linkbutton('disable');
									$('#btnOff').linkbutton('disable');
									//$('#btnSave').linkbutton('disable');
								   //}else $.messager.alert('系统提示','更新失败!','info');
							   
							   },
							  dataType: "text"
						});	

				
				}
			});
	}
	function tt_allon(){
		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){


						$.ajax({
							  type: 'POST',
							  url: '<?php echo $this->createUrl('onoff',array('charpter_id'=>$charpter_id))?>',
							  data: {action:'alladdon'},
							  success: function(data){//alert(data);
								   //if(data=="yes"){
									$.messager.alert('系统提示',data,'info');
									$('#tt').datagrid('reload');
									$('#btnRemove').linkbutton('disable');
									$('#btnOn').linkbutton('disable');
									$('#btnOff').linkbutton('disable');
									//$('#btnSave').linkbutton('disable');
								   //}else $.messager.alert('系统提示','更新失败!','info');
							   
							   },
							  dataType: "text"
						});	

				
				}
			});	
	}
	function tt_alloff(){
		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){


						$.ajax({
							  type: 'POST',
							  url: '<?php echo $this->createUrl('onoff',array('charpter_id'=>$charpter_id))?>',
							  data: {action:'alladdoff'},
							  success: function(data){//alert(data);
								   //if(data=="yes"){
									$.messager.alert('系统提示',data,'info');
									$('#tt').datagrid('reload');
									$('#btnRemove').linkbutton('disable');
									$('#btnOn').linkbutton('disable');
									$('#btnOff').linkbutton('disable');
									//$('#btnSave').linkbutton('disable');
								   //}else $.messager.alert('系统提示','更新失败!','info');
							   
							   },
							  dataType: "text"
						});	

				
				}
			});		
	}
	function tt_clear_pinlv(){
		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){


						$.ajax({
							  type: 'POST',
							  url: '<?php echo $this->createUrl('clear_zero',array('charpter_id'=>$charpter_id))?>',
							  data: {action:'pinlv'},
							  success: function(data){//alert(data);
								   //if(data=="yes"){
									$.messager.alert('系统提示',data,'info');
									$('#tt').datagrid('reload');
									$('#btnRemove').linkbutton('disable');
									$('#btnOn').linkbutton('disable');
									$('#btnOff').linkbutton('disable');
									//$('#btnSave').linkbutton('disable');
								   //}else $.messager.alert('系统提示','更新失败!','info');
							   
							   },
							  dataType: "text"
						});	

				
				}
			});		
	}	
	function tt_clear_nandu(){
		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){


						$.ajax({
							  type: 'POST',
							  url: '<?php echo $this->createUrl('clear_zero',array('charpter_id'=>$charpter_id))?>',
							  data: {action:'nandu'},
							  success: function(data){//alert(data);
								   //if(data=="yes"){
									$.messager.alert('系统提示',data,'info');
									$('#tt').datagrid('reload');
									$('#btnRemove').linkbutton('disable');
									$('#btnOn').linkbutton('disable');
									$('#btnOff').linkbutton('disable');
									//$('#btnSave').linkbutton('disable');
								   //}else $.messager.alert('系统提示','更新失败!','info');
							   
							   },
							  dataType: "text"
						});	

				
				}
			});		
	}
function my_query(){
	if($.trim($("#title").val())==""){
		$.messager.alert('系统提示','请输入标题!','info');
		$("#title").focus();
		return ;
	}
	var title=$.trim($("#title").val());
	var queryParams = $('#tt').datagrid('options').queryParams; 
	queryParams.title = title;  
	$('#tt').datagrid('options').queryParams=queryParams;        
	$("#tt").datagrid('reload'); 
}	
function display_all(){
	$('#tt').datagrid('options').queryParams=[];  
	$("#tt").datagrid('reload'); 
}
</script>
<div data-options="region:'center',border:false">
	<table id="tt">  
	</table>
</div>
<div id="w" class="easyui-window" data-options="title:'编辑',closed:true,shadow:true,modal:true,iconCls:'icon-save'" style="width:880px;height:650px;padding:5px;">
	<div id="myform">

	</div>
</div>
<div id="tb" style="padding:5px;height:auto">
	<div style="margin-bottom:5px">
		<a href="javascript:tt_add()" class="easyui-linkbutton" iconCls="icon-add" plain="true">增加</a>
		<a href="javascript:tt_remove()" id="btnRemove" disabled="false" class="easyui-linkbutton" iconCls="icon-remove" plain="true">删除</a>
		<a href="javascript:tt_on()" id="btnOn" disabled="false" class="easyui-linkbutton" iconCls="icon-add" plain="true">启用</a>
		<a href="javascript:tt_off()" id="btnOff" disabled="false" class="easyui-linkbutton" iconCls="icon-remove" plain="true">禁用</a>
		<a href="javascript:tt_allon()" class="easyui-linkbutton" iconCls="icon-add" plain="true">全体启用</a>
		<a href="javascript:tt_alloff()" class="easyui-linkbutton" iconCls="icon-remove" plain="true">全体禁用</a>
		<a href="javascript:tt_clear_nandu()" class="easyui-linkbutton" iconCls="icon-remove" plain="true">难度清零</a>
		<a href="javascript:tt_clear_pinlv()" class="easyui-linkbutton" iconCls="icon-remove" plain="true">频率清零</a>
	</div>
	<div>
		标题: <input id="title" type="text" style="width:300px"/>
		<a href="javascript:my_query()" class="easyui-linkbutton" iconCls="icon-search">搜索</a>
		<a href="javascript:display_all()" class="easyui-linkbutton" iconCls="icon-search">显示全部</a>
	</div>
</div>
