我有一个ajax调用,用于从我的rails应用程序中的jqGrid获取服务器的数据。
$(document).ready(function() {
colNamesData = ['Project', 'Activity', 'Description', 'Hours', 'Date','']
colModelData = [
{name:'projects.name',index:'projects.name', width:130, sorttype:"text"},
{name:'creation_date',index:'creation_date', width:130, sorttype:"date"},
{name:'buttons', index:'buttons', width:270, align:'center', search:false}
]
$("#time_sheet_table").jqGrid({
datatype: 'json',
colNames: colNamesData,
colModel: colModelData,
url: 'timesheets/timesheet_index_jqgrid?table_name = timesheet_index_jqgrid&',
mtype: "Get",
jsonReader: {
root: "rows",
page: "page",
total: "total",
records: "records",
id: "id",
repeatitems: true,
cell: 'cell',
button: "buttons",
},
这是我的控制器代码:
if timesheet.deletable?(@user)
@buttons = 1
buttons += "<div style='margin-right:55px'>"
buttons += "<a class='button delete-button' href='#' onclick=\"jQuery('#time_sheet_table').jqGrid('delGridRow', #{timesheet.id}, {});\"><span class='link'>Delete</span></a>" + "</div>"
end
@cell << {
:id => timesheet.id,
:cell => [timesheet.project_name,timesheet.creation_date.strftime("%Y-%m-%d"),buttons]
}
end
list_of_resources = {
"total" => total_page(timesheets.count,grid_id),
"page" => session[:page][:time_sheet_table],
"records" => timesheets.count,
"rows" => @cell
}
我想从控制器访问@buttons的值,但无法猜测如何在我的视图中传递和访问,以便我可以像以前那样设置条件......
<% if @buttons == 1 %>
colNamesData.splice(1,1);
colModelData.splice(1,1);
<% end %>
感谢名单
答案 0 :(得分:0)
我不是Ruby on Rail开发人员,但如果您使用任何服务器技术使用jqGrid,最好的方法是在服务器响应中发送仅数据而不发送HTML标记。 jqGrid有custom formatter,它允许你构造任何将放在列中的HTML片段。您可以从服务器发送布尔值timesheet.deletable
(作为“true”和“false”或“1”和“0”),而不是当前的HTML片段。在自定义格式化程序内部,您可以访问每个rowObject
参数从服务器发送的整行数据。根据您从服务器返回的数据格式,您应该使用命名属性,如rowObject.creation_date
或数组索引rowObject[2]
。
还有一句话。我不建议您使用name
colModel
属性中的任何特殊字符。您应该使用name:'projects.name'
或name:'name'
而不是name:'projects_name'
。如果点字符将放在JSON数据行的属性名称中(似乎我不是你的情况),你应该使用jsonmap: 'projects.name'
(见here)。
最后一句话:sorttype
中的colModel
将仅用于本地数据,datatype:'json'
会被忽略。