我的jQgrid表中有一个用户列表,现在我想创建子网格,该子网格将列出选定用户的所有项目。
当然,他们有has_many和belongs_to关系。
现在我的代码看起来像这样
jQuery("#datatable").jqGrid({
url: "/user/details",
serializeGridData: function(data) {
var Data = jQuery.extend(data, {
_search: "true",
date_from: function() { return jQuery("#date_from").val(); },
date_to: function() { return jQuery("#date_to").val();
});
return jQuery.param(Data);
},
ajaxGridOptions: {async: true},
ajaxCellOptions: {async: true},
ajaxRowOptions: {async: true},
mtype: "POST",
datatype: "local",
overlay: "false",
jsonReader: {repeatitems: false},
loadonce: false,
pager: '#nav_grid',
rowEdit: true,
colNames:['id','Name','Surname', 'Age', 'Company'],
colModel:[
{name:'id',key:true,hidden:true,width:0,index:'id', sorttype:"int", editable: false, align:'left',search:true},
{name:'name',width:175,sortable:true, align:'left',search:true},
{name:'surname',fixed:true,width:250,sortable:true, align:'left',search:true},
{name:'age',width:0,index:'item_code', sorttype:"int", editable: false, align:'left',search:true},
{name:'company',width:300,sortable:true, align:'left',search:true},
{name:'company',width:100,sortable:true, align:'left',search:true,formatter:'currency'}
],
jQuery('[id^="gs"]').attr("placeholder", "Search...");
},
rownum:500,
rowNum:500,
rowList:[500, 1000],
shrinktofit: true,
forcefit: false,
autowidth:true,
sortname: 'projects',
sortorder: "desc",
height: "380",
ignoreCase: true,
viewrecords: true,
caption:"",
subGrid: true,
subGridRowExpanded: function(subgrid_id, row_id) {
var user_id = jQuery('#data_table').getRowData(row_id)['user_id']
var subgrid_table_id;
subgrid_table_id = subgrid_id + "_t";
jQuery("#"+subgrid_id).html("<table id='"+subgrid_table_id+"' class='scroll'></table><div id='"+ "p_"+subgrid_table_id +"' class='scroll'></div>");
jQuery("#"+subgrid_table_id).jqGrid({
url:"/user/project_details",
datatype: "json",
jsonReader: {repeatitems: false},
postData: data,
colNames: ['id','Category','Supplier', 'Item Code', 'Name', 'Price', '#','Unit', 'Total Cost (no tax)', 'Total Tax', 'Charges', 'Credits', 'Grand Total'],
colModel: [
{name:'project_id',key:true,hidden:true,width:0,index:'id', sorttype:"int", editable: false, align:'left',search:true},
{name:'project_name',width:175,sortable:true, align:'left',search:true},
{name:'email',fixed:true,width:250,sortable:true, align:'left',search:true},
{name:'user_id',width:0,index:'item_code', sorttype:"int", editable: false, align:'left',search:true},
],
height: '100%',
sortname: 'num',
sortorder: "asc",
shrinktofit: true,
forcefit: false,
autowidth:true,
rownum:500,
rowNum:500,
loadonce:true,
rowList:[500,1000],
viewrecords: true,
pager:"p_"+subgrid_table_id
});
问题是如何找到current_row(选定的行),然后列出与选定用户相关的所有项目。
在用户控制器中(这是应该在子网格中列出的项目)
def index
@projects = Project.joins(:user).where(:projects => {project_id: selected_user.id(?))
end
我尝试过类似的操作,但是我不知道如何命名selected_user.id,有什么方法可以解决这个问题?
或者如果这根本不好,您将如何建议实现这一目标?
答案 0 :(得分:0)
据我了解的问题,您将需要在subGridOptions中设置selectOnExpand选项才能获取当前选定的行。参见documentation here
subGrid: true,
subGridOptions : {
selectOnExpand : true
}
subGridRowExpanded: function(subgrid_id, row_id) {
...
}