1

I have a ajax call to fetch data from server for the jqGrid in my rails application.

$(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...

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

thanx

Haim Evgi
  • 123,187
  • 45
  • 217
  • 223
Bijendra
  • 9,467
  • 8
  • 39
  • 66

1 Answers1

0

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. jqGrid has custom formatter which allows you to construct any HTML fragmant which will be placed in the column. 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"). Inside of the custom formatter you have access to the whole row of data send from the server per rowObject parameter. 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].

One more remark. I don't recommend you to use any special characters in the name property of colModel. Instead of name:'projects.name' you should use name:'name' or 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).

The last remark: sorttype in the colModel will be used only for local data and will be ignored in case of datatype:'json'.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • @oleg...thanx for the response..actually i have reduced the code to the maxm limit so it's seems to be unclear.Projects.name is used for project table.sorting and searching are handled at server side.. – Bijendra Apr 26 '11 at 11:11
  • @GhostRider: For sorting will be used `index` and not the `name` property. To access the data (what you asked) you should use together with the `rowObject` parameter of the custom formatter the `data` parameter of `loadComplete` event handler. – Oleg Apr 26 '11 at 11:20