遍历子行中的关联模型(DataTables + Rails)

时间:2019-06-23 16:50:41

标签: ruby-on-rails ruby datatables

我想遍历数据表子行中的关联模型。

课程数据显示良好。但是参与破坏了数据表。

我使用gem'ajax-datatables-rails'。

型号:

  • 课程(有很多参加)
  • 参与(属于课程)

JSON看起来像这样:

{"data":[ {
  "courses": {
     "runde":"2",
     "kursnavn":"SMÅBARN",
     "alder":"1-5år",
     "sted":"test",
     "startdato":"16.06.2019",
     "slutdato":"16.06.2019",
     "tid":"12:12-12:23",
     "delt":"0",
     "tilgang":"Tidligere kurs",
     "status":"Tidligere kurs",
     "id":"2"
     },
   "participations":[{
     "status_translated":"Venter på godkjenning"},{
     "status_translated":"Venter på godkjenning"},{
     "status_translated":"Venter på godkjenning"
    }
  ]
}

在JS中

$(document).ready(function() {
  var table = $('#datatable').DataTable( {
    "processing": true,
    "ajax": {
      "url": $('#datatable').data('source'),

    "class":          'details-control',
    "orderable":      false,
    "data":           null,
    "defaultContent": ''
    },
    "columns": [
          {"data": "courses.runde"},
          {"data": "participations.status_translated"},
    ]
  });
});

“ ajax-datatables-rails” gem中的course_datatable.rb片段

def get_raw_records
  Participation.joins(:courses)
end

这是正确的吗?


我在javascript中尝试过此方法,而且我认为这很糟

function format ( d ) {

  $.each($(d.participations),function(key,value){
      trs+='<tr><td>'+d.participations.status_translated+'</td><tdr>'+d.participations.status_translated+'</td></tr>';
      //loop through each product and append it to trs and am hoping that number of price 
      //values in array will be equal to number of products
  });

  return '<table class="table table-border table-hover">'+
           '<thead>'+
              '<th>Participation</th>'+
                '<th>ID</th>'+  
             '</thead>+'
           '<tbody>'+trs+'</tbody>'+
           '</table>';
});

错误:

Error: "TypeError: headerCells[i] is undefined"

0 个答案:

没有答案