简体   繁体   English

如何在jqGrid中访问Ajax调用数据

[英]How to Access ajax call data in jqGrid

I have a ajax call to fetch data from server for the jqGrid in my rails application. 我有一个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",
      },  

Here's my controller code: 这是我的控制器代码:

   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
                    }

I want to access the value of @buttons from controller but unable to guess how to pass and access in my view so that i can put conditions like for ex... 我想从控制器访问@buttons的值,但无法在我的视图中猜测如何传递和访问,以便我可以将条件设​​置为ex ...

<% if @buttons == 1 %>
      colNamesData.splice(1,1);
      colModelData.splice(1,1);
  <% end %>

thanx 谢谢

I am not Ruby on Rail developer, but if you work with jqGrid with any server technology, the best way would be to send only data and no HTML markup in the server response. 我不是Ruby on Rail开发人员,但是如果您使用具有任何服务器技术的jqGrid,最好的方法是在服务器响应中发送数据而不发送HTML标记 jqGrid has custom formatter which allows you to construct any HTML fragmant which will be placed in the column. jqGrid具有自定义格式化程序 ,该格式化程序允许您构造将放置在该列中的任何HTML fragmant。 Instead of the current HTML fragment you could just send from the server the boolean value timesheet.deletable (as "true" and "false" or "1" and "0"). 可以从服务器发送布尔值timesheet.deletable (如“ true”和“ false”或“ 1”和“ 0”)来代替当前的HTML片段。 Inside of the custom formatter you have access to the whole row of data send from the server per rowObject parameter. 在自定义格式化程序内部,您可以访问每个rowObject参数从服务器发送的整行数据。 Depend on the format of the data which you return from the server you should use named properties like rowObject.creation_date or array index rowObject[2] . 根据从服务器返回的数据格式,应使用诸如rowObject.creation_date或数组索引rowObject[2]类的命名属性。

One more remark. 再说一遍。 I don't recommend you to use any special characters in the name property of colModel . 我不建议您在colModelname属性中使用任何特殊字符。 Instead of name:'projects.name' you should use name:'name' or name:'projects_name' . 取而代之的name:'projects.name'你应该使用name:'name'name:'projects_name' If the dot character will be do placed in the property names of the row of JSON data (it seems me not in your case) you should use jsonmap: 'projects.name' (see here ). 如果将点字符放置在JSON数据行的属性名称中(似乎不是我的情况),则应使用jsonmap: 'projects.name' (请参见此处 )。

The last remark: sorttype in the colModel will be used only for local data and will be ignored in case of datatype:'json' . 最后一句话: sorttypecolModel将仅用于本地数据,并将在的情况下,被忽略datatype:'json'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM