【发布时间】:2012-04-14 20:05:43
【问题描述】:
在我的控制器中,我从表中获取一些记录。我正在尝试将记录记录发送到 Js 并显示在我的页面中。
@item_list = TransferDetail.find(:all) - Code to get data from table
@item_list 的输出为
[{:source_id=>8, :object=>"11375810_0", :prefix=>"a",:unit=>"0", :description=>"xxxxx"}, {:source_id=>8, :object=>"11375810_1", :prefix=>"b", :unit=>"0", :description=>"yyyyy"}]
向JS端发送记录
WebView.execute_js("replaceItemList('#{@item_list}')")
在Js端
function replaceItemList(item_list){
alert (item_list);
}
警报结果是这样的
[{:source_id=>8, :object=>"11375810_0", :prefix=>"a",:unit=>"0", :description=>"xxxxx"}, {:source_id=>8, :object=>"11375810_1", :prefix=>"b", :unit=>"0", :description=>"yyyyy"}]
任何人都可以建议我如何在我的视图页面中显示。
我知道如何在下面解析并显示在视图页面中。
"{\"transferType\":\"D\", \"accountNumber\":\"132\", \"employeeId\":\"23\", \"orderedByPhone\":\"2423453453\", \"deliveryInstructions\":\"fdgfghvbn\"}"
可以像这样转换我的@item_list 值或让我知道其他建议
【问题讨论】:
-
我完全看不懂题主。无论如何,发送 JSON 而不是对象数组的字符串表示不是更有意义吗?
-
尝试使用 to_json
WebView.execute_js("replaceItemList('#{@item_list.to_json}')") -
@Joshua:如果任何字段中有单引号,这将失败,因此
.gsub("'", "\\'")也是必要的。
标签: javascript jquery ruby rhomobile