omines / datatables-bundle

DataTables bundle for Symfony
https://omines.github.io/datatables-bundle/
MIT License
262 stars 114 forks source link

Magic with option data and option field #154

Closed lbnvwork closed 4 years ago

lbnvwork commented 4 years ago

First sorry for my English. I have two tables with constraint OneToOne: AuthUser and Patient. And I am trying to concatinate AuthUser.firstName, AuthUser.lastName, AuthUser.patronomycName fields in one string in one column value. My entity now is Patient. I am making something like this: ->add( 'fio', TextColumn::class, [ 'label' => 'Full Name', 'data' => function ($value) { return (new PatientInfoService())->getFIO($value); } ] ) And it is works, until i add something from AuthUser like this: ->add( 'phone', TextColumn::class, [ 'label' => 'Phone', 'field' => 'AuthUser.phone' ] ) If I use option 'field' with value AuthUser.phone, I get into $value of my callback function... empty object Patient with empty object AuthUser where not null only fields "phone" and "id"!!! So, when I use my 'data' option, I must only use data option for constraint AuthUser, like this: ->add( 'phone', TextColumn::class, [ 'label' => 'Телефон', // 'field' => 'AuthUser.phone' 'data' => function ($value) { return $value->getAuthUser()->getPhone(); } ] ) I think it`s strange behavior...Please somebody explain me why it works so strange and how to do my task differently.

micotodev commented 4 years ago

You are probably better off doing this kind of thing in your template javascript. You can play with the columnDefs e.g.

columnDefs: [{
    "render" function(data, type, row) {
         return data + ' ' + row.[lastNameFieldName];
    },
"targets": [column number of your first name]
}

More info on the javascript https://datatables.net/reference/option/columnDefs

Then you can set 'visble' = false in the controller code of your last name field.

curry684 commented 4 years ago

Agreed with @michaelotoole, you're trying multiple kinds of magic simultaneously here. At some point you have to get explicit about what you want 😉