GridFieldTrait.php
34.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* 自定义 grid列表 字段
+-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Admin\Traits
* @package App\Admin\Traits
* @author Richer <yangzi1028@163.com>
* @date 2020年11月13日 01:26:09
* @copyright 2021-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Admin\Traits;
use App\Models\Area\Area;
use App\Models\System\SystemConfig;
use Carbon\Carbon;
use Encore\Admin\Grid;
use Encore\Admin\Widgets\Table;
use Illuminate\Support\Arr;
/**
* Trait GridFieldTrait
*
* @category App\Admin\Traits
* @package App\Admin\Traits
* @author Richer <yangzi1028@163.com>
* @date 2020年11月13日 01:26:09
* @copyright 2021-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
trait GridFieldTrait
{
/**
* 增加用户信息
*
* @param string $relation
* @return mixed
*/
public function gridUser($relation = '')
{
$avatar = $relation ? $relation .'.avatar' : 'avatar';
$nickname = $relation ? $relation .'.nickname' : 'nickname'; ;
$mobile = $relation ? $relation .'.mobile' : 'mobile';;
$this->gridSingleImageField($avatar, __('avatar'));
$this->gridTextField($nickname, __('nickname'));
$this->gridTextField($mobile, __('mobile'));
}
/**
* 渲染生成 form 表单的通用字段
*
* @param int $show_created_at
* @param int $show_updated_at
* @param int $show_status
* @param int $show_sort
* @return void
*/
public function gridCommonFields($show_created_at = 1, $show_updated_at = 0, $show_status = 0, $show_sort = 0)
{
if ($show_sort) {
$editable = 0;
if ($this->can_edit) {
$editable = 1;
}
$this->gridNumberField('sort', __('sort'), 1, $editable);
}
if ($show_status) {
if ($this->can_edit) {
$this->gridSwitchField('status', __('status'), config('constants.STATUS_STATE_OPTIONS'));
} else {
$this->gridSingleSelect('status', __('status'), config('constants.STATUS_OPTIONS'), config('constants.STATUS_COLOR_OPTIONS'));
}
}
if ($show_created_at) {
$this->gridDateField();
}
if ($show_updated_at) {
$this->gridDateField('updated_at');
}
}
/**
* 增加行序号
*
* @param string $field
* @param string $comment
* @param bool $show_id
* @return mixed
*/
public function gridRowNo($field = 'No', $comment = '#', $show_id = false)
{
$this->grid->column($field, trans($comment));
// 获取分页参数
$perPage = $this->grid->perPage ?? 20;
// 获取分页参数
$page = request()-> get('page') ?? 1 ;
// 增加一列
$this->grid->rows(function ($row, $number) use ($perPage, $page) {
// 计算序号
$No = $perPage * ($page -1) + ($number + 1) ;
$row->column('No', $No);
});
if ($show_id === true) {
$this->gridTextField('id');
}
return $this->grid;
}
/**
* 渲染生成通用字段类型
*
* @param string $field 字段名
* @param string $comment 字段注释
* @param integer $limit 截取字符串长度,默认为0,不截取
* @param integer $sortable 是否可排序 ,默认为0,不可排序
* @param integer $editable 是否可编辑,默认为0,不可编辑
* @param int $width
* @return Grid\Column
*/
public function gridCommonField($field = 'title', $comment = '', $limit = 0, $sortable = 0, $editable = 0, $width = 0)
{
if (!$comment) {
$comment = __($field);
}
$_grid = $this->grid->column($field, $comment);
// 是否截取字符串
if ($limit) {
$_grid->limit($limit);
}
// 是否可编辑
if ($editable) {
$_grid->editable();
}
// 是否可排序
if ($sortable) {
$_grid->sortable();
}
// 是否可排序
if ($width) {
$_grid->width($width);
}
$_grid->display(function ($value) {
if (isset($value)) {
return $value;
}
return '--';
});
// 查询方式
//$_grid->filter('like');
// 返回对象,方便继续链式调用
return $_grid;
}
/**
* 渲染生成input 为text的字段类型
*
* @param string $field 字段名
* @param string $comment 字段注释
* @param integer $limit 截取字符串长度,默认为0,不截取
* @param integer $sortable 是否可排序 ,默认为0,不可排序
* @param integer $editable 是否可编辑,默认为0,不可编辑
* @param int $width
* @return Grid\Column
*/
public function gridTextField($field = 'title', $comment = '', $limit = 0, $sortable = 0, $editable = 0, $width = 0)
{
return $this->gridCommonField($field, $comment, $limit, $sortable, $editable, $width);
}
/**
* 渲染生成number类型字段
*
* @param string $field 字段名
* @param string $comment 字段注释
* @param integer $sortable 是否可排序 ,默认为0,不可排序
* @param integer $editable 是否可编辑,默认为0,不可编辑
* @return Grid\Column
*/
public function gridNumberField($field = 'number', $comment = '', $sortable = 0, $editable = 0)
{
return $this->gridCommonField($field, $comment, 0, $sortable, $editable);
}
/**
* 渲染生成number类型字段
*
* @param string $field 字段名
* @param string $comment 字段注释
* @return Grid\Column
*/
public function gridColorField($field, $comment)
{
$this->gridTextField($field, $comment)->display(function ($value) {
if (isset($value)) {
$str = '<a style="background: '.$value .'!important;margin-right: 10px;"> </a>';
$str .= '<a style="color: '.$value.'">'.$value.'</a>';
return $str;
return '<span><span style="background: '.$value .'!important;width: 40px;height: 20px;display: block"></span> '. $value;
}
return '--';
});
}
/**
* 渲染生成switch选择框
*
* @param string $field 字段名
* @param string $comment 字段注释
* @param array $states states
* @return grid
*/
public function gridSwitchField($field, $comment, $states)
{
if (!$comment) {
$comment = __($field);
}
// 国际化
foreach ($states as &$state) {
if (isset($state['text'])) {
$state['text'] = __($state['text']);
}
}
// 设置text、color、和存储值
$this->grid->$field($comment)->switch($states);
// 返回对象,方便继续链式调用
return $this->grid;
}
/**
* 渲染生成select选择框
*
* @param string $field 字段名
* @param string $comment 字段注释
* @param array $options options
* @param int $sortable
* @return mixed
*/
public function gridCheckboxField($field, $comment, $options = [], $sortable = 0)
{
if (!$comment) {
$comment = __($field);
}
// 获取代码集配置
$options = $options ?: config('constants.'.$field.'_options');
$_grid = $this->grid->$field($comment)->checkbox($options);
// 是否可排序
if ($sortable) {
$_grid->sortable();
}
return $_grid;
}
/**
* 渲染生成select选择框
*
* @param string $field 字段名
* @param string $comment 字段注释
* @param array $options options
* @param array $colors
* @param int $sortable
* @param string $type
* @return mixed
*/
public function gridSingleSelectField($field, $comment = '', $options = [], $colors = [], $sortable = 0, $type = 'text')
{
if (!$comment) {
$comment = __($field);
}
// 获取代码集配置
$options = $options ?: config('constants.'.$field.'_options');
// 获取颜色代码集配置
$options_color = $colors ?: config('constants.'.$field.'_color_options');
$_grid = $this->grid->$field($comment)->display(function ($data) use ($field, $options, $options_color, $type) {
if ($data === null) {
return '--';
}
if (is_array($data)) {
return '--';
}
// 获取每个状态对应的颜色,默认为系统颜色
$color = Arr::get($options_color, $data, '');
$value = __(Arr::get($options, $data));
if (!$value) {
return '--';
}
if ($type === 'label') {
return '<span class="label label-'.$color.'" style="background-color:'.$color.';white-space:nowrap;">'.$value.'</span>';
} else {
return '<span class="text text-'.$color.'" style="color:'.$color.';white-space:nowrap;">'.$value.'</span>';
}
});
// 是否可排序
if ($sortable) {
$_grid->sortable();
}
return $_grid;
}
/**
* 渲染生成select选择框,需要从其他关联表中获取
*
* @param grid $grid grid对象
* @param string $field 字段名
* @param string $comment 字段注释
* @param string $related_model 关联模型
* @param string $related_field 关联字段
* @param string $related_value_field 关联模型中字段
* @return Grid
*/
public function gridRelatedSelectField($grid, $field, $comment, $related_model, $related_field, $related_value_field)
{
$grid-> $field($comment)->display(function () use ($related_field, $related_model, $related_value_field) {
$related_field_value = $this->$related_field;
$display ='';
if ($related_field_value) {
$related_value = $related_model::find($related_field_value);
$display = $related_value->$related_value_field;
}
return $display;
});
// 返回对象,方便继续链式调用
return $grid;
}
/**
* 渲染生成 select 多选框
*
* @param grid $grid grid对象
* @param string $field 字段名
* @param string $comment 字段注释
* @param array $options options
* @return grid
*/
public function gridMultipleSelectField($grid, $field, $comment, $options = [])
{
$grid->$field($comment)->display(function ($data) use ($field, $options) {
if ($data) {
// 转换审核状态
$array = [];
// 转换审核状态
$options = $options ?: config('constants.'.$field.'_options');
if ($options) {
foreach ($options as $key => $option) {
if (in_array($key, $data)) {
$array[] = $option;
}
}
} else {
$array = $data;
}
if (!empty($array)) {
return implode(',', $array);
}
}
return '—';
});
// 返回对象,方便继续链式调用
return $grid;
}
/**
* 渲染生成价格的字段类型
*
* @param string $field 字段名
* @param string $comment 字段注释
* @param integer $sortable 是否可排序,默认为1,可排序
* @param integer $editable 是否可编辑,默认为0,不可编辑
* @return Grid\Column
*/
public function gridCurrencyField($field = 'price', $comment = '', $sortable = 0, $editable = 0)
{
if (!$comment) {
$comment = __($field);
}
$_grid = $this->grid->column($field, $comment)->display(function ($data) {
return format_money($data);
});
// 是否可编辑
if ($editable) {
$_grid->editable();
}
// 是否可排序
if ($sortable) {
$_grid->sortable();
}
//$_grid->style('max-width:150px;word-break:break-all;text-align:right;');
// 返回对象,方便继续链式调用
return $_grid;
}
/**
* 渲染生成价格的字段类型
*
* @param grid $grid grid对象
* @param string $field 字段名
* @param string $comment 字段注释
* @param integer $sortable 是否可排序,默认为1,可排序
* @param integer $editable 是否可编辑,默认为0,不可编辑
* @return grid
*/
public function gridSpecialCurrencyField($grid, $field, $comment, $sortable = 0, $editable = 0)
{
// 不存在的`full_name`字段
$grid->column($field, $comment)->display(function () {
return '<span style="color:#e08e0b">'.rtrim(rtrim($this->purchase_price, '0'), '.') .
'</span> / <span style="color: #999">' .rtrim(rtrim($this->price, '0'), '.').'</span>';
});
// 返回对象,方便继续链式调用
return $grid;
}
/**
* 渲染生成单个上传图片字段
*
* @param string $field 字段名
* @param string $comment 字段注释
* @param array $options
* @return Grid
*/
public function gridSingleImageField($field = 'cover_image', $comment = '', $options = ['zooming' => true, 'width' => 60, 'height' => 60, 'class' => 'rounded'])
{
// 列表的图片可以点击放大,并设置大小
//$grid->$field($comment)->lightbox($options);
if (!$comment) {
$comment = __($field);
}
$this->grid->$field($comment)->gallery($options);
//$grid->column($field,$comment)->carousel($width = 300, $height = 200);
// 返回对象,方便继续链式调用
return $this->grid;
}
/**
* 渲染生成单个上传图片字段
*
* @param string $field 字段名
* @param string $comment 字段注释
* @param array $options
* @return mixed
*/
public function gridMultipleImageField($field = 'image', $comment = '图片', $options = ['zooming' => true,'width' => 93,'height' => 52,'class' => 'rounded'])
{
// 列表的图片可以点击放大,并设置大小
//$grid->$field($comment)->lightbox($options);
$this->grid->$field($comment)->gallery($options);
// $this->grid->column($field, $comment)->carousel($width = 300, $height = 200);
// $this->grid->column($field, $comment)->carousel();
// 返回对象,方便继续链式调用
return $this->grid;
}
/**
* 渲染 video 字段
*
* @param string $field
* @param string $comment
*
* @return void
*/
public function gridVideoField($field = 'video', $comment = '')
{
if (!$comment) {
$comment = __($field);
}
$this->grid->column($field, $comment)->video(['videoWidth' => '90%', 'videoHeight' => '90%']);
return $this->grid;
}
/**
* 渲染生成日期类型的字段
*
* @param string $field 字段名
* @param string $comment 字段注释
* @param string $format 日期转换格式
* @param integer $sortable 是否可排序 ,默认为1,可排序
* @return Grid\Column
*/
public function gridDateField($field = 'created_at', $comment = '', $format = 'Y-m-d H:i:s', $sortable = 1)
{
if (!$comment) {
$comment = __($field);
}
//$comment = __($field);
$_grid = $this->grid->column($field, $comment)->display(function ($data) use ($format) {
if ($data) {
return format_date($data, $format);
$value = Carbon::parse($data) ->format($format);
return '<span style="white-space: nowrap;">'.$value.'</span>';
$value_arr = explode(' ', $value);
if (count($value_arr) === 2) {
return '<span class="d-block text-nowrap">'.$value_arr[0].'</span>' .
'<span class="d-block text-nowrap">'.$value_arr[1].'</span>';
} else {
return '<span style="white-space: nowrap;">'.$value.'</span>';
}
} else {
return '--';
}
});
// 是否可排序
if ($sortable) {
$_grid->sortable();
}
// 返回对象,方便继续链式调用
return $_grid;
}
/**
* 渲染生成日期类型的字段
*
* @param grid $grid grid对象
* @param string $field 字段名
* @param string $comment 字段注释
* @param string $format 日期转换格式
* @param int $sortable
* @param int $countdown
* @return Grid\Column
*/
public function gridDateCountdownField($grid, $field = '', $comment = '', $format = 'Y-m-d', $sortable = 0, $countdown = 10)
{
$_grid = $grid->column($field, $comment)->display(function ($data) use ($format, $countdown) {
$url = '/admin/rooms/' .$this->id .'/check-record';
if ($data) {
$value = Carbon::parse($data) ->format($format);
// 比较日期
$days = today()->diffInDays(Carbon::parse($value), false);
// $str = '<span class="d-block text-nowrap">'.$value.'</span>';
$str ='<span class="d-block text-nowrap"><a href="javascript:;" data-id="{$this->id}" class="text text-primary" onclick=roomCheckRecordsPage("'.$url.'")>'.$value.'</a></span>';
$str .= '<span style="white-space: nowrap;font-size: 0.9rem;';
if ($days <= $countdown) {
$str .= 'color: red;';
}
if ($days > 0) {
$str .= '">还剩 '.$days.' 天</span>';
} elseif ($days < 0) {
$str .= '">已过 '.abs($days).' 天</span>';
} else {
$str .= '">今天截止</span>';
}
return $str;
} else {
return '<a href="javascript:;" data-id="{$this->id}" class="text text-primary" onclick=roomCheckRecordsPage("'.$url.'")>设置日期</a>';
}
});
// 是否可排序
if ($sortable) {
$_grid->sortable();
}
// 返回对象,方便继续链式调用
return $_grid;
}
/**
* 渲染生成创显示对错字段
*
* @param string $field 字段名
* @param string $comment 字段注释
* @param array $options
* @return Grid
*/
public function gridBoolField($field, $comment = '', $options = [0 => false, 1 => true])
{
if (!$comment) {
$comment = __($field);
}
$this->grid->column($field, $comment)->bool($options);
return $this->grid;
}
/**
* 渲染生成创建用户字段
*
* @param Grid $grid grid对象
* @param string $field 字段名
* @param string $comment 字段注释
* @return Grid
*/
public function gridCreatedByField($grid, $field = 'user', $comment = '用户')
{
// 不存在的`full_name`字段
$grid->column($field, $comment)->display(function ($data) {
if ($data) {
$name = Arr::get($data, 'name');
$nickname = Arr::get($data, 'nickname');
$str = '<span class="username">'.$name;
if ($nickname) {
$str .= '('.$nickname.')';
}
return $str.'</span>';
// return '<span class="username">'.$nickname.'<br/></span>'.$username;
} else {
return '—';
}
});
// 返回对象,方便继续链式调用
return $grid;
}
/**
* 渲染生成创建供应商机构字段
*
* @param Grid $grid grid对象
* @param string $field 字段名
* @param string $comment 字段注释
* @param bool $isChild
*
* @return Grid
*/
public function gridSupplierField($grid, $field = 'supplier', $comment = '供应商', $isChild = false)
{
$grid->column($field, $comment)->display(function ($data) use ($isChild) {
if ($isChild) {
$father = $this->father;
if ($father && $supplier = $father->supplier) {
return $supplier->name;
} else {
return '—';
}
} else {
if ($data) {
return Arr::get($data, 'name');
} else {
return '—';
}
}
});
// 返回对象,方便继续链式调用
return $grid;
}
/**
* 渲染生成创建供应商机构字段
*
* @param Grid $grid grid对象
* @param string $field 字段名
* @param string $comment 字段注释
* @return Grid
*/
public function gridFatherSupplierField($grid, $field = 'supplier', $comment = '供应商')
{
$grid->column($field, $comment)->display(function ($data) {
$father = $this->father;
if ($father) {
$supplier = $father->supplier;
dump($supplier);
return Arr::get($data, 'name');
} else {
return '—';
}
});
// 返回对象,方便继续链式调用
return $grid;
}
/**
* 渲染生成 状态
*
* @param string $field 字段名
* @param string $comment 字段注释
* @param array $options options
* @param array $options_color
* @param int $sortable
* @param int $editable
* @return mixed
*/
public function gridStatusField($field = 'status', $comment = '', $options = [], $options_color = [], $sortable = 0, $editable = 0)
{
if (!$comment) {
$comment = __($field);
}
// 获取代码集配置
$options = $options ?: config('constants.STATUS_OPTIONS');
// 获取颜色代码集配置
$options_color = $options_color ?: config('constants.STATUS_COLOR_OPTIONS');
return $this->gridSingleSelectField($field, $comment, $options, $options_color);
}
/**
* 渲染生成 审核状态
*
* @param string $field 字段名
* @param string $comment 字段注释
* @param array $options options
* @param array $options_color
* @param int $sortable
* @param int $editable
* @return mix
*/
public function gridAuditStatusField($field = 'audited_status', $comment = '', $options = [], $options_color = [], $sortable = 0, $editable = 0)
{
if (!$comment) {
$comment = __($field);
}
// 获取代码集配置
$options = $options ?: config('constants.AUDIT_STATUS_OPTIONS');
// 获取颜色代码集配置
$options_color = $options_color ?: config('constants.AUDIT_STATUS_COLOR_OPTIONS');
$grid = $this->gridSingleSelectField($field, $comment, $options, $options_color);
return $grid;
}
/**
* 渲染生成 审核状态
*
* @param string $field 字段名
* @param string $comment 字段注释
* @param array $options options
* @param array $options_color
* @param int $sortable
* @param int $editable
* @return Grid|mix
*/
public function gridUnshelvedStatusField($field = 'unshelved_status', $comment = '', $options = [], $options_color = [], $sortable = 0, $editable = 0)
{
if (!$comment) {
$comment = __($field);
$comment = __('is_shelved');
}
$grid = $this->gridBoolField($field, $comment, [1,0]);
return $grid;
if (!$comment) {
$comment = __('is_shelved');
}
// 获取代码集配置
$options = $options ?: config('constants.UNSHELVED_STATUS_OPTIONS');
// 获取颜色代码集配置
$options_color = $options_color ? : config('constants.UNSHELVED_COLOR_OPTIONS');
// $grid = $this->gridSingleSelectField($field, $comment, $options, $options_color);
return $grid;
}
/**
* 渲染生成 推荐状态
*
* @param string $field 字段名
* @param string $comment 字段注释
* @param array $options options
* @param array $options_color
* @param int $sortable
* @param int $editable
* @return mixed
*/
public function gridRecommendedStatusField($field = 'recommended_status', $comment = '', $options = [], $options_color = [], $sortable = 0, $editable = 0)
{
if (!$comment) {
$comment = __('is_recommended');
}
return $this->gridBoolField($field, $comment);
if (!$comment) {
$comment = __($field);
}
// 获取代码集配置
$options = $options ?: config('constants.RECOMMENDED_STATUS_OPTIONS');
// 获取颜色代码集配置
$options_color = $options_color ? : config('constants.RECOMMENDED_COLOR_OPTIONS');
return $this->gridSingleSelectField($field, $comment, $options, $options_color);
}
/**
* 渲染评论
*
* @param $grid
* @param string $field
* @param string $comment
* @return mixed
*/
public function gridCommentField($grid, $field, $comment = '评论')
{
$this->gridNumberField($grid, $field, $comment)->modal('最新评论', function ($model) {
// 获取最新的10条数据
$comments = $model->comments()->with('user')->take(10)->latest()
->get(['id','user_id','comment','created_at'])
->map(function ($comment) {
$comment->username = '<span class="username">'.
$comment->user->username .'<br/></span>'.
$comment->user->nickname;
return $comment->only(['id', 'comment', 'username','created_at']);
});
return new Table(['#', '内容', '发布用户','发布时间'], $comments->toArray());
})->sortable();
return $grid;
}
/**
* 渲染评论
*
* @param $grid
* @param string $field
* @param string $comment
* @param int $sort
* @param string $room_field
* @return mixed
*/
public function gridAreaField($grid, $field = 'area', $comment = '设备位置', $sort = 0, $room_field = 'room_id')
{
if ($sort === 1) {
$this->gridTextField($grid, 'village_id', '所属'.Area::VILLAGE_ZH)->display(function () {
return $this->village->title;
})->sortable();
$this->gridTextField($grid, 'group_id', '所属'.Area::GROUP_ZH)->display(function () {
return $this->group->title;
})->sortable();
$this->gridTextField($grid, 'building_id', '所属'.Area::BUILDING_ZH)->display(function () {
return $this->building->title;
})->sortable();
$this->gridTextField($grid, $room_field, '所属'.Area::ROOM_ZH)->display(function () use ($room_field) {
if ($room_field === 'id') {
return $this->title;
} else {
return $this->room->title;
}
})->sortable();
} else {
$this->gridTextField($grid, $field, $comment)->display(function ($value) use ($room_field) {
if ($room_field === 'id') {
$room_title = $this->title;
} else {
$room_title = optional($this->room)->title;
}
return optional($this->village)->title . ','.
optional($this->group)->title . ','.
optional($this->building)->title. ','.
$room_title;
});
}
}
/**
* 获取单选选项
*
* @param $field
* @param string $comment
*/
public function gridTextLabelField($field, $comment = '')
{
if (!$comment) {
$comment = __($field);
}
$this->gridTextField($this->grid, $field, $comment)->display(function ($value) {
if (!$value) {
return '--';
}
return '<span class="" style="margin: 2px;white-space: nowrap">'.$value .'</span>';
});
}
/**
* 获取单选选项
*
* @param $key
* @param string $comment
* @param array $options
* @param array $color_options
*/
public function gridSingleSelect($key, $comment = '', $options = [], $color_options = [])
{
if (!$comment) {
$comment = __($key);
}
if (!$options) {
$options = SystemConfig::getSelectOptions($key);
}
$this->grid->column($key, $comment)->display(function ($value) use ($options, $color_options) {
if (!$value) {
return '--';
}
$color = Arr::get($color_options, $value, 'success');
$value = __(Arr::get($options, $value));
if (!$value) {
return '--';
}
return '<span class="label label-' . $color . '" style="background-color:' . $color . ';white-space:nowrap;">' . $value . '</span>';
});
}
/**
* 获取单选选项到多选
*
* @param $key
*/
public function gridSingleSelect2SingleSelect($key)
{
$this->gridTextField($this->grid, $key, __($key))->display(function ($value) use ($key) {
// 获取配置
$option = SystemConfig::getSelectOptions($key);
if ($value == config('constants.STATUS_OK')) {
// 获取补充信息
$key_supplement = $key.'_supplement';
$value_supplement = $this->$key_supplement;
if (!$value_supplement) {
return '';
}
return '<span class="label label-success" style="margin: 2px">'.Arr::get($option, $value_supplement).'</span>';
} else {
return '<span class="label label-info" style="margin: 2px">'. __('no') .'</span>';
return __('no');
}
});
}
/**
* 获取单选选项到多选
*
* @param $key
*/
public function gridSingleSelect2MultipleText($key)
{
$this->gridTextField($this->grid, $key, __($key))->display(function ($value) use ($key) {
// 获取配置
// $option = SystemConfig::getSelectOptions($key);
if ($value == config('constants.STATUS_OK')) {
// 获取补充信息
$key_supplement = $key.'_supplement';
$value_supplement = $this->$key_supplement;
$str = '';
if ($value_supplement) {
foreach ($value_supplement as $item) {
$str .='<span class="label label-success" style="margin: 2px">'.Arr::get($item, 'name').'-'.Arr::get($item, 'mobile').'</span>';
}
}
return $str;
} else {
return '<span class="label label-info" style="margin: 2px">'. __('no') .'</span>';
}
});
}
/**
* 获取单选选项到多选
*
* @param $key
*/
public function gridSingleSelect2Text($key)
{
$this->gridTextField($this->grid, $key, __($key))->display(function ($value) use ($key) {
// 获取配置
if ($value == 'other') {
// 获取补充信息
$key_supplement = $key.'_supplement';
$value_supplement = $this->$key_supplement;
$str = '';
if ($value_supplement) {
$str .='<span class="label label-success" style="margin: 2px">'.__('other').':'.$value_supplement.'</span>';
}
return $str;
} else {
$option = SystemConfig::getSelectOptions($key);
return '<span class="label label-success" style="margin: 2px">'.Arr::get($option, $value).'</span>';
}
});
}
/**
* 获取单选到多选到其他
*
* @param $key
*/
public function gridSingleSelect2MultipleSelect($key)
{
$this->gridTextField($this->grid, $key, __($key))->display(function ($value) use ($key) {
if ($value == config('constants.STATUS_OK')) {
// 获取补充信息
$key_supplement = $key.'_supplement';
$key_other = $key.'_supplement_other';
$value_supplement = $this->$key_supplement;
$value_other = $this->$key_other;
$str = '';
$option = SystemConfig::getSelectOptions($key);
if ($value_supplement) {
foreach ($value_supplement as $item) {
if ($item === 'other') {
$str .='<span class="label label-success" style="margin: 2px">'. __('other') .':' . $value_other.'</span>';
} else {
$str .='<span class="label label-success" style="margin: 2px">'.Arr::get($option, $item).'</span>';
}
}
}
return $str;
} else {
return '<span class="label label-info" style="margin: 2px">'. __('no') .'</span>';
}
});
}
}