lundi 11 septembre 2017

How Jump to with collections of data in laravel

I'm implementing the Jump to... functionality in my project. What I want is:

  1. If the user is in the first item the left button is NOT included.
  2. If the user is in the last item the right button is NOT included.
  3. The left button should have a link same with the link before the current/selected item.
  4. The right button should have a link same with the link after the current/selected item.

Please see my code below:

Controller

$course = Course::with([
  'assignments' => $undeleted,
  'quizzes' => $undeleted,
  'add_links' => $undeleted
])->findOrFail($id);
$course_items = collect($course->assignments);
$course_items = $course_items->merge(collect($course->quizzes));
$course_items = $course_items->merge(collect($course->add_links));
$course_items = $course_items->sortBy('item_number');

View

<div class="jump-to">
    <div class="pull-right">
        <div class="btn-group" role="group">

            @foreach($course->topics as $topic)
                @foreach(collect($topic->assignments)->merge(collect($topic->quizzes))->merge(collect($topic->add_links))->sortBy('item_number') as $first_item)
                    @if($first_item->id != $assignment->id)
                        <a href="#" class="btn btn-primary">
                            <i class="fa fa-angle-left"></i>
                        </a>
                    @endif
                    <?php break 2; ?>
                @endforeach
            @endforeach
            <div class="btn-group" role="group">
                <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    Jump to <div class="caret"></div>
                </button>
                <ul class="dropdown-menu" role="menu">
                    @foreach($course->topics as $topic)
                        <li class="jdivider">
                            Topic 
                        </li>
                        @foreach(collect($topic->assignments)->merge(collect($topic->quizzes))->merge(collect($topic->add_links))->sortBy('item_number') as $topic_item)
                            <li>
                                @if($topic_item->item_type() == 'assignment')
                                    <a title="Assignment: " class="regular-link" href="">
                                        <i class="fa fa-tasks"></i> 
                                    </a>
                                @elseif($topic_item->item_type() == 'quiz')
                                    <a title="Quiz: " class="regular-link" href="">
                                        <i class="fa fa-file-text"></i> 
                                    </a>
                                @elseif($topic_item->item_type() == 'add_link')
                                    <a href="#">
                                        @if(!empty($topic_item->file_location) && in_array(strtolower(last(explode('.', $topic_item->file_location))), ['pdf', 'docx', 'doc']))
                                            <i class="fa fa-file-pdf-o"></i>
                                        @elseif(empty($topic_item->file_location))
                                            <i class="fa fa-globe"></i>
                                        @else
                                            <i class="fa fa-file-text-o"></i>
                                        @endif
                                        
                                    </a>
                                @endif
                            </li>
                        @endforeach
                    @endforeach
                </ul>
            </div>
            <a href="#" class="btn btn-primary">
                <i class="fa fa-angle-right"></i>
            </a>
        </div>
    </div>
</div>



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire