我有一系列这样的小部件:
每个小部件右上角都有一个按钮,可以根据您在应用程序中的位置添加或删除小部件,下面是代码部分:
div class="col-xs-2">
@if(Route::current()->uri() == 'welcome')
<form action="{{ action('WidgetController@destroy', $widgetNo ) }}" method="post">
@csrf @method('delete')
<button type="submit" class="widget">
<i class="fa fa-bars "></i>
</button>
</form>
@elseif(Route::current()->uri() == 'widget-library')
@foreach($user->widgets as $widget)
@if($widget->widget_id == $widgetNo)
<i class="fa fa-bars "></i>
@else
<form action="{{action('WidgetController@store')}}" method="POST">
@csrf
<input type="hidden" name="user_id" value="{{ $user->id }}">
<input type="hidden" name="widget_id" value="{{ $widgetNo }}">
<button type="submit" class="widget">
<i class="fa fa-plus "></i>
</button>
</form>
@endif
@endforeach
@endif
</div>
在“widget library”中,代码检查用户拥有哪些widget,并放置一个加号或一个条(因为我想停止用户重新添加他们已经拥有的widget)。
如果只有一个小部件,则如下所示:
如你所见,这似乎是可行的。
不过,有两个问题。
-
当一个用户有多个小部件时,foreach循环循环多次以创建多个按钮。
-
此外,当用户没有小部件时,图标不会显示,因为它们是在foreach循环中生成的。
小部件本身只是html,所以为了添加一个惟一的id,我在小部件库视图中完成了以下工作。
@include('widgets.quick-search-full-width', ['widgetNo' => 1])
@include('widgets.calendar', ['widgetNo' => 2])
@include('widgets.suggestion', ['widgetNo' => 3])
@include('widgets.task', ['widgetNo' => 4])
@include('widgets.quick-search', ['widgetNo' => 5])
@include('widgets.message', ['widgetNo' => 6])
@include('widgets.find-expert', ['widgetNo' => 7])
@include('widgets.holiday', ['widgetNo' => 8])
@include('widgets.expenses', ['widgetNo' => 9])
@include('widgets.snap-pole', ['widgetNo' => 10])
@include('widgets.teams-cms', ['widgetNo' => 11])
@include('widgets.events-cms', ['widgetNo' => 12])
@include('widgets.team-cms', ['widgetNo' => 13])
@include('widgets.article-todo', ['widgetNo' => 14])
@include('widgets.article-cms', ['widgetNo' => 15])
@include('widgets.vacancies', ['widgetNo' => 16])
@include('widgets.yammer', ['widgetNo' => 17])
@include('widgets.poll-results', ['widgetNo' => 18])
如您所见,我正在为每个小部件传递一个id。
然后在主页上,我有一个大switch语句来决定要呈现哪个部分。
@foreach ($user->widgets as $widget)
@switch($widget->widget_id)
@case (1)
@include('widgets.quick-search-full-width', ['widgetNo' => 1])
@break;
@case(2)
@include('widgets.calendar', ['widgetNo' => 2])
@break;
@case (3)
@include('widgets.suggestion', ['widgetNo' => 3])
@break;
@case (4)
@include('widgets.task', ['widgetNo' => 4])
@break;
@case (5)
@include('widgets.quick-search', ['widgetNo' => 5])
@break;
@case (6)
@include('widgets.message', ['widgetNo' => 6])
@break;
@case(7)
@include('widgets.find-expert', ['widgetNo' => 7])
@break;
@case (8)
@include('widgets.holiday', ['widgetNo' => 8])
@break;
@case (9)
@include('widgets.expenses', ['widgetNo' => 9])
@break;
@case (10)
@include('widgets.snap-pole', ['widgetNo' => 10])
@break;
@case (11)
@include('widgets.teams-cms', ['widgetNo' => 11])
@break;
@case (12)
@include('widgets.events-cms', ['widgetNo' => 12])
@break;
@case (13)
@include('widgets.team-cms', ['widgetNo' => 13])
@break;
@case (14)
@include('widgets.article-todo', ['widgetNo' => 14])
@break;
@case (15)
@include('widgets.article-cms', ['widgetNo' => 15])
@break;
@case (16)
@include('widgets.vacancies', ['widgetNo' => 16])
@break;
@case(17)
@include('widgets.yammer', ['widgetNo' => 17])
@break;
@case(18)
@include('widgets.poll-results', ['widgetNo' => 18])
@break
@default
<h3>You currently have no widgets</h3>
@endswitch
@endforeach
我想我可以通过foreach循环中的一个简单的中断来解决这个问题,但这不起作用。