简体   繁体   English

Rails 3.1 simple_datatables

[英]Rails 3.1 simple_datatables

I have been looking at Simple Datatables but the documentation and example provided are not very clear. 我一直在看简单数据表,但是提供的文档和示例不是很清楚。 It appears to implement jsonify and utilize a template handler but I am not sure. 它似乎实现了jsonify并利用了模板处理程序,但我不确定。 Also the provided code does not compile; 提供的代码也不会编译; is the author using some other gem to parse the views? 作者是否使用其他宝石来解析视图? For example, it says to put this in your search view... 例如,它说将其放入您的搜索视图中...

@products.each do |product|
  json << [product.name, product.manufacturer.name]
end

which I assuming has to be translated to... 我认为必须将其翻译为...

<%= @products.each do |product| %>
 <% json << [product.name, product.manufacturer.name] %>
<% end %>

but it comes back and says 'undefined local variable or method 'json' for class blah blah' 但它回来并说“类blah blah的未定义局部变量或方法'json'”

same type of deal for the index view. 索引视图的交易类型相同。 The example says to just throw this in your view, 该示例说,只是将此添加到您的视图中,

%table#products
  %thead
    %tr
      %th= Product.human_attribute_name :name
      %th= Product.human_attribute_name :manufacturer

  %tbody

but if I do that it simply puts that raw text in my view. 但是如果我这样做,它只会将原始文本放入我的视图。 Again, it seems like the author is using some gem to parse his views but not referencing it in the docs. 同样,作者似乎正在使用一些gem来解析他的观点,但未在文档中引用它。 Any thoughts? 有什么想法吗? Thanks! 谢谢!

Edit: 编辑:
Ok, so I got the table to load correctly by calling it as a table... duh... 好的,因此我通过将其称为表来正确加载该表... duh ...

<table class="products">
  <thead>
  <tr>
    <th>Name</th>
    <th>Manufacturer</th>

 </thead>
 <tbody></tbody>
 </table>

but it is now telling me it has no data to load. 但现在它告诉我没有要加载的数据。 I checked the route and /search works but /search.datatables comes back telling me there is no template. 我检查了路由,/ search工作正常,但/search.datatables回来告诉我没有模板。 Thanks again, 再次感谢,

The author isn't using any other gem as far as I can tell. 据我所知,作者没有使用任何其他宝石。

@products.each do |product|
   json << [product.name, product.manufacturer.name]
end

You need to put the above code into a search.datatables.jsonify file instead of just search.datatables. 您需要将上面的代码放入search.datatables.jsonify文件中,而不仅仅是search.datatables。

Also you should change the class="products" to id="products" in your table markup. 另外,您还应该在表标记中将class =“ products”更改为id =“ products”。 You will also want to put the corresponding id's into the th tags too. 您还需要将相应的ID放入th标签。

Finally the javascript was a bit mangled too, try the following 最后,JavaScript也有点混乱,请尝试以下操作

$("#products").dataTable( {
   "sAjaxSource"     : "/products/search.datatables",
   "aaSorting"       : [[0, 'asc']],
   "aoColumns"       : [
      {"sName":"name"},
      {"sName":"manufacturer_name"},
],
"bServerSide"     : true,
"fnServerData"    : simpleDatatables
});

Hope this helps. 希望这可以帮助。

索引视图正在使用haml gem。

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

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