monkenWu / TablesIgniter

Tableslgniter is an addins base on CodeIgniter4. It will help you use jQuery Datatables in server side mode.
https://tablesigniter.monken.tw/
MIT License
14 stars 7 forks source link

Custom icon as an output on table #6

Closed firazyn closed 3 years ago

firazyn commented 3 years ago

Hello!

First thing to say is, thanks for this great library. It helped me a lot.

I tried to make the "Project Status" output with custom icons like this:

image

It can be done easily with a client-side, but I'm a bit confused on how to do it with the server-side (with your library, of course).

I tried with this code snippet, but it only returns a "Detail Button"

public function fetchProjectData()
    {
        $table = new TablesIgniter();
        $table->setTable($this->projectModel->noticeTable())
            ->setDefaultOrder('project_name', 'ASC')
            ->setOrder([
                'project_name', 'client_name', 'pm_name',
                'start_date', 'finish_date', 'project_status', null
            ])
            ->setSearch(['project_name', 'client_name', 'pm_name', 'project_status'])
            ->setOutput([
                'project_name', 'client_name', 'pm_name', 'start_date',
                'finish_date', $this->projectStatus('project_status'), $this->projectModel->button()
            ]);
        return $table->getDatatable();
    }

public function projectStatus($project_status)
    {
        if ($project_status == 'hold') {
            $closureFun = function () {
                return '<span class="badge rounded-pill bg-primary">Hold</span>';
            };
        } else {
            $closureFun = function () {
                return '<button type="button" class="btn btn-info">Detail</button>'; //detail button
            };
        }
        return $closureFun;
    }

My goal is, the output can be the custom icons I've made, just like when I return a Button with the closure style. I'm still learning web development with CI4..

Thanks in advance.

monkenWu commented 3 years ago

Happy Lunar New Year.

In the TableIgniter, setOutput() allows you to transmit arrays consists of the strings and anonymous function. If the array includings anonymous function, you should be aware of these three hints down below:

  1. TableIgniter will process every data fetched from the database on by one, and every time it will process the anonymous function you transferred.
  2. The anonymous function must only have a single parameter, and the parameter should be according to the database. This means you can do the logical judgments you need and the preprocessing for each row of data
  3. The return of the anonymous function must be a String, because the TableIgniter will combine the results into the frontend DataTables.

I made some changes to the codes you provided:

public function fetchProjectData()
{
    $table = new TablesIgniter();
    $table->setTable($this->projectModel->noticeTable())
        ->setDefaultOrder('project_name', 'ASC')
        ->setOrder([
            'project_name', 'client_name', 'pm_name',
            'start_date', 'finish_date', 'project_status', null
        ])
        ->setSearch(['project_name', 'client_name', 'pm_name', 'project_status'])
        ->setOutput([
            'project_name', 'client_name', 'pm_name', 'start_date',
            'finish_date', $this->projectStatus(), $this->projectModel->button()
        ]);
    return $table->getDatatable();
}

public function projectStatus()
{
    $closureFun = function ($row) {
        // $row should belong to the query results.
        if ($row['project_status'] == 'hold') {
            return '<span class="badge rounded-pill bg-primary">Hold</span>';
        } else {
            return '<button type="button" class="btn btn-info">Detail</button>'; 
        }
    }
    return $closureFun;
}
firazyn commented 3 years ago

Issue solved. Thank you! I will close this thread.

AnusiyaAnu commented 3 years ago

Hi

public function projectStatus() { $closureFun = function ($row) { // $row should belong to the query results. if ($row['status'] == '1') { return 'Hold'; } else { return ''; } } return $closureFun; }

getting syntax error, unexpected token "return"

Thanks