I would like the export file (expecially XLS) generated by Yii2-export to mimic gridview by exporting all columns in order selected in gridview configuration popup menu.
I mean, lets have two columns A and B. In gridview configuration menu (Little wrench icon) I set that B comes first. My dynagrid output looks like following:
B title| A title
-------|---------
B data | A data
However the export completely ignores this setting and outputs A as first column (because it is defined first in columns array passed as configuration):
A title | B title
---------|----------
A data | B data
In DB, I have table tbl_dynagrid which should contain configuration of gridview. I found corresponding record with id gridview_.
But the content of data column is not changing with reordering columns via gridview configuration.
Is there way how to load (preferably by PHP itself, without JS) order of columns and export to XLS file with that order in mind?
Thank you for help.
Update:
I found out, that gridview is connected to different database. Value of data column in table tbl_gridview is changing as expected after each customization.
This way, I need a way how to translate hashes used in gridview customization menu as column IDs to actual column names or so.
Actual code:
$dataProvider = //..
$pageName = //..
Columns array:
$columns = [
[ 'attribute' => 'col1', 'encodeLabel' => false, 'label' => 'Column A' ],
[ 'attribute' => 'col2', 'encodeLabel' => false, 'label' => 'Column B' ]
];
Export widget:
echo ExportMenu::widget([
'dataProvider' => $dataProvider,
'target'=>ExportMenu::TARGET_SELF,
'showConfirmAlert'=>false,
'container'=>['class'=>'myclass'],
'filename'=>'test',
'columns' => $columns,
'fontAwesome' => true,
'dropdownOptions' => [
'label' => Yii::t('layout','Export'),
'class' => 'btn btn-default'
]]);
And finally dynagrid:
$dynagrid = DynaGrid::begin([
'columns'=>$columns,
'theme'=>'simple-striped',
'showPersonalize'=>true,
'allowThemeSetting'=>false,
'allowFilterSetting'=>false,
'allowSortSetting'=>false,
'toggleButtonGrid'=>['class'=>'toggleButton'],
'gridOptions'=>[
'dataProvider'=>$dataProvider,
'options'=>['class'=>'myid'],
'filterModel'=>$searchModel,
'showPageSummary'=>false,
'floatHeader'=>false,
'pjax'=>false,
'toolbar' => [
['content'=>'{dynagridFilter}{dynagridSort}{dynagrid}'],
'{export}',
]
],
'options'=>['id'=>$pageName]
]);
All I want is to be able to export columns in order selected in gridview, not in the order they are set in $columns array.
The data exported are based on a dataProvider. based on this you should select the column in the .. sequence you need ..
you should build a dataProvider based on a select where you assign each column in the sequence you need ..
$yourDataProvider = YourModel::find()
->select('col1, col2, col3 ... ')
->where( ... )
....
Related
How do we make a select2_multiple sortable? I have a users table and a badges table. Naturally, I want to have a user_badge pivot table which will keep track of all the badges a user has and also sort it based on the order that I define using the select2_multiple.
Currently, I have my setupCreateOperation() setup as follows:
protected function setupCreateOperation()
{
$this->crud->addField([
'name' => 'badges_multi',
'label' => 'Badges',
'type' => 'select2_multiple',
'attribute' => 'internal_name',
'entity' => 'badges_multi',
'model' => 'App\Models\Badges',
'pivot' => true,
'sortable' => true,
'sortable_options' => [
'handle' => '.my-custom-handle',
],
]);
}
This returns the select2_multiple but the input is not sortable, i.e. I can drag and drop to rearrange. It only returns badges in alphabetical order
The select_multiple field does not have the ordering functionality.
To order you can use the select_and_order field or the repeatable field
You can also create a custom version of the select_multiple field that has the order functionality if you want to implement it that way.
Doing php artisan backpack:field select_multiple_with_order --from=select_multiple would create you a new file in your resources folder that is equal to Backpack select_multiple, from there you can add the functionality you need, and re-use it in other cruds if you need it there too.
Wish you the best.
everyone
I am tring to save the Multiple values from select option, but i don't have knowledge about how to save in laravel Backpack .I have been trying to solve the error from 2 days, but still it's not working from my side.
Please have a look, help would be appreciated.
Here is Screen shot of multiple select option ---
But it does not saving in db, but if i am going to edit the record , it gives this error ---
Here is my controller code
CRUD::addField([ // select2_from_ajax: 1-n relationship
'label' => "City Name", // Table column heading
'type' => 'select2_from_ajax_multiple',
'name' => 'location_id', // the column that contains the ID of that connected entity;
'entity' => 'location', // the method that defines the relationship in your Model
'attribute' => 'name', // foreign key attribute that is shown to user
'tab' => 'Texts',
'data_source' => url('api/location'), // url to controller search function (with /{id} should return model)
'placeholder' => 'Select location', // placeholder for the select
'include_all_form_fields' => true, //sends the other form fields along with the request so it can be filtered.
'minimum_input_length' => 0, // minimum characters to type before querying results
'dependencies' => ['state'], // when a dependency changes, this select2 is reset to null
// 'method' => 'GET', // optional - HTTP method to use for the AJAX call (GET, POST)
"allows_multiple" => true,
'pivot' => true,
]);
I have a table like this with 5 columns.
TableName
-Column1
-Column2
-Column3
-Column4
-Column5
I had merged them to display them in grid view as single column.
Question
How can i make filter condition query to search them based on user input.?
Eg.User types something as input, it have search from all the 5 columns and return the result based on the search input.(Sorting works fine, please help me with filtering)
If someone could helpme it would be great,
Thanks.
UPDATE:
$query->andFilterWhere(['ilike', '"x"."y"', $this->variantName]) ->andFilterWhere(['"a"."b"' => $this->ClassId])
->andFilterWhere(['"c"."d"' => $this->FamilyId])
->andFilterWhere(['"e"."f"' => $this->PlatformId])
->andFilterWhere(['ilike', '"g"."h"', $this->subFamilyName])
This is how my old model looks like the fields with familyId,classId,PlatformId are integer and subfamilyname,variantname are text.
Modified:
$query->andFilterWhere(['or',
['ilike', '"x"."y"', $this->Combo],
['"a"."b"' => $this->Combo],
['"c"."d"' => $this->Combo],
['"e"."f"' => $this->Combo],
['ilike', '"g"."h"', $this->Combo],
])
UPDATE 2:
This is how the query looked before merging columns.
->andFilterWhere(['ilike', '"storeNames"."variantName"', $this->variantName])
->andFilterWhere(['"storeNames"."classId"' => $this->malwareClassId])
->andFilterWhere(['"storeNames"."familyId"' => $this->malwareFamilyId])
->andFilterWhere(['"storeNames"."platformId"' => $this->malwarePlatformId])
->andFilterWhere(['ilike', '"storeNames"."subFamilyName"', $this->subFamilyName]);
I will add an example that uses a countries table to display data with GridView.
In order to accomplish what you need you have to take the following steps.
Create a custom attribute/field inside your SearchModel.
Add the field to gridview column.
Define the field as safe inside the rules.
update the search() function inside the SearchModel to search and compare based on the new custom field.
Let's say I have a Countries table with 2 models
Countries
CountriesSearch
The countries table has name and code field and i want to show country name and code inside a single column like below.
and i want that the filter field above on the gridview should be able to search if i type name or code any one of them.
Add field name $combo in the CountrieSearch
public $combo
add the field to the rules as safe
public function rules() {
return [
[ [ 'id' ] , 'integer' ] ,
[ [ 'name' , 'code' , 'combo' ] , 'safe' ] ,
];
}
Add the new field to the gridview
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'attribute'=>'combo',
'label'=>'Name/Code',
'value'=>function($model){
return '<span class="label label-success">'.$model->name.'</span><span class="label label-info">('.$model->code.')</span>';
},
'format'=>'raw',
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
Then update the search function and add the following lines just before you are returning the $dataProvider inside the search() method
$query->andFilterWhere ( [ 'OR' ,
[ 'like' , 'name' , $this->combo ],
[ 'like' , 'code' , $this->combo ],
] );
Hope this helps you out
WHen i try to do :
$fields = array('id' => 'custom_id', 'title' => 'some_name');
The result I get has id as a string.
If I do:
$fields = array('custom_id', 'title' => 'some_name');
then it gives custom_id as integer.
How can I obtain custom_id as id without loosing the data type. I read the documentation but didn't found much help.
There is something that virtual fields can do I think. But is it possible inside the find query without the use of virtual fields etc?
Thanks in Advance
As of CakePHP 3.2
you can use Query::selectTypeMap() to add further types, which are only going to be used for casting the selected fields when data is being retrieved.
$query = $table
->find()
->select(['alias' => 'actual_field', /* ... */]);
$query
->selectTypeMap()
->addDefaults([
'alias' => 'integer'
]);
You can use any of the built-in data types, as well as custom ones. In this case the alias field will now be casted as an integer.
See also
API > \Cake\Database\Query::selectTypeMap()
Cookbook > Database Access & ORM > Database Basics > Data Types
Cookbook > Database Access & ORM > Database Basics > Adding Custom Types
With CakePHP 3.1 and earlier
you'll have to use Query::typeMap(), which will not only affect the selected field when data is being retrieved, but in various other places too where data needs to be casted according to the field types, which might cause unwanted collisions, so use this with care.
$query
->typeMap()
->addDefaults([
'alias' => 'integer'
]);
See also
API > \Cake\Database\Query::typeMap()
Change the type of existing columns
Changing the type of an existing column of a table is possible too, however they need to be set using a specific syntax, ie in the column alias format used by CakePHP, that is, the table alias and the column name seprated by __, eg, for a table with the Articles alias and a column named id, it would be Articles__id.
This can be either set manually, or better yet retrieved via Query::aliasField(), like:
// $field will look like ['Alias__id' => 'Alias.id']
$field = $query->aliasField('id', $table->alias());
$query
->selectTypeMap()
->addDefaults([
key($field) => 'string'
]);
This would change the the default type of the id column to string.
See also
API > \Cake\Datasource\QueryInterface::aliasField()
Hi my alternative example full, user schema() in controller Users add type column aliasFiels by join data:
$this->Users->schema()
->addColumn('is_licensed', [
'type' => 'boolean',
])
->addColumn('total_of_licenses', [
'type' => 'integer',
]);
$fields = [
'Users.id',
'Users.username',
'Users.first_name',
'Users.last_name',
'Users.active',
'Users__is_licensed' => 'if(count(LicenseesUsers.id)>=1,true,false)',
'Users__total_of_licenses' => 'count(LicenseesUsers.id)',
'Users.created',
'Users.modified',
'Languages.id',
'Languages.name',
'Countries.id',
'Countries.name',
'UserRoles.id',
'UserRoles.name',
];
$where = [
'contain' => ['UserRoles', 'Countries', 'Languages'],
'fields' => $fields,
'join' => [
'LicenseesUsers' => [
'table' => 'licensees_users',
'type' => 'LEFT',
'conditions' => [
'Users.id = LicenseesUsers.users_id'
],
],
],
'group' => 'Users.id'
];
// Set pagination
$this->paginate = $where;
// Get data in array
$users = $this->paginate($this->Users)->toArray();
WHen i try to do :
$fields = array('id' => 'custom_id', 'title' => 'some_name');
The result I get has id as a string.
If I do:
$fields = array('custom_id', 'title' => 'some_name');
then it gives custom_id as integer.
How can I obtain custom_id as id without loosing the data type. I read the documentation but didn't found much help.
There is something that virtual fields can do I think. But is it possible inside the find query without the use of virtual fields etc?
Thanks in Advance
As of CakePHP 3.2
you can use Query::selectTypeMap() to add further types, which are only going to be used for casting the selected fields when data is being retrieved.
$query = $table
->find()
->select(['alias' => 'actual_field', /* ... */]);
$query
->selectTypeMap()
->addDefaults([
'alias' => 'integer'
]);
You can use any of the built-in data types, as well as custom ones. In this case the alias field will now be casted as an integer.
See also
API > \Cake\Database\Query::selectTypeMap()
Cookbook > Database Access & ORM > Database Basics > Data Types
Cookbook > Database Access & ORM > Database Basics > Adding Custom Types
With CakePHP 3.1 and earlier
you'll have to use Query::typeMap(), which will not only affect the selected field when data is being retrieved, but in various other places too where data needs to be casted according to the field types, which might cause unwanted collisions, so use this with care.
$query
->typeMap()
->addDefaults([
'alias' => 'integer'
]);
See also
API > \Cake\Database\Query::typeMap()
Change the type of existing columns
Changing the type of an existing column of a table is possible too, however they need to be set using a specific syntax, ie in the column alias format used by CakePHP, that is, the table alias and the column name seprated by __, eg, for a table with the Articles alias and a column named id, it would be Articles__id.
This can be either set manually, or better yet retrieved via Query::aliasField(), like:
// $field will look like ['Alias__id' => 'Alias.id']
$field = $query->aliasField('id', $table->alias());
$query
->selectTypeMap()
->addDefaults([
key($field) => 'string'
]);
This would change the the default type of the id column to string.
See also
API > \Cake\Datasource\QueryInterface::aliasField()
Hi my alternative example full, user schema() in controller Users add type column aliasFiels by join data:
$this->Users->schema()
->addColumn('is_licensed', [
'type' => 'boolean',
])
->addColumn('total_of_licenses', [
'type' => 'integer',
]);
$fields = [
'Users.id',
'Users.username',
'Users.first_name',
'Users.last_name',
'Users.active',
'Users__is_licensed' => 'if(count(LicenseesUsers.id)>=1,true,false)',
'Users__total_of_licenses' => 'count(LicenseesUsers.id)',
'Users.created',
'Users.modified',
'Languages.id',
'Languages.name',
'Countries.id',
'Countries.name',
'UserRoles.id',
'UserRoles.name',
];
$where = [
'contain' => ['UserRoles', 'Countries', 'Languages'],
'fields' => $fields,
'join' => [
'LicenseesUsers' => [
'table' => 'licensees_users',
'type' => 'LEFT',
'conditions' => [
'Users.id = LicenseesUsers.users_id'
],
],
],
'group' => 'Users.id'
];
// Set pagination
$this->paginate = $where;
// Get data in array
$users = $this->paginate($this->Users)->toArray();