2016-12-06 77 views
1

我想在Laravel中使用Kendo UI。这是工作很好,而我不想做一个CRUD的东西。在Telerik UI(Kendo)包中包含使用PHP封装CRUD函数的工作示例,但是我无法正确使用Laravel框架实现它。 (只显示空的网格。)在控制器的第一部分,当请求是'POST'并且取决于参数使某些CRUD内容时创建一个json内容。第二部分创建网格和后面的DataSourceTransport(等),以及称为'POST'请求的URL。该路线调用'任何'路线(路线\ web.php),但无法正常工作。我想象的路线设置是坏的,但我不知道什么是解决方案。 (我公司签订 “// !!!!!!!!!!!!!!!!!” 的问卷调查部分)Laravel和Kendo UI Grid:在网格编辑功能中路由URL请求CRUD json

GjtorzsXController.php

<?php 
namespace App\Http\Controllers; 
use Illuminate\Http\Request; 
use DB; 

    class GjtorzsXController extends Controller 
    { 
     // 
     public function index() 
     { 
      $result = new \DataSourceResult('sqlite:../database/database.sqlite');// 
      //1st section---------------------------------------------------------- 
      if ($_SERVER['REQUEST_METHOD'] == 'POST') {   
      header('Content-Type: application/json'); 
      $request = json_decode(file_get_contents('php://input')); 
      $type = $_GET['type']; 
      $columns = array('Rendszam', 'Tipus'); 
      switch($type) { 
       case 'create': 
        $result = $result->create('gepjarmu', $columns, $request->models, 'id'); 
        break; 
       case 'read': 
        $result = $result->read('gepjarmu', $columns, $request); 
        break; 
       case 'update': 
        $result = $result->update('gepjarmu', $columns, $request->models, 'id'); 
        break; 
       case 'destroy': 
        $result = $result->destroy('gepjarmu', $request->models, 'id'); 
        break; 
      } 
      echo json_encode($result); 
      exit; 
      } 

      //2nd section------------------------------------------------------ 
      $transport = new \Kendo\Data\DataSourceTransport(); 
      $create = new \Kendo\Data\DataSourceTransportCreate(); 
      $create->url('gjtorzsx?type=create') 
        ->contentType('application/json') 
        ->type('POST'); 
      $read = new \Kendo\Data\DataSourceTransportRead(); 
      $read->url('gjtorzsx?type=read') //TestPage.php?type=read gjtorzsx?type=read 
       ->contentType('application/json') 
       ->type('POST'); 
      $update = new \Kendo\Data\DataSourceTransportUpdate(); 

    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
    //This url is not correct?????????????????????? 
      $update->url('gjtorzsx?type=update') //???????????????????????????? 
        ->contentType('application/json') 
        ->type('POST'); 

      $destroy = new \Kendo\Data\DataSourceTransportDestroy(); 
      $destroy->url('gjtorzsx?type=destroy') 
        ->contentType('application/json') 
        ->type('POST'); 

      $transport->create($create) 
         ->read($read) 
         ->update($update) 
         ->destroy($destroy) 
         ->parameterMap('function(data) { 
          return kendo.stringify(data); 
         }'); 

      $model = new \Kendo\Data\DataSourceSchemaModel(); 


      $rendszamField = new \Kendo\Data\DataSourceSchemaModelField('Rendszam'); 
      $rendszamField->type('string'); 

      $tipusField = new \Kendo\Data\DataSourceSchemaModelField('Tipus'); 
      $tipusField->type('string'); 

      $model->id('id') 
        ->addField($rendszamField) 
        ->addField($tipusField; 

      $schema = new \Kendo\Data\DataSourceSchema(); 
      $schema->data('data') 
        ->errors('errors') 
        ->model($model) 
         ->total('total'); 

      $dataSource = new \Kendo\Data\DataSource(); 
      $dataSource->transport($transport) 
         ->batch(true) 
         ->pageSize(30) 
         ->schema($schema); 

      $grid = new \Kendo\UI\Grid('grid'); 

      $rendszamColumn = new \Kendo\UI\GridColumn(); 
      $rendszamColumn->field('Rendszam') 
          ->title('Rendszám') 
          ->width(100); 

      $tipusColumn = new \Kendo\UI\GridColumn(); 
      $tipusColumn->field('Tipus') 
         ->title('Típus') 
         ->width(200);   

      $command = new \Kendo\UI\GridColumn(); 
      $command->addCommandItem('destroy') 
        ->title(' ') 
        ->width(150); 

      $grid->addColumn($rendszamColumn, $tipusColumn, $command) 
       ->dataSource($dataSource) 
       ->addToolbarItem(new \Kendo\UI\GridToolbarItem('create'), 
        new \Kendo\UI\GridToolbarItem('save'), new \Kendo\UI\GridToolbarItem('cancel')) 
       ->height(540) 
       ->navigatable(true) 
       ->editable(true) 
       ->groupable(true) 
       ->pageable(true); 

      $args = array('grid' => $grid); 
      return \View::make('hello2')->with($args); 
     } 
    } 

路径\ web.php:

Route::any('gjtorzsx', '[email protected]'); 

helo2.blade.php: ... {! $ grid-> render()!!} ...

+0

任何错误信息?检查你的日志。你可以将其修改为https://stackoverflow.com/help/mcve吗? – Robert

+0

没有错误消息,只有网格是空的。我检查日志和错误是在路线url:'TokenMismatchException in VerifyCsrfToken.php line 68'我尝试解决方案:'“>”但不起作用... –

回答

0

我也遇到了这个VerifyCsrfToken异常。从这个链接的信息是非常有用的: http://laravel.io/forum/01-30-2015-laravel5-tokenmismatchexception-in-verifycsrftoken/?page=3) ,寻找geethpw的响应下。在我的HTML文件的顶部

在我的元标签,我加入

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

然后我jQuery的声明之后添加下面的脚本在我的头:具体地说,我做了如下补充

<script type="text/javascript"> 
    $.ajaxSetup({ 
     headers: { 
      'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') 
     } 
    }); 
</script> 

我没有将CSRF标记添加为隐藏字段,因为网格没有在表单的上下文中声明。

我也看到了你对Laracasts的评论,因为它似乎是我们在同样的问题上挣扎。我会注意到,我处理DataSource的方式与您的方式稍有不同;如果遇到任何问题,请告诉我,我会发布我最终做的事情。尽管我还没有开始实施其他CRUD操作,但我终于获得了显示数据。