MedicOneSystems / MedicOneSystems/livewire-datatables

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

Open
#357 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
1.2k
Forks
257
PR merge metrics
No merged PRs in 30d

Description

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(),
          
          ];
  }

}
`

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the PHP example in issue #357 and inspect the Column::callback entry point, especially callbacks receiving multiple fields and the reported PostgreSQL behavior. Confirm the expected syntax and image-rendering guidance, then document a working example; no documentation file or test is named in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
laravel, php, postgresql, tailwind
Domain
backend, documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.