我需要一些帮助。
我有两个模型,Country
和Buy
,其间有Many to many relationship
。我使用 Yajra 包中的Laravel Datatables来显示所有国家/地区和购买的表格。
国家
Schema::create('countries', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
收购
Schema::create('buys', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('code_one');
$table->integer('code_two');
$table->timestamps();
});
buy_country
Schema::create('buy_country', function (Blueprint $table) {
$table->increments('id');
$table->integer('buy_id');
$table->integer('country_id');
$table->timestamps();
});
模型
class Country extends Model
{
public function buys(){
return $this->belongsToMany('App\Buy', 'buy_country');
}
}
class Buy extends Model
{
public function countries(){
return $this->belongsToMany('App\Country', 'buy_country');
}
}
我想在第一行显示国家/地区名称,在子行上显示购买表中的产品。
无论有没有Yajra包,我怎么能这样做?
答案 0 :(得分:0)
对于这种情况,我认为您应该使用yajra数据表来填充表行的子项。
您可以使用jquery Datatables Child Rows并使用ajax填充其下的数据。 基于此处:https://datatables.net/examples/api/row_details.html
HTML code:
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th></th> <!-- Left the head blank for plus icon -->
<th>Country Name</th>
</tr>
</thead>
<tfoot>
<tr>
<th></th> <!-- Left the footer blank for plus icon -->
<th>Country Name</th>
</tr>
</tfoot>
</table>
jQuery Datatables Part:
table = $('#example').DataTable({
processing: true,
serverSide: true,
"order": [[ 2, "asc" ]],
columns: [
{
"className": 'details-control',
"orderable": false,
"data": null,
"defaultContent": '',
'render': function (){
return "<i class='fa fa-plus-circle'></i>";
}
},
{ //Hiding column country id for sorting purpose
"data": "country_id",
"searchable": false,
"orderable": true,
"visible": false,
},
{ data: 'country_name', name: 'country_name' }
],
ajax: function (data, callback, settings) {
settings.jqXHR = $.ajax( {
"dataType": 'json',
"url": "URL", // Link to Select All from from country
"type": "GET",
"data": data,
"success": callback,
"error": function (e) {
console.log(e.message);
}
});
}
});
function format ( d ) {
// `d` is the original data object for the row
// This is when you populate data from buy table
var childContent = '';
$.ajax({
url: "URL", //URL to select from buy_country using country_id and get data from buy (use join table)
data: {country_id: d.country_id},
type:'POST',
dataType: "json"
success: function( data ) {
childContent += '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">' ;
$.each(data, function (i, elem) {
childContent += '<tr>';
childContent += '<td>'+elem.name+'</td>';
childContent += '<td>'+elem.code1+'</td>';
childContent += '<td>'+elem.code2+'</td>';
childContent += '</tr>';
});
childContent += '</table>';
}
});
return childContent;
}
$('#example tbody').on('click', 'tr.even, tr.odd', function (e) {
var tr = $(this).closest('tr');
var row = table.row( tr );
if ( row.child.isShown() ) {
// This row is already open - close it
$(this).find('td:first-child i').removeClass("fa fa-minus-circle");
$(this).find('td:first-child i').addClass("fa fa-plus-circle");
row.child.hide();
tr.removeClass('shown');
}
else {
// Open this row
$(this).find('td:first-child i').removeClass("fa fa-plus-circle");
$(this).find('td:first-child i').addClass("fa fa-minus-circle");
row.child( format(row.data()) ).show(); //When open run formt() function.
tr.addClass('shown');
}
});