jQuery DataTables Master/Details(子行)作为laravel partial view


jQuery DataTables Master/Details (child rows) as a laravel partial view

我有一个Laravel应用程序,我使用jQuery数据表(yajra/DataTables Laravel插件)来显示一些记录。我还使用3个子行来显示更详细的信息。在子行中,我显示了一些额外的信息,一个包含内容的年度数据的图表,以及过去6个月的记录值。问题是,我不能像填充第一行那样填充最后2个子行,因为我不能将所有数据放入1个查询中。

这里是提供给DataTable的控制器方法

public function getRowDetails()
{
    return view('reports.creates', compact('data'));
}
public function getRowDetailsData()
{
    $kpi = $this->getUserActiveKpi();
    $data = DB::table('reports')
              ->orderBy('month','desc')
              ->groupBy('kpi_id')
              ->take(5)
              ->get();
    return Datatables::of($kpi, $data)
              ->make(true);
}
private function getUserActiveKpi(){
    $user = Auth::user();
    $kpis = DB::table('kpis')
              ->where('kpi_status',1)
              ->where('responsible_user', $user->id);
    return $kpis;
}

初始化脚本:

$(document).ready(function () {
    var table;
    table = $('#monthly_table').DataTable({
        processing: true,
        serverSide: true,
        dom: "fr<'clear'>Ttip",
        ajax: '{{ url("reports/row-details-data") }}',
        tableTools: {.....},
        columns: [.....],
        order: [[1, 'asc']]
    });
});
以下是返回子行数据的函数:
var kpi;
function kpi_info(d) {
    // `d` is the original data object for the row
    return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">'+
    '<tr>' +
    '<td>KPI:</td>' +
    '<td>' + d.kpi_code + '</td>' +
    '</tr>' +
    '<tr>' +
    '<td>Workload:</td>' +
    '<td>' + d.kpi_workload + '</td>' +
    '</tr>' +
    '<tr>' +
    '<td>KPI Description:</td>' +
    '<td>'+ d.kpi_description + '</td>' +
    '</tr>' +
    '</table>'
}
function kpi_values(d) {
    // `d` is the original data object for the row
    kpi = d.id;
    // it returns the id, here I want to return the partial view 
    // that contains the data using the *kpi* as a parameter
    return kpi; 
}
function kpi_graph(d) {
    kpi = d.id;
    // it returns the id, here I want to return the partial view that
    // contains the graph using the *kpi* as a parameter
    return kpi; 
}

,下面是显示它们的函数:

$('#monthly_table tbody').on('click', 'td.details-control', function () {
        var tr = $(this).closest('tr');
        var row = table.row(tr);
        if (row.child.isShown()) {
            // This row is already open - close it
            row.child.hide();
            tr.removeClass('shown');
        }
        else {
            // Open this row
            row.child(kpi_data(row.data())).show();
            tr.addClass('shown');
        }
    });

现在我要实现的是,当我单击显示子节点的链接之一时,将kpi参数提供给以下控制器方法

public function data($id){
    $data = DB::table('reports')
              ->where('kpi_id',$id)
              ->orderBy('month','desc')
              ->take(5)
              ->get();
    return view('reports.data', compact('data'));
}

如果还有什么需要知道的,请告诉我,感谢所有的帮助

编辑

通过说I cannot put all the data inside 1 query,我的意思是我必须结合3个查询来做到这一点。第一个是:

DB::table('kpis')
    ->where('kpi_status',1)
    ->where('responsible_user', $user->id);

第二个是:

DB::table('reports')
    ->where('kpi_id',$id)
    ->orderBy('month','desc')
    ->take(5)
    ->get();

第三个是

DB::table('reports')
    ->where('reports.is_validated',1)
    ->where('reports.year',$current_year)
    ->orderBy('month','asc')
    ->get();

我想不出一个查询可以一次得到所有这些数据。

注意

有另一个查询与上一个带有$prev_year参数的查询类似。

编辑2

Schema::create('kpis', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('kpi_code');
        $table->string('kpi_description');
    });
Schema::create('reports', function(Blueprint $table)
    {
        $table->increments('id');
        $table->integer('kpi_id')->unsigned()->nullable(); 
        //fk kpi -> values
        $table->float('value');
        $table->integer('month',false,false,'2');
        $table->integer('year',false,false,'4');
    });
Schema::table('reports', function($table){
        $table->foreign('kpi_id')->references('id')->on('kpis');
    });

这段代码需要编辑,但希望您能理解。

我会看雄辩:关系>定义关系>一对多以及数据演示网站页面,EloquentController.php - Eloquent . gethasmany .title.

<<p> Kpi模型/strong>
<?php namespace App;
use Illuminate'Database'Eloquent'Model;
class Kpi extends Model {
  /**
   * Get the reports for the kpi.
   */
  public function reports()
  {
    return $this->hasMany('App'Report')
        ->orderBy('month','desc');
  }
  /**
   * Get the Kpis for a specific user
   */
  public function scopeForUser($query, $user_id)
  {
        $query->where('kpi_status',1)
      ->where('responsible_user', $user_id);
  }
}
<<p> 报告模型/strong>
<?php namespace App;
use Illuminate'Database'Eloquent'Model;
class Report extends Model {
    /**
     * A report belongs to a kpi
     *
     * @return mixed
     */
    public function kpi()
    {
        return $this->belongsTo('App'Kpi');
    }
    /**
     * Get the reports for a specific year
     */
    public function scopeForYear($query, $year)
    {
        $query->where('is_validated',1)
            ->where('year',$year)
            ->orderBy('month','asc');
    }
}
控制器

public function getRowDetails()
{
    return view('reports.creates');
}
public function getRowDetailsData()
{
    if (Auth::guest())
    {
        return ['status'=>'error', 'nessage' => 'Unauthorized user.'];
    }
    $user_id = Auth::user()->id;
    $kpis = Kpi::with(['reports' => function($q){
        $q->take(5);
    })->forUser($user_id)->get();
  return Datatables::of($kpis)
           ->make(true);
}

->take(5)可能将您的查询限制为每个KPI总共只有5条记录,而不是5个报告。如果是这种情况,请查看这篇文章。

按注释更新

看起来你使用的插件不提供嵌套循环

你可能会做这样的事情,但我从来没有使用过这个插件,所以我无法给你更多的信息。

columns: [
  {data: 'kpi_code', name: 'kpi'},
  {data: 'kpi_workload', name: 'workload'},
  {data: 'kpi_description', name: 'description'},
  {data: 'reports[0].name', name: 'report_1'},
  {data: 'reports[1].name', name: 'report_2'},
  {data: 'reports[2].name', name: 'report_3'},
  {data: 'reports[3].name', name: 'report_4'},
  {data: 'reports[4].name', name: 'report_5'}
]