jQuery UI Sortable在Laravel

时间:2017-09-21 11:55:52

标签: ajax laravel jquery-ui-sortable

我已将此代码插入我的脑海:

<meta name="csrf-token" content="{{ csrf_token() }}" />

这是我在刀片中的脚本:

<script>
    $(document).ready(function(){
        $('#list-items').sortable({
            update: function(){
                    $.map($(this).find('li'), function(el){
                        var itemID = el.id;
                        var itemIndex = $(el).index();

                    $.ajaxSetup({
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                        }
                    });

                    $.ajax({
                        type: 'GET',
                        url: '/elaborazione_processi/ordinaFasi',
                        headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                        data: {"_token": "{{ csrf_token() }}", itemID: itemID, itemIndex: itemIndex},
                        dataType: 'json',
                        success: function(data) { console.log(data); },
                        error : function(err) {
                            console.log('Error!', err)
                        },
                    })
                });
            }
        });
    });
</script>

这是我的控制者:

public function ordinaFasi(Request $request){

    try{
        $itemID = $request->itemID;
        $itemIndex = $request->itemIndex;
        FaseProcesso::where('id', $itemID)->update(['posizione' => $itemIndex]);

        Log::info($request->all());
    }
    catch(Exception $exception){
        echo json_encode($exception->getMessage());
    }
}

这是控制台中的错误:

为行数打印错误。

这是我的路线:

Auth::routes(); 
Route::get('/elaborazione_processi/ordinaFasi','FasiProcessoController@ordinaFasi');

如何解决这个问题?我想订购商品清单!

Error I got

控制台 - 网络: enter image description here

Laravel.log()错误:

    Next ErrorException: Trying to get property of non-object (View: C:\laragon\www\BancaStatoHR\resources\views\processi\elaborazioneProcessi.blade.php) in C:\laragon\www\BancaStatoHR\storage\framework\views\0b6d347da0d8c0285a6745e0d1c578d3f6675f4e.php:22
Stack trace:
#0 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\View\Engines\PhpEngine.php(44): Illuminate\View\Engines\CompilerEngine->handleViewException(Object(ErrorException), 1)
#1 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\View\Engines\CompilerEngine.php(59): Illuminate\View\Engines\PhpEngine->evaluatePath('C:\\laragon\\www\\...', Array)
#2 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\View\View.php(137): Illuminate\View\Engines\CompilerEngine->get('C:\\laragon\\www\\...', Array)
#3 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\View\View.php(120): Illuminate\View\View->getContents()
#4 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\View\View.php(85): Illuminate\View\View->renderContents()
#5 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Http\Response.php(38): Illuminate\View\View->render()
#6 C:\laragon\www\BancaStatoHR\vendor\symfony\http-foundation\Response.php(201): Illuminate\Http\Response->setContent(Object(Illuminate\View\View))
#7 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Routing\Router.php(615): Symfony\Component\HttpFoundation\Response->__construct(Object(Illuminate\View\View))
#8 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Routing\Router.php(572): Illuminate\Routing\Router->prepareResponse(Object(Illuminate\Http\Request), Object(Illuminate\View\View))
#9 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(30): Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#10 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Routing\Middleware\SubstituteBindings.php(41): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#11 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Illuminate\Routing\Middleware\SubstituteBindings->handle(Object(Illuminate\Http\Request), Object(Closure))
#12 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#13 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken.php(65): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#14 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure))
#15 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#16 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\View\Middleware\ShareErrorsFromSession.php(49): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#17 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#18 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#19 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Session\Middleware\StartSession.php(64): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#20 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#21 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#22 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse.php(37): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#23 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure))
#24 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#25 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\EncryptCookies.php(59): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#26 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure))
#27 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#28 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(102): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#29 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Routing\Router.php(574): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#30 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Routing\Router.php(533): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#31 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Routing\Router.php(511): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#32 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(176): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#33 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(30): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#34 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php(30): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#35 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#36 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#37 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php(30): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#38 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#39 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#40 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\ValidatePostSize.php(27): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#41 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle(Object(Illuminate\Http\Request), Object(Closure))
#42 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#43 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode.php(46): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#44 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#45 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#46 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(102): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#47 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(151): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#48 C:\laragon\www\BancaStatoHR\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(116): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#49 C:\laragon\www\BancaStatoHR\public\index.php(53): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#50 {main} 

查看 - ElaborazioneFaseProcessi:

@section('content')
<!-- Main content -->
<section class="content container-fluid">

    <div class="row">
        <div class="col-md-4">
            <div class="card">
                <div class="card-block">
                    <form method="POST" action="/elaborazione_processi/{{ $processo->id }}/fasi_processo">
                        {{ csrf_field() }}
                        <div class="form-group">
                            {!! Form::label('nome', 'Nome') !!}
                            {!! Form::text('nome', null, ['class' => 'form-control','placeholder'=>'Inserisci qui il nome della fase del processo']) !!}
                        </div>
                        <div class="form-group">
                            {!! Form::label('descrizione', 'Descrizione') !!}
                            <div class="card">
                                <div class="card-block">
                                    <div class="form-group">
                                        <textarea name="descrizione" class="form-control my-editor" placeholder="Inserisci descrizione..."></textarea>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <button type="submit" class="btn btn-primary">Inserisci fase processo</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="comments">
                <ul id="list-items" class="list-group">

                    @php($posizione=1)

                    @foreach ($processo->fasi_processo as $faseProcesso)
                        <li id="{{ $faseProcesso->id }}" class="list-group-item ui-state-default">
                            <div class="row">
                                <div class="col-sm-8">
                                    <b>{!!($posizione)!!})</b>
                                    <b>{!!($faseProcesso->nome)!!}</b></br>
                                    {!!html_entity_decode($faseProcesso->descrizione)!!}
                                </div>
                                <div class="col-sm-2">
                                    {!! Form::open(['method' => 'DELETE','route' => ['fasi_processo.destroy', $faseProcesso->id]]) !!}
                                    <button type="submit" class="btn btn-danger">
                                        <i class='fa fa-trash' aria-hidden='true'></i>
                                    </button>
                                    {!! Form::close() !!}
                                </div>
                                <div class="col-sm-2">
                                    <a href="/fasi_processo/{{ $faseProcesso->id }}/edit" class="btn btn-primary"><i class='fa fa-edit' aria-hidden='true'></i></a>
                                </div>
                            </div>
                        </li>
                        @php($posizione++)
                    @endforeach
                </ul>
            </div>
        </div>
    </div>
</section>
<!-- /.content -->

@stop

@section('css')

@stop

@section('js')

<script src="{{ asset('vendor/adminlte/bower_components/jquery-ui/jquery-ui.js') }}"></script>

<script>
    $( function() {
        $( "#list-items" ).sortable();
        $( "#list-items" ).disableSelection();
    } );
</script>

<script>
    $(document).ready(function(){
        $('#list-items').sortable({
            update: function(){
                $.map($(this).find('li'), function(el){
                    var itemID = el.id;
                    var itemIndex = $(el).index();

                    $.ajax({
                        type: 'GET',
                        url: '/elaborazione_processi/ordinaFasi',
                        data: {itemID: itemID, itemIndex: itemIndex},
                        dataType: 'json',
                        success: function(data) { console.log(data); },
                        error : function(err) {
                            console.log('Error!', err)
                        },
                    })
                });
            }
        });
    });
</script>

<script>
var editor_config = {
    path_absolute : "/",
    selector: "textarea.my-editor",
    plugins: [
        "advlist autolink lists link image charmap print preview hr anchor pagebreak",
        "searchreplace wordcount visualblocks visualchars code fullscreen",
        "insertdatetime media nonbreaking save table contextmenu directionality",
        "emoticons template paste textcolor colorpicker textpattern"
    ],
    toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image media",
    relative_urls: false,
    file_browser_callback : function(field_name, url, type, win) {
        var x = window.innerWidth || document.documentElement.clientWidth || document.getElementsByTagName('body')[0].clientWidth;
        var y = window.innerHeight|| document.documentElement.clientHeight|| document.getElementsByTagName('body')[0].clientHeight;

        var cmsURL = editor_config.path_absolute + 'laravel-filemanager?field_name=' + field_name;
        if (type == 'image') {
            cmsURL = cmsURL + "&type=Images";
        } else {
            cmsURL = cmsURL + "&type=Files";
        }

        tinyMCE.activeEditor.windowManager.open({
            file : cmsURL,
            title : 'Filemanager',
            width : x * 0.8,
            height : y * 0.8,
            resizable : "yes",
            close_previous : "no"
        });
    }
};

tinymce.init(editor_config);

</script>
@stop

型号:

FaseProcesso.php

protected $table = 'fasi_processo';

public function processo() {
    return $this->belongsTo(Processo::class);
}

Processo.php

protected $table = 'processi';

public function fasi_processo() {
    return $this->hasMany(\App\FaseProcesso::class);
}

1 个答案:

答案 0 :(得分:0)

尝试这个答案,我仍然不确定它是否有效或不发布您的web.php

<head>删除此行,因为csrf_token()仅用于发帖请求而不是获取请求

<meta name="csrf-token" content="{{ csrf_token() }}" />

然后按照我的说法进行更改

<script>
 $(document).ready(function(){
     $('#list-items').sortable({
            update: function(){
         $.map($(this).find('li'), function(el){
             var itemID = el.id;
             var itemIndex = $(el).index();
                    $.ajax({
                        type: 'GET',
                        url: '/elaborazione_processi/ordinaFasi',
                        data:  itemID: itemID, itemIndex: itemIndex},
                        dataType: 'json',
                        success: function(data) { console.log(data); },
                        error : function(err) {
                 console.log('Error!', err)
                        },
                    })
                });
     }
        });
    });
</script>

使用此

更改您的控制器
public function ordinaFasi(Request $request){
    try{
        $itemID = $request->itemID;
        $itemIndex = $request->itemIndex;

        FaseProcesso::where('id', $itemID)->update(['posizione' => $itemIndex]);
return redirect()->action(
    'FasiProcessoController@show');
    }
    catch(\Exception $e){
        echo json_encode($e->getMessage());
    }
}

更改控制器的show功能

public function show($id){
     $processo = Processo::find($id); 
     return view('processi.elaborazioneProcessi',['processo'=>$processo]); 
}

检查一下,如果您有任何问题,请在评论栏中点击此处。