MedicOneSystems / livewire-datatables

Advanced datatables using Laravel, Livewire, Tailwind CSS and Alpine JS
https://livewire-datatables.com/
MIT License
1.2k stars 259 forks source link

Here's how I solved this, because callback( ['name', 'type'] -- with two values, in postgres doesn't work. #357

Open bbarclay opened 2 years ago

bbarclay commented 2 years ago

I will share the error. I didn't save it, but I'm going to try and update. Here is an example though, I think it should be in the documentation. because I was unable to pass two fields through a calback.

This produces a row with an image, a row with an external link that has an svg link image, and an internal link with the slug. I couldn't use the name because of that issue of passing two fieleds through. Also, there didn't seem to be an easy way to render an image.

-- LET ME NOTE: Callback functions are amazing, I just hope I can figure out how to pass a couple fields through. Also, I'm pretty sure that the query builder is broken for Postgres, but I'll update that in another issue.

Screen Shot 2021-12-12 at 11 07 07 AM

I've tried 3 tables systems now. This has been the easiest to use so far.

`<?php

namespace App\Http\Livewire\Tables;

use App\Models\SiteAppCounts; use Mediconesystems\LivewireDatatables\Http\Livewire\LivewireDatatable; use Mediconesystems\LivewireDatatables\Column; class SiteAppCountsTable extends LivewireDatatable { public $model = SiteAppCounts::class; public $hideable = 'select'; public $exportable = true;

  public function columns()
  {
      //INSERT INTO "table_schema"."table_name" ("name", "count", "website", "slug", "icon", "categories") VALUES

      return [

          // Name column basic sortable

          // Name column basic sortable

          //column icon tyep ::callback return image from icons in /images/app_icons/icons folder
          Column::callback(['icon'], function ($icon) {
              return "<img src='/images/app_icons/icons/$icon' width='50px' height='50px'/>";
          }),

          Column::callback([ 'slug'], function ($slug) {
              $slug2 = $slug ;
              // name = remove - and upper case first letter
              // link = slug with - and /siteapp/sites/
              $slugrpl = str_replace('-', ' ', $slug2);
              $slugrpl = ucwords($slugrpl);

// return ''.$slugrpl.''; // link color blue return ''.$slugrpl.'';

              return $slug2;

          }),

         // SortBy this row and show counts as number with commas
         Column::callback(['count'], function ($count) {
             // make this 30000 to 30,000
             $count = number_format($count);
             return $count;
         })
          ->label('Count')
          ->sortBy('count', 'desc')
          ->filterable(),

          //website with link but exclude from export
          Column::callback(['website'], function ($website) {
             /* using tailwinds icons here
              convert website to a link open in a new tab
              use this svg icon next to the website */
              $svg = '<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 inline" fill="none" viewBox="0 0 24 24" stroke="currentColor">
              <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14" />
              </svg>';
              $website = "<a href='$website' target='_blank'> $svg   $website</a>";
              return $website;
               })
          ->label('Website')
          ->excludeFromExport()
          ->filterable(),

          //Hide in table but show in export.
          Column::name('website')
          ->label('Website')
          ->hide(),

          Column::callback(['categories'], function ($categories) {

           //   [{"id": 79, "name": "Geolocation", "slug": "geolocation"}, {"id": 80, "name": "Geolocation2", "slug": "geolocation2"}]
              $categories = json_decode($categories);
              $categories = array_map(function ($category) {
                  return $category->name;
              }, $categories);
              $categories = implode(', ', $categories);
              return $categories;  // return categories as a comma separated lists

          })
          ->filterable(),

          ];
  }

} `

abbasmashaddy72 commented 2 years ago

Please check the below link: https://github.com/MedicOneSystems/demo-livewire-datatables/blob/master/app/Http/Livewire/ActionsDemoTable.php

You will get the idea.

peeshypow commented 2 years ago

I am having the same problem. Something that should be very simple. Gives an error with Postgres.

Syntax error: 7 ERROR: syntax error at or near "`"

That is literally taking and copying the code you referred to him. As far as I can tell, two values are not possible with postgres. Is there any suggestions from the team or are we on our own to make ridiculous workarounds because we can only pass one value in callback.

You are 100% correct. I think this is the best option for tables but without the ability of passing two values with the callback option i have to start looking elsewhere for a solution. Which is a shame.