<script type="text/javascript"> jQuery(document).ready(function(){ var lastcell; jQuery("#grid").jqGrid({ url:'{{ json_data_handler }}?year={{ get_param_year }}&month={{ get_param_month }}&project_id={{ get_param_project_id }}',datatype: "json",mtype: 'GET',colNames:['hour_record_pk','project_pk','weekday','date','sum','description'],colModel:[ {name:'hour_record_pk',index:'hour_record_pk',width:55,sortable:false,editable:true,editoptions:{readonly:true,size:10}},{name:'project_pk',index:'project_pk',{name:'weekday',index:'weekday',width:300,editable:false,sortable:false},{name:'date_str',index:'date_str',editoptions:{readonly:true}},{name:'sum',index:'sum',width:100,sortable:true,editrules:{number:true,minValue:0,maxValue:24,required:true}},{name:'description',index:'description',width:600,},],jsonReader : { repeatitems:false },rowNum:31,rowList:[10,20,31],//pager: jQuery('#gridpager'),sortname: 'id',viewrecords: true,sortorder: "asc",width: 800,height: 750,caption:"Hour Record Overview",reloadAfterEdit: true,//seems to have no effect reloadAfterSubmit: true,//seems to have no effect onSelectRow: function(id){ if(id && id!==lastcell){ jQuery('#grid').jqGrid('restoreRow',lastcell); jQuery('#grid').jqGrid('editRow',id,true); lastcell=id; } },editurl:"{% url set_hour_record_json_set %}" }).navGrid('#gridpager'); }); function reload(result) { $("#grid").trigger("reloadGrid"); }
我创建了一个重新加载的方法,但我不知道该放在哪里。我试过:
jQuery('#grid').jqGrid('editRow',true,reload());
但是当用户点击一行时,这已经被调用了。
上面的代码允许用户点击一行,当按下输入时,数据被提交并且更新或创建记录。
在用户创建新对象后,我必须重新加载网格,因为我需要新创建的对象的对象ID,以便进一步编辑此行。
编辑:解决方案:
onSelectRow: function(row_id){ if(row_id != null) { if(row_id !== last_selected_row) { jQuery('#grid').jqGrid('restoreRow',last_selected_row); jQuery('#grid').jqGrid('saveRow',row_id) .editRow(row_id,false,reload); last_selected_row = row_id; } else { last_selected_row=row_id; } } },
更新:供以后参考。从js网格开始的所有用户也可以看看Slickgrid,因为我从jqgrid切换到slickgrid,只能推荐它。
解决方法
jQuery("#grid_id").jqGrid('editRow',rowid,keys,oneditfunc,succesfunc,url,extraparam,aftersavefunc,errorfunc,afterrestorefunc);
oneditfunc: fires after successfully
accessing the row for editing,prior
to allowing user access to the input
fields. The row’s id is passed as a
parameter to this function.succesfunc: if defined,this function
is called immediately after the
request is successful. This function
is passed the data returned from the
server. Depending on the data from
server; this function should return
true or false.aftersavefunc: if defined,this
function is called after the data is
saved to the server. Parameters passed
to this function are the rowid and the
response from the server request.
在您的情况下,如果要在保存行之后重新加载网格,则对editRow方法的调用应如下所示。
jQuery('#grid').jqGrid("editRow",'',reload)
我已经分配了你的重载功能,它为’aftersavefunc’参数重新加载网格
重新加载方法本身应该定义如下
function reload(rowid,result) { $("#grid").trigger("reloadGrid"); }