sebastienheyd / boilerplate

Laravel AdminLTE 3 Boilerplate package with blade components, users, roles and permissions management
MIT License
209 stars 64 forks source link

Datatable checkbox should contains the reference data id #62

Closed cod3rshotout closed 2 years ago

cod3rshotout commented 2 years ago

I was testing the $dt->showCheckboxes() which is working well, but imho we should add the data id of the row, in this way we can store the correct record reference, code is better than any words:

 public function getColumns()
{
    $columns = $this->columns();

    if ($this->checkboxes) {
        $cbid = uniqid('checkbox_');
        array_unshift($columns, Column::add(
            '<div class="icheck-primary mb-0 mt-0">
                <input type="checkbox" name="dt-check-all" id="' . $cbid . '" autocomplete="off">
                <label for="' . $cbid . '"></label>
            </div>'
        )
            ->notSearchable()
            ->notOrderable()
            ->data('checkbox', function ($data) {
                $id = uniqid('checkbox_');
                $name = isset($data['id'])
                    ? 'dt-checkbox[' . $data['id'] . ']'
                    : 'dt-checkbox[]';

                return '<div class="icheck-primary mb-0">
                        <input type="checkbox" name="' . $name . '" id="' . $id . '" autocomplete="off">
                        <label for="' . $id . '"></label>
                    </div>';
            }));
    }

    return $columns;
}

As you can see I check if $data have the id, then I assign for each checkbox the id of the record. When sent a post request, I will get:

Immagine 2022-03-30 173314

Which represent the id of the rows selected, this id is associated can be associated to a pk of a table.

What do you think about it?

sebastienheyd commented 2 years ago

Id is now added by default as the reference to dt-checkbox.

But you can also use another field as reference if needed : see documentation here