Datatable icon indicating copy to clipboard operation
Datatable copied to clipboard

[CollectionProvider] Returned columns are empty

Open timgws opened this issue 9 years ago • 6 comments

I am using https://github.com/OpenSkill/Datatable/commit/d765c9f485ad94779e0c2b35deb7f22c26fe3635

Here is my controller code:

class DatatableTestController extends Controller
{
    public function test() {
        $t = Datatable::make(new CollectionProvider(Categories::all()))
            ->column('id') // show the id column of the user model
            ->column('name', null, Searchable::NONE(), Orderable::NONE()) // also show the full name of the user, but do not allow searching or ordering of the column
            ->build();

        if ($t->shouldHandle()) {
            return $t->handleRequest();
        }

        return view('admin.test.categories', array(
            'datatable' => $t->view()
        ));
    }
}

admin/test/categories.blade.php has the following code:

    <h1>Testing Datatable 0.2.1</h1>

    {!!
        $datatable
            ->headers() // tell the table to render the header in the table
            ->columns('id', 'id') // show # in the header instead of 'id'
            ->columns('name', 'name') // show 'Full name' in the header instead of 'name'
            ->endpoint(route('admin.datatabletest'))
            // render just the table
            ->table()
    !!}
    {!!
        $datatable
            // now render the script
            ->script()
    !!}

The endpoint works perfectly :+1:

The AJAX request that is sent contains the following request parameters:

sEcho:1
iColumns:2
sColumns:
iDisplayStart:0
iDisplayLength:10
mDataProp_0:id
mDataProp_1:name
sSearch:
bRegex:false
sSearch_0:
bRegex_0:false
bSearchable_0:true
sSearch_1:
bRegex_1:false
bSearchable_1:true
iSortCol_0:0
sSortDir_0:asc
iSortingCols:1
bSortable_0:true
bSortable_1:true

and the response is:


{
    "sEcho": "1",
    "iTotalRecords": 5,
    "iTotalDisplayRecords": 5,
    "aaData": [{
        "id": "",
        "name": ""
    }, {
        "id": "",
        "name": ""
    }, {
        "id": "",
        "name": ""
    }, {
        "id": "",
        "name": ""
    }, {
        "id": "",
        "name": ""
    }]
}

Note the empty id, and name for each column, even though running dd(Categories::all()) shows that there is infact data inside the Collection.

(Note that my register function inside DatatableServiceProvider has been edited to force VersionEngine to only use Datatable19Version (I could not find a way to nicely have ->view render with the datatable19.blade.php Datatable javascript, but that's a story for another feature request).

timgws avatar Feb 22 '16 07:02 timgws

I thought it might have been caused by https://github.com/OpenSkill/Datatable/commit/5cfce36619ecf17bca6754a725bd1aa03eb9d855, however reverting this commit still shows the issue.

timgws avatar Feb 22 '16 07:02 timgws

I found the cause. compileCollection inside CollectionProvider calls $this->callable with the default function being set by ColumnConfigurationBuilder, however, all of them fail on a collection.

After adding the following to the list of if's inside checkCallable:

                if (is_object($data) && property_exists($data, 'attributes')) {
                    return $data->$name;
                }

I no longer get empty values returned.

timgws avatar Feb 22 '16 09:02 timgws

(Note that my register function inside DatatableServiceProvider has been edited to force VersionEngine to only use Datatable19Version (I could not find a way to nicely have ->view render with the datatable19.blade.php Datatable javascript, but that's a story for another feature request).

You can use the Datatable::view() function, which will take a view as parameters. You still need to provide your columns, but maybe we can add a function on the view to set the view or even the version.

What about two options: view($viewTemplate) and version($version) ?

Oh and we should write a test for that bug :)

Chumper avatar Feb 22 '16 11:02 Chumper

I will write a test for it in the morning. I want to find a more permanent/suitable solution than the one I have committed here too, if possible.

     ---- On Mon, 22 Feb 2016 22:08:49 +1100  [email protected]  wrote ----

(Note that my register function inside DatatableServiceProvider has been edited to force VersionEngine to only use Datatable19Version (I could not find a way to nicely have ->view render with the datatable19.blade.php Datatable javascript, but that's a story for another feature request).

You can use the Datatable::view() function, which will take a view as parameters. You still need to provide your columns, but maybe we can add a function on the view to set the view or even the version. —Reply to this email directly or view it on GitHub.

timgws avatar Feb 22 '16 13:02 timgws

@Chumper any idea for a better solution on https://github.com/timgws/OpenSkillDatatable/commit/8a1ff4f1522515e0f89770b9af1abfea34d87027 ?

timgws avatar Apr 27 '16 02:04 timgws

I have no answer offhand right now. I thought that a collection implements ArrayAccess and therefore should have a standard behaviour.

I guess we need to experiment with the default function if you are not happy with the current solution.

Chumper avatar Apr 27 '16 07:04 Chumper