Tag Archive index

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

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

ติดตั้งโดย

  1. เปิดไฟล์ composer.json เพิ่ม
    ...
            "laravelcollective/html": "^5",
            "maatwebsite/excel": "~2.1.0",
            "nayjest/grids": "^1.3.1"
    ...
    
  2. Command
    composer update
  3. เปิดไฟล์ \config\app.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',
    ...
    ]

สร้าง grid โดย

  1. <?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,
            ]);
        }
    
    }
    
  2. สร้าง view \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
  3. สร้าง route โดยไปที่ \app\Http\routes.php
    ...
    Route::get('/users/index', 'UsersController@index');
    ...

ทดลองโดยเรียก /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 เพิ่ม
    ...
            "laravelcollective/html": "^5",
            "nayjest/grids": "^1.3.1"
    ...
    
  2. Command
    composer update
  3. เปิดไฟล์ \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',
    ...
    ]

สร้าง grid โดย

  1. <?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);
        }
    
    }
    
  2. สร้าง route โดยไปที่ \app\Http\routes.php
    ...
    Route::get('/users/index', 'UsersController@index');
    ...

ทดลองโดยเรียก /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 ตัดคำสั้นไป !!! สำหรับวิธีแก้ปัญหา

หาอินเด็กซ์ที่หายไป

ถ้าต้องการหาอินเด็กซ์ ที่หายไปในตารางที่มีอินเด็กซ์แบบ auto_increment ไว้หรือคีย์มีค่าแบบเรียงลำดับ 1,2,3,4…

index Alpha
1 a
2 b
4 c
5 e
6 f

ทำได้โดยเอาตารางมาจอยกับตัวเองโดยกำหนดให้ index น้อยกว่าอีกฝั่งอยู่ 1 ค่าแล้วหาว่าฝั่งขวามีข้อมูลรึเปล่า

SELECT *
FROM testTable  AS a
  LEFT JOIN testTable  AS b
    ON a.id = b.key_column - 1
WHERE b.key_column IS NULL;