samedi 22 décembre 2018

I got 2 views and sometimes the second one has to use content that is already present in the first one and vice-versa. How to get that content?

I have 2 very similar views that are rendered when a user views the following URLs:

www.example.com/admin/categories
www.example.com/admin/users

First view:

<div class="admin-tabs">
    <p class='admin-tab' data-tab='categories'>Categories</p>
    <p class='admin-tab' data-tab='users'>Users</p>
</div>
<div class="admin-container">
    @foreach($categories as $category)
        <p>$category->name</p>
    @endforeach
</div>

Second view:

<div class="admin-tabs">
    <p class='admin-tab' data-tab='categories'>Categories</p>
    <p class='admin-tab' data-tab='users'>Users</p>
</div>
<div class="admin-container">
    @foreach($users as $user)
        <p>$user->username</p>
    @endforeach
</div>

I've successfully implemented an AJAX call that triggers when the admin clicks any of the <p> elements with admin-tab class. This AJAX call determines which <p> element has been clicked based on the data-tab and updates the HTML content of the div with a class of admin-container.

This is what I can't figure out:

If I am on URL www.example.com/admin/categories, I want to update the admin-container div with what is inside the admin-container div of the second view.

Vice-versa, if I'm on URL www.example.com/admin/users, I want to update the admin-container div with what is inside the admin-container div of the first image.

I could do that by creating 2 views which contain:

First view:

@foreach($categories as $category)
    <p>$category->name</p>
@endforeach

Second view:

@foreach($users as $user)
    <p>$user->username</p>
@endforeach

And then just return one of those 2 views, however, this way I'd be creating extra views with repeating code which I already have somewhere else. Is there any way to avoid this by getting the code from the already existing views?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire