Playing more on JQgrid plugin from 2dconcept, I faced a strange issue. Sometimes it skips some pages.
For example if you have total 15 pages, and you navigate till 6th page and then you hit next page button on the grid, it will not show you the 7th page! you have to click next page button once more to see the 8th page. So where the 7th page is gone?
No, it's not invisible, it's all hidden in json response.
When I saw the response for page 7 (using firbug), it was coming well, but the grid was not showing the 7th page at all. I dug into the library file more and figured out that, if in the response you have a double quote, it will not display that page.
Say you have this json response:
{"page":"1","total":1,"records":"1","rows":[{"id":"7","cell":["gourav tiwari hel"lo!","this","should","be right!"]}]}
See the double quote in the response.
I tried using to_json library method, but it actually removes all the double quotes, even the necessary ones as well. So not a good idea. I extended to_jqgrid_json method like this:
module JqgridJson
def to_jqgrid_json(attributes, current_page, per_page, total)
json = %Q({"page":"#{current_page}","total":#{total/per_page.to_i+1},"records":"#{total}")
if total > 0
json << %Q(,"rows":[)
each do |elem|
elem.id ||= index(elem)
json << %Q({"id":"#{elem.id}","cell":[)
couples = elem.attributes.symbolize_keys
attributes.each do |atr|
value = get_atr_value(elem, atr, couples)
value = value.is_a?(String) ? value.gsub(/"/, '\"') : value # added this line
json << %Q("#{value}",)
end
json.chop! << "]},"
end
json.chop! << "]}"
else
json << "}"
end
end
end
And the response become this:
{"page":"1","total":1,"records":"1","rows":[{"id":"7","cell":["gourav tiwari hel\"lo!","this","should","be right!"]}]}
No invisible page in JQGrid anymore!
For example if you have total 15 pages, and you navigate till 6th page and then you hit next page button on the grid, it will not show you the 7th page! you have to click next page button once more to see the 8th page. So where the 7th page is gone?
No, it's not invisible, it's all hidden in json response.
When I saw the response for page 7 (using firbug), it was coming well, but the grid was not showing the 7th page at all. I dug into the library file more and figured out that, if in the response you have a double quote, it will not display that page.
Say you have this json response:
{"page":"1","total":1,"records":"1","rows":[{"id":"7","cell":["gourav tiwari hel"lo!","this","should","be right!"]}]}
See the double quote in the response.
I tried using to_json library method, but it actually removes all the double quotes, even the necessary ones as well. So not a good idea. I extended to_jqgrid_json method like this:
module JqgridJson
def to_jqgrid_json(attributes, current_page, per_page, total)
json = %Q({"page":"#{current_page}","total":#{total/per_page.to_i+1},"records":"#{total}")
if total > 0
json << %Q(,"rows":[)
each do |elem|
elem.id ||= index(elem)
json << %Q({"id":"#{elem.id}","cell":[)
couples = elem.attributes.symbolize_keys
attributes.each do |atr|
value = get_atr_value(elem, atr, couples)
value = value.is_a?(String) ? value.gsub(/"/, '\"') : value # added this line
json << %Q("#{value}",)
end
json.chop! << "]},"
end
json.chop! << "]}"
else
json << "}"
end
end
end
And the response become this:
{"page":"1","total":1,"records":"1","rows":[{"id":"7","cell":["gourav tiwari hel\"lo!","this","should","be right!"]}]}
No invisible page in JQGrid anymore!
3 comments:
Great job !
Thanks for publishing this ...
I have tried this with both 1.8.7 and 1.9.1p376, and both returned this:
>> {:a => 'asd"f'}.to_json
=> "{\"a\":\"asd\\\"f\"}"
I don't think to_json would do any harm to the code.
I tried with ruby 1.8.6 and it was actually removing the double quote, so I had to the hack. I am glad to hear that 1.8.7 onwards, this issue is resolved.
Post a Comment