AlenV

Experience

1,410

0 Best Reply Awards

  • Member Since 1 Week Ago
  • 13 Lessons Completed
  • 3 Favorites

14th February, 2018

AlenV left a reply on Laravel 5.5 Ajax Problem • 1 week ago

It works, thank you for helping and teaching me, much appreciated. Now I have to make it work with paginator, but won't bother you anymore. Thank you once again.

AlenV left a reply on Laravel 5.5 Ajax Problem • 1 week ago

I did as you said, and now it returns only that 'carfeature_data' file. So I think the problem is here in controller.

$view = view('private.carfeature.carfeature_data', compact('carFeatures', 'sortBy'));

Should I create new function in controller which will be called by ajax?

AlenV left a reply on Laravel 5.5 Ajax Problem • 1 week ago

Okay, I've done that, and now it changes but I get like duplicate page.

HTML

@extends('layouts.main')

@section('title')
Car feature
@endsection

@section('content')

<?php $number=1;?>

<!-- Page title and Order by -->
<div class="row mt-5">

    <!-- Page title -->
    <div class="col-md-6 d-flex justify-content-left">
        <h1 class="text-left">Car feature</h1>
    </div>

    <!-- Sort By -->
    <div class="col-md-6 d-flex justify-content-right">

        <div class="col-md-5 mt-3">
            <h4 class="text-center">Sort by</h4>
        </div>

        <div class="col-md-7 mt-3">
            <select id="carFeatureSort" class="form-control" onchange="sortCarFeatures()">
                <option value="az">
                    Name A-Z
                </option>
                <option value="za">
                    Name Z-A
                </option>
            </select>
        </div>
    </div>
</div>

<div class="row justify-content-center mt-5">
    <div class="col-md-12">
        <table class="table table-striped table-inverse carfeature_table">
            <thead>
                <tr>
                    <th class="text-center">
                        #
                    </th>
                    <th class="text-center">
                        Feature name
                    </th>
                    @if(Auth::user()->user_rights==='Admin')
                    <th class="text-center">
                        Option
                    </th>
                    @endif
                </tr>
            </thead>

            <tbody>
                @foreach($carFeatures as $carFeature)
                <tr>
                    <td class="text-center">
                        {{$number}}<?php $number++;?>
                    </td>
                    <td class="text-center">
                        {{$carFeature->car_feature_name}}
                    </td>
                    @if(Auth::user()->user_rights==='Admin')
                    <td class="text-center">
                        <a href="/carfeature_edit/{{$carFeature->car_feature_id}}">Edit</a> | <a href="/carfeature_remove/{{$carFeature->car_feature_id}}">Remove</a>
                    </td>
                    @endif
                </tr>

                @endforeach

                @if(Auth::user()->user_rights==='Admin')
                <tr>
                    <td class="text-center" colspan="3">
                        <a href="/carfeature_add">Add new feature</a>
                    </td>
                </tr>
                @endif

            </tbody>
        </table>


        <!-- Paginator-->
        <div class="row mt-5 justify-content-center">
            <?php
            $number = $number;
            echo $carFeatures->render();
            ?>
        </div>

    </div>
</div>

<p id="sorttest">{{$sortBy}}</p>

@endsection

Controller

// Function to fetch all car features for listing
    public function listCarFeatures(Request $request) {
        $sortBy = 'az';
        if($request->has('sortBy')){
            $sortBy = $request->input('sortBy');
        }

        if($sortBy == 'az') {
            // Get car features from Database
            $carFeatures = DB::table('car_features')->orderBy('car_feature_name', 'asc')->paginate(5);
        } else {
            $carFeatures = DB::table('car_features')->orderBy('car_feature_name', 'desc')->paginate(5);
        }

        $view = view('private.carfeature.carfeature_show', compact('carFeatures', 'sortBy'));
        return $view->render();
    }

AJAX

function sortCarFeatures() {
    x = document.getElementById('carFeatureSort').value;

    $.ajax({
        url: '/carfeature',
        type: 'GET',
        data: { sortBy: x },
        success: function (response) {
            $('div.carfeature_table').replaceWith(response);
        },
        error: function(response){
            console.log('Error: ' + response);
        }
    });
}

13th February, 2018

AlenV left a reply on Laravel 5.5 Ajax Problem • 1 week ago

Alright. Yes, I want it to change the existing HTML.

AlenV left a reply on Laravel 5.5 Ajax Problem • 1 week ago

Hmm now when I put order by Z-A it gives me response what it should, but it doesn't update the view

AlenV left a reply on Laravel 5.5 Ajax Problem • 1 week ago

Oh my bad, forgot to delete it. I removed it, but still not working

AlenV started a new conversation Laravel 5.5 Ajax Problem • 1 week ago

Hello,

The problem is that I want to sort CarFeatures by name, but when I change listing from A-Z to Z-A nothing happens, I see the request in developer tools, but no changes.

Route

    Route::get('/carfeature/{sortBy}', ['uses' => '[email protected]']);

CarFeatureController

public function listCarFeatures(Request $request, $sortBy) {
        if($request->has('sortBy')){
            $sortBy = $request->input('sortBy');

        }
        if($sortBy == 'az') {
            // Get car features from Database
            $carFeatures = DB::table('car_features')->orderBy('car_feature_name', 'asc')->paginate(5);
        } else {
            $carFeatures = DB::table('car_features')->orderBy('car_feature_name', 'desc')->paginate(5);
        }

        $carFeatures = DB::table('car_features')->orderBy('car_feature_name', 'asc')->paginate(5);

        // Return view with CarFeatures
        return view('private.carfeature.carfeature_show')
        ->with(['carFeatures' => $carFeatures]);
    }

AJAX

function sortCarFeatures() {
    x = document.getElementById('carFeatureSort').value;

    $.ajax({
        url: 'carfeature',
        type: 'GET',
        data: { sortBy: x },
        success: function(response){
            console.log('Success: ' + response);
        },
        error: function(response){
            console.log('Error: ' + response);
        }
    });

}
Edit Your Profile
Update

Want to change your profile photo? We pull from gravatar.com.