Skip to content

Commit 61292d8

Browse files
committed
Add support for 'rows' column type
1 parent f338ca3 commit 61292d8

File tree

5 files changed

+105
-0
lines changed

5 files changed

+105
-0
lines changed

src/Controllers/BaseController.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,4 +468,38 @@ public function pivot($columnId, $column)
468468
}
469469
return $response;
470470
}
471+
472+
// Return the line input for a many to many (pivot) relationship
473+
public function rows($columnId, $column)
474+
{
475+
$data = $this->getModelData($column);
476+
$response = '<table class="rows" id="input_' . $columnId . '">';
477+
$response .= '<tr class="template">';
478+
foreach ($column['columns'] as $columnId2 => $opt) {
479+
$response .= '<td>';
480+
if (empty($opt['type'])) {
481+
$opt['type'] = 'string';
482+
}
483+
484+
if ($opt['type'] == 'string' || $opt['type'] == 'password' || $opt['type'] == 'date' || $opt['type'] == 'datetime' || $opt['type'] == 'number') {
485+
$response .= '<input class="' . ($opt['type'] == 'date' ? 'datepicker' : '') . ($opt['type'] == 'datetime' ? 'datetimepicker' : '' ) . '" type="' . ($opt['type']=='string' || $opt['type'] == 'date' || $opt['type'] == 'datetime' ? 'text' : $opt['type']) . '" name="'. $columnId . '_' . $columnId2 . '[]" data-column="' . $columnId . '_' . $columnId2 . '" placeholder="' . $this->locale('placeholder', $opt, '') . '">';
486+
} elseif ($opt['type'] == 'foreign') {
487+
$response .= $this->foreign($columnId . '_' . $columnId2.'[]', $opt, false);
488+
} else {
489+
$response .= $opt['type'];
490+
}
491+
$response .= '</td>';
492+
}
493+
if ($this->can('delete')) {
494+
$response .= '<td><button type="button" data-confirm="' . trans('admin::base.deleteconfirm') . '" class="pivot-delete button is-red"><i class="fa fa-trash"></i></button></td>';
495+
}
496+
$response .= '</tr>';
497+
$response .= '<tr>';
498+
foreach ($column['columns'] as $col => $opt) {
499+
$response .= '<th>' . $this->locale('title', $opt, $col) . '</th>';
500+
}
501+
$response .= '</tr>';
502+
$response .= '</table>';
503+
return $response;
504+
}
471505
}

src/Controllers/ModelController.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,22 @@ private function save($model, Request $request)
2323

2424
$sync = [];
2525
$morph = [];
26+
$row = [];
2627
foreach($this->columns() as $columnId => $column) {
2728
if (isset($column['type']) && $column['type'] == 'pivot') {
2829
$sync[$column['model']] = $request[$columnId];
2930
if (!empty($column['morph'])) {
3031
$morph[$column['model']] = $column['morph'];
3132
}
33+
} elseif (isset($column['type']) && $column['type'] == 'rows') {
34+
$row[$column['model']]['self'] = $column['self'];
35+
foreach($column['columns'] as $columnId2 => $opt) {
36+
$r = $request[$columnId . '_' . $columnId2];
37+
array_shift($r);
38+
foreach($r as $key => $value) {
39+
$row[$column['model']][$key][$columnId2] = $value;
40+
}
41+
}
3242
} elseif (isset($column['type']) && $column['type'] == 'array') {
3343
// If column is of type array json decode it
3444
$model[$columnId] = json_decode($request[$columnId], true);
@@ -57,6 +67,21 @@ private function save($model, Request $request)
5767

5868
$model->save();
5969

70+
foreach($row as $foreign => $data) {
71+
$fm = new $foreign;
72+
$self = $data['self'];
73+
unset($data['self']);
74+
$fm::where($self, $model->id)->delete();
75+
foreach($data as $row) {
76+
$fm = new $foreign;
77+
$fm->$self = $model->id;
78+
foreach($row as $column => $value) {
79+
$fm->$column = $value;
80+
}
81+
$fm->save();
82+
}
83+
}
84+
6085
foreach($sync as $foreign => $values) {
6186
if (isset($morph[$foreign])) {
6287
$model->morphToMany($foreign, $morph[$foreign])->sync($values);
@@ -114,6 +139,11 @@ public function show($slug, $id)
114139
if ($column['type'] == 'array') {
115140
$row[$columnId] = json_encode(json_decode($row[$columnId]), JSON_PRETTY_PRINT);
116141
}
142+
// If column type is rows return those rows
143+
if ($column['type'] == 'rows') {
144+
unset($row['"'.$columnId.'"']);
145+
$row[$columnId] = $this->model()::findOrFail($id)->hasMany($column['model'])->get(array_keys($column['columns']))->toArray();
146+
}
117147
// If column type is pivot return matching ids
118148
if ($column['type'] == 'pivot') {
119149
unset($row['"'.$columnId.'"']);

src/css/base.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,17 @@ nav li:hover > ul > li {max-height:40px}
187187
#editview UL.input_images > .button.add:not(:first-child) {width:45px;height:90px;opacity:0.3}
188188
#editview UL.input_images > .button.add:not(:first-child):hover {opacity:1}
189189
#editview UL.input_images.image > .button.add:not(:first-child) {display:none}
190+
#editview .content TABLE.rows {border-spacing:0;border-collapse:collapse;max-width:100%}
191+
#editview .content TABLE.rows TR.template {display:none}
192+
#editview .content TABLE.rows TR:last-child TH {display:none}
193+
#editview .content TABLE.rows TH,
194+
#editview .content TABLE.rows TD {font-weight:inherit;text-align:left;padding:0 0 3px 3px}
195+
#editview .content TABLE.rows TH:first-child,
196+
#editview .content TABLE.rows TD:first-child {padding-left:0}
197+
#editview .content TABLE.rows SELECT {width:100%}
198+
#editview .content TABLE.rows .button {padding:3px 7px 4px}
199+
#editview .content TABLE.rows INPUT {padding:1px 4px 2px}
200+
190201
#editview .content TABLE.array {border-spacing:0;border-collapse:collapse}
191202
#editview .content TABLE.array TD:first-child {color:rgba(0,0,0,0.5);padding-right:0.5em;font-size:0.95em}
192203
#editview .content TABLE.array TD:first-child:first-letter {text-transform:uppercase}

src/js/model.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ function modelShow(slug, id) {
170170
$.ajax(slug + '/' + id, {
171171
cache: false,
172172
}).done(function (data, status, xhr) {
173+
$('#editview .rows .data').remove();
173174
for (i in data) {
174175
if (i.substr(0, 7) == '_pivot.') {
175176
$('#editview input[type=checkbox].pivot-' + i.substr(7)).prop('checked', false);
@@ -187,6 +188,11 @@ function modelShow(slug, id) {
187188
});
188189
} else if ($('#input_' + i).attr('type') == 'checkbox') {
189190
$('#input_' + i).prop('checked', data[i] == true);
191+
} else if ($('#input_' + i).is('TABLE') && $('#input_' + i).hasClass('rows')) {
192+
var rowData = data[i];
193+
for (n in rowData) {
194+
modelAddLine(slug, $('#input_' + i), rowData[n], i)
195+
}
190196
} else {
191197
$('#input_' + i).val(data[i]).change();
192198
}
@@ -512,6 +518,22 @@ function hideColumns(t) {
512518
}
513519
}
514520

521+
function modelAddLine(slug, element, data, column) {
522+
var tr = $(element).find('TR.template').clone().removeClass('template').addClass('data');
523+
tr.appendTo($(element)).find('.pivot-delete').click(function () {
524+
$(this).parent().parent().remove();
525+
});
526+
if (data) {
527+
for (n in data) {
528+
tr.find('INPUT[data-column='+column+'_'+n+']').val(data[n]);
529+
tr.find('SELECT[data-column='+column+'_'+n+']').val(data[n]);
530+
console.log(n,data[n]);
531+
}
532+
}
533+
// Fix rendering bug that prevents TH from being shown after deleting
534+
$(element).find('TH').hide().show();
535+
}
536+
515537
function modelInit(slug) {
516538
$('.datepicker').datepicker({
517539
showButtonPanel: true,
@@ -543,6 +565,9 @@ function modelInit(slug) {
543565
$('UL.input_images .button.add').click(function () {
544566
modelAddMedia(slug, this);
545567
});
568+
$('DIV.rows .button.add').click(function () {
569+
modelAddLine(slug, $(this).prev());
570+
});
546571
$('.header .search INPUT').keyup(function (e) {
547572
modelSearch(this.value);
548573
});

src/views/model.blade.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@
8282
{!! $lp->foreign($id, $column) !!}
8383
@elseif ($column['type'] == 'pivot')
8484
<div class="pivot">{!! $lp->pivot($id, $column) !!}</div>
85+
@elseif ($column['type'] == 'rows')
86+
<div class="rows">
87+
{!! $lp->rows($id, $column) !!}
88+
<button class="button add"><i class="fa fa-plus"></i></button>
89+
</div>
8590
@elseif ($column['type'] != 'boolean')
8691
{{$column['type']}}
8792
@endif

0 commit comments

Comments
 (0)