Tag Archive อินเด็กซ์

สร้าง grid ใน laravel แบบ advance

การใช้ Nayjest Grids แบบ advance เพื่อที่จะสามารถค้นหา filter ข้อมูล, export ไฟล์ ออกมาในรูปแบบ .csv และ excel ได้

ติดตั้งโดย

  1. เปิดไฟล์ composer.json เพิ่ม[code language=”php” title=”composer.json”]

    "laravelcollective/html": "^5",
    "maatwebsite/excel": "~2.1.0",
    "nayjest/grids": "^1.3.1"

    [/code]
  2. Command[code language=”text”]composer update[/code]
  3. เปิดไฟล์ \config\app.php เพิ่ม [code language=”php” title=”configpp.php”]
    ‘providers’ => [

    ‘Collective\Html\HtmlServiceProvider’,
    ‘Maatwebsite\Excel\ExcelServiceProvider’,
    ‘Nayjest\Grids\ServiceProvider’,

    ‘aliases’ => [

    ‘Excel’ => ‘Maatwebsite\Excel\Facades\Excel’,
    ‘Form’ => ‘Collective\Html\FormFacade’,
    ‘Grids’ => ‘Nayjest\Grids\Grids’,
    ‘HTML’ => ‘Collective\Html\HtmlFacade’,
    ‘Input’ => ‘Illuminate\Support\Facades\Input’,

    ][/code]

สร้าง grid โดย

  1. [code language=”php” title=”\app\Http\Controllers\UsersController.php”]
    <?php

    namespace App\Http\Controllers;

    use App\user;
    use HTML;
    use Nayjest\Grids\Components\Base\RenderableRegistry;
    use Nayjest\Grids\Components\ColumnHeadersRow;
    use Nayjest\Grids\Components\ColumnsHider;
    use Nayjest\Grids\Components\CsvExport;
    use Nayjest\Grids\Components\ExcelExport;
    use Nayjest\Grids\Components\FiltersRow;
    use Nayjest\Grids\Components\Filters\DateRangePicker;
    use Nayjest\Grids\Components\HtmlTag;
    use Nayjest\Grids\Components\Laravel5\Pager;
    use Nayjest\Grids\Components\OneCellRow;
    use Nayjest\Grids\Components\RecordsPerPage;
    use Nayjest\Grids\Components\RenderFunc;
    use Nayjest\Grids\Components\ShowingRecords;
    use Nayjest\Grids\Components\TFoot;
    use Nayjest\Grids\Components\THead;
    use Nayjest\Grids\EloquentDataProvider;
    use Nayjest\Grids\FieldConfig;
    use Nayjest\Grids\FilterConfig;
    use Nayjest\Grids\Grid;
    use Nayjest\Grids\GridConfig;

    class UsersController extends Controller
    {

    public function Index()
    {

    $query = (new User)
    ->query();

    $grid = new Grid(
    (new GridConfig)
    ->setDataProvider(
    new EloquentDataProvider($query)
    )
    ->setName(‘UserIndex’) /* grid unque id */
    ->setPageSize(20)
    ->setColumns([
    (new FieldConfig)
    ->addFilter(
    (new FilterConfig)
    ->setOperator(FilterConfig::OPERATOR_LIKE)
    )
    ->setLabel(‘ID’)
    ->setName(‘id’) /* field */
    ->setSortable(true)
    ->setSorting(Grid::SORT_DESC)
    ,
    (new FieldConfig)
    ->addFilter(
    (new FilterConfig)
    ->setOperator(FilterConfig::OPERATOR_LIKE)
    )
    ->setLabel(‘Name’)
    ->setName(‘name’)
    ->setSortable(true)
    ,
    (new FieldConfig)
    ->addFilter(
    (new FilterConfig)
    ->setOperator(FilterConfig::OPERATOR_LIKE)
    )
    ->setLabel(‘Email’)
    ->setName(’email’)
    ->setSortable(true)
    ,
    (new FieldConfig)
    ->addFilter(
    (new FilterConfig)
    ->setOperator(FilterConfig::OPERATOR_LIKE)
    )
    ->setLabel(‘Update Information’)
    ->setName(‘count_update’)
    ->setSortable(true)
    ,
    (new FieldConfig)
    ->setLabel(‘Created’)
    ->setName(‘created_at’)
    ->setSortable(true)
    ,
    (new FieldConfig)
    ->setLabel(‘Last Update’)
    ->setName(‘updated_at’)
    ->setSortable(true),
    ])
    ->setComponents([
    (new THead)
    ->setComponents([
    (new ColumnHeadersRow),
    (new FiltersRow)
    ->addComponents([
    (new RenderFunc(function () {
    return HTML::style(‘js/daterangepicker/daterangepicker-bs3.css’)
    . HTML::script(‘js/moment/moment-with-locales.js’)
    . HTML::script(‘js/daterangepicker/daterangepicker.js’)
    . "<style>
    .daterangepicker td.available.active,
    .daterangepicker li.active,
    .daterangepicker li:hover {
    color:black !important;
    font-weight: bold;
    }
    </style>";
    }))
    ->setRenderSection(‘filters_row_column_created_at’),
    (new DateRangePicker)
    ->setName(‘created_at’)
    ->setRenderSection(‘filters_row_column_created_at’)
    ->setDefaultValue([‘1990-01-01’, date(‘Y-m-d H:i:s’)]),
    (new DateRangePicker)
    ->setName(‘created_at’)
    ->setRenderSection(‘filters_row_column_updated_at’)
    ->setDefaultValue([‘1990-01-01’, date(‘Y-m-d H:i:s’)]),
    ])
    ,
    (new OneCellRow)
    ->setRenderSection(RenderableRegistry::SECTION_END)
    ->setComponents([
    new RecordsPerPage,
    new ColumnsHider,
    (new CsvExport)
    ->setFileName(‘usersExport_’ . date(‘Y-m-d’))
    ,
    (new ExcelExport())
    ->setFileName(‘usersExport_’ . date(‘Y-m-d’))
    ,
    (new HtmlTag)
    ->setContent(‘<span class="glyphicon glyphicon-refresh"></span> Filter’)
    ->setTagName(‘button’)
    ->setRenderSection(RenderableRegistry::SECTION_END)
    ->setAttributes([
    ‘class’ => ‘btn btn-success btn-sm’,
    ]),
    ]),
    ])
    ,
    (new TFoot)
    ->setComponents([
    (new OneCellRow)
    ->setComponents([
    new Pager,
    (new HtmlTag)
    ->setAttributes([‘class’ => ‘pull-right’])
    ->addComponent(new ShowingRecords)
    ,
    ]),
    ])
    ,
    ])
    );
    $grid = $grid->render();

    return view(‘users.index’, [
    ‘grid’ => $grid,
    ]);
    }

    }
    [/code]

  2. สร้าง view \resources\views\users\index.blade.php[code language=”php” title=”\resources\views\users\index.blade.php”]@extends(‘layouts.app’)

    @section(‘title’, ‘Users’)

    @section(‘main-content’) <a href="{{ url(‘/excels/form’) }}">
    <button class="btn btn-success btn-sm"><span class="glyphicon glyphicon-upload"></span> Import Datas</button>
    </a> @if(!empty($text))
    <div class="container">{!! $text !!}</div>
    @endif
    <?=$grid;?>
    @stop[/code]

  3. สร้าง route โดยไปที่ \app\Http\routes.php[code language=”php” title=”\app\Http\routes.php”]

    Route::get(‘/users/index’, ‘[email protected]’);
    …[/code]

ทดลองโดยเรียก /users/index ในเครื่องผมคือ http://localhost/laravel52/public/users/index grid แสดงข้อมูลออกมาแล้วแต่ จะส่งออกเป็นไฟล์ csv หรือ excel ยังไงละ ใจเย็นๆครับ อ่าน สร้าง grid ใน laravel แบบ advance ครับ

สร้าง grid ใน laravel

ตัว laravel จะไม่มี grid ติดตั้งมาให้ในตัวครับ ถ้าจะใช้ต้องลงเอง ผมเลือกใช้ คือ Nayjest Grids เพราะว่าใช้ bootstrapt เป็นพื้นฐานใช้งานง่าย ไม่ยากจนเกินไป สามารถส่งออกเป็นไฟล์ .csv และ excel ได้ ถึงจะไม่ได้เป็นแบบ ajax แต่เจ้าของโครงการก็ประกาศว่าจะทำ และดูจากการ update แล้วถือว่าถี่มากคงอีกไม่นานก็ได้ใช้

ติดตั้งโดย

  1. เปิดไฟล์ composer.json เพิ่ม[code language=”php” title=”composer.json”]

    "laravelcollective/html": "^5",
    "nayjest/grids": "^1.3.1"

    [/code]
  2. Command[code language=”text”]composer update[/code]
  3. เปิดไฟล์ \config\app.php เพิ่ม [code language=”php” title=”\config\app.php”]
    ‘providers’ => [

    ‘Collective&#92;Html&#92;HtmlServiceProvider’,
    ‘Nayjest&#92;Grids&#92;ServiceProvider’,

    ‘aliases’ => [

    ‘Form’ => ‘Collective&#92;Html&#92;FormFacade’,
    ‘Grids’ => ‘Nayjest&#92;Grids&#92;Grids’,
    ‘HTML’ => ‘Collective&#92;Html&#92;HtmlFacade’,
    ‘Input’ => ‘Illuminate&#92;Support&#92;Facades&#92;Input’,

    ][/code]

สร้าง grid โดย

  1. [code language=”php” title=”\app\Http\Controllers\UsersController.php”]
    <?php

    namespace App&#92;Http&#92;Controllers;

    use App&#92;Http&#92;Controllers&#92;Controller;
    use Grids;

    class UsersController extends Controller
    {
    public function index()
    {
    $grid = [
    ‘src’ => ‘App&#92;User’,
    ‘columns’ => [
    ‘id’,
    ‘name’,
    ’email’,
    ‘created_at’,
    ‘updated_at’,
    ],
    ];
    echo Grids::make($grid);
    }

    }
    [/code]

  2. สร้าง route โดยไปที่ \app\Http\routes.php[code language=”php” title=”\app\Http\routes.php”]

    Route::get(‘/users/index’, ‘[email protected]’);
    …[/code]

ทดลองโดยเรียก /users/index ในเครื่องผมคือ http://localhost/laravel52/public/users/index grid แสดงข้อมูลออกมาแล้วแต่ จะส่งออกเป็นไฟล์ csv หรือ excel ยังไงละ ใจเย็นๆครับ อ่าน สร้าง grid ใน laravel แบบ advance ครับ

การตัดคำใน wordpress สั้นเกินไป

หลังจาก update theme แล้วหน้า index บนความแสดงตัวอย่างเนื้อหาสั้นจุ๊ดจู๋ เหลือแค่ 2 บรรทัด แต่ละหัวข้อถ้าไม่ดูชื่อเรื่อง อ่านยังไงก็ไม่รู้เรื่องเลยว่าเขียนเกี่ยวกับอะไร แล้วใครจะเข้าไปอ่าน

วิธีแก้คือ

  1. ftp เข้าไปที่ wordpress/wp-content/languages
  2. แล้วลบไฟล์ th.mo ทิ้งไป
  3. refresh ดู หายแล้ว ^_^

แต่ก็สงสัยต่อไป ไฟล์ th.mo คืออะไร? เปิดของเก่าดูอ่านไม่รู้เรื่อง แต่ก็เป็นระเบียบเกินกว่าจะเป็น cache file ค้นพี่เกิ้ลไปก็ไปเจอ Installing WordPress in Your Language เข้าใจว่ามันคือไฟล์แปลภาษานั่นเอง ดูใน ftp มันไม่ได้สร้างให้ใหม่ ตามที่ตอนแรกคิดว่าเป็นเป็นแค่ cache file ไม่อยากเอากลับมาจาก backup ก็เอามาจาก

  1. Translate WordPress ภาษาไทยแปลเรียบร้อย 100% เลยขอบคุณทีมงานมากๆ (มีรายชื่อทีมงานในหน้าต่อไป) กดตรง Details
  2. โหลดไฟล์ตรง “Download language pack”
  3. ไปที่ /wp-admin/plugin-install.php?tab=upload อัพโหลดไฟล์ขึ้นไป แล้วกด “ติดตั้งตอนนี้” เลยครับ

เสร็จแล้ว ขอบคุณบทความ หน้าแรก WordPress ตัดคำสั้นไป !!! สำหรับวิธีแก้ปัญหา