Save data from gridview to another table yii2 - yii

I have a gridview with filtered data. How can I export data to another table, like ExportMenu from kartik?
<?= GridView::widget([
'options' => ['width' => '70'],
'dataProvider' => $dataProvider,
echo ExportMenu::widget([
'dataProvider' => $dataProvider,
'columns' => $gridColumns
]);
?>

if you have a gridview with filterd data this mean that you have $dataProvider
simmply try using render to the new view (if the table is in an other view) or in the new girdview
return $this->render('my_other_view', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
or call an action passing the dataProvider as a param
Url:to(['/your_controller/action', 'dataProvider' => $dataProvider);
each dataProvider contain a models attribute that list all the model related to the dataProvider so if you need save this models in a new table you could use this code in you action
foreach( $dataProvider->models as $model) {
$myModel = new MyModel();
$myModel->att1 = $model->att1;
$myModel->att2 = $model->att2;
......
$myModel->save();
}

Related

Nested Whgridview. Grids disappear on sort or pagination click

I'm a newbie in Yii programming.
I'm using boostrap library on Yii via Yiistrap/Yiiwheels
I've created a relation table view
The related view is a Whgridview itself
The first (master grid) has a TbRelationColum clicking it i display the second grid (detail grid).
When I click on the row to display the sub grid, everything appears ok. When I change the sort order or the page of the sub grid disappear both grid.
I understand we should differentiate the css class of the pager and the sort of sub grid from the main grid. How to do this specifically in Yii-Way?
Is This the problem?
This is the view of the main grid:
$this->widget('yiiwheels.widgets.grid.WhGridView',array(
'id'=>'masterGrid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'template' => "{summary}{items}<div class=\"row-fluid\"><div class=\"pull-right\">{pager}</div></div>",
'type' => array(TbHtml::GRID_TYPE_BORDERED, TbHtml::GRID_TYPE_STRIPED),
'columns'=>array(
array(
'class' => 'yiiwheels.widgets.grid.WhRelationalColumn',
//'name' => 'multiMembers.id',
'type' => 'raw',
'header' => 'Sub Items',
'url' => $this->createUrl('multiGroup/ajaxSubItems'),
'cacheData' => false,
'value' => "CHtml::tag('button',array('class'=>'btn btn-primary'),'Sub Items')",
'htmlOptions'=>array('style'=>'width:90px;'),
'cssClass' => 'showSubItems',
),
'id',
'title',
array(
'class'=>'bootstrap.widgets.TbButtonColumn',
),
),
));
This is the sub-grid:
echo CHtml::tag('h3',array(),'Sub Items Group #"'.$id.'"');
$this->widget('yiiwheels.widgets.grid.WhGridView', array(
'id'=>'subGrid_'.$id,
'type'=>array(TbHtml::GRID_TYPE_BORDERED, TbHtml::GRID_TYPE_STRIPED),
'dataProvider' => $gridDataProvider,
'template' => "{summary}{items}<div class=\"row-fluid\"><div class=\"pull-right\">{pager}</div></div>",
'columns' => $gridColumns,
));
This is the controller:
/**
* Manages all models.
*/
public function actionAdmin()
{
$model=new MultiGroup('search');
$model->unsetAttributes(); // clear any default values
if (isset($_GET['MultiGroup'])) {
$model->attributes=$_GET['MultiGroup'];
}
$this->render('admin',array(
'model'=>$model,
));
}
public function actionAjaxSubItems()
{
$id = Yii::app()->getRequest()->getParam('id');
$model = $this->loadModel($id);
if($model->numSubItems > 0) {
$this->renderPartial('_child', array('id' => $id,
'gridDataProvider' => $this->getGridDataProvider($id),
'gridColumns' => $this->getGridColumns()
), false, true);
} else {
echo 'Non ci sono Sub Items.';
}
}
public function getGridDataProvider($id) {
$sql = 'SELECT * FROM multi_member WHERE groupid = :groupid ORDER BY lastname,firstname';
$cmd = Yii::app()->db->createCommand($sql);
$cmd->bindParam(':groupid', $id, PDO::PARAM_INT);
$result = $cmd->queryAll();
$dataProvider = new CArrayDataProvider(
$result, array(
'sort' => array(
'attributes' => array('id','groupid','firstname','lastname','membersince'),
'defaultOrder' => array('lastname' => CSort::SORT_ASC, 'firstname' => CSort::SORT_ASC),
),
'pagination' => array(
'pageSize' => 2,
),
));
return $dataProvider;
}
public function getGridColumns() {
return array('id', 'lastname', 'firstname', 'membersince');
}
How can I do?
thank you ..
If the extensions you're using all extend CGgridView, then you should be able to use option 'ajaxUpdate' (link to documentation).
Try setting 'ajaxUpdate'=>false in one of the grids (or in both of them) to see whether it helps you.
Sometimes setting ajaxUpdate to false is the only way I get get some grids to behave the way I want them to...

Yii, filtering and ordering column in grid view that has data from custom model function

This is follow up on this question:
Display related has_many data inside once cell in Yii TbExtendedGridView
I got that cell working, but now i have no idea how to make it sortable and filterable (filter field is hidden).
View:
$this->widget('bootstrap.widgets.TbExtendedGridView', array(
'id'=>'user-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
'user_name',
'favorite_color',
array(
'value'=>'$data->getAllDates()',
),
),
));
One user can have many Dates that can be single date or date range, so i have getAllDates function that fetches em all and put em all inside string so they can be represented inside single cell for that user.
Model:
public function relations()
{
return array(
'listofdates' => array(self::HAS_MANY, 'Dates', 'user_id'),
);
}
public function getAllDates()
{
$data = '';
foreach ($this->listofdates as $date) {
$data .= $date->start_date.'-'.$date->end_date;
}
return $data;
}
I have no idea how to enable filtering and search for dates column. There is no even title for it or filter field.
I can enable filter field for that column by adding 'name' => 'whatever', but of course there is no single column in database for that data so i get MYSQL error.
I'm guessing i need to create another custom function for filtering but i have no idea where to start.
you can create an additional field for filtering and sorting $datesFilter and filter like this:
in model:
public $datesFilter;
public function rules()
{
array(
array('datesFilter', 'safe', 'on'=>'search')
);
}
public function search()
{
$criteria=new CDbCriteria;
...
// filtering
if ($this->datesFilter!==null)
$criteria->addCondition('YOUR QUERY CONDITION');
...
// sorting
$sort = new CSort;
$sort->attributes =
array(
...
'datesFilter'=>=>array('asc'=>'YOUR SORT ASC', 'desc'=>'YOUR SORT DESC'),
);
}
in view:
$this->widget('bootstrap.widgets.TbExtendedGridView', array(
'id'=>'user-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
'user_name',
'favorite_color',
array(
'value'=>'$data->getAllDates()',
'name'=>'datesFilter',
),
),
));

Adding CbuttonColumn to Csv Header Column array (Used in CgridView) - Yii Framework

I am trying to add Delete button to CgridView , I am displaying CgridView columns from CSV file headers . I am storing Csv columns names in array, Now i want to add one more button column to delete the record.
Here is my code,
<?php
$file = fopen('D:/xampp/htdocs/ccvv7/images/importcsv/load.csv', 'r');
$data = array();
while (($line = fgetcsv($file)) !== FALSE) {
$data[] = $line;
}
fclose($file);
$columns = array();
foreach ($data[0] as $key => $value) {
$columns[] = array(
'name' => $key,
'header' => $value,
);
}
$data = array_slice($data, 1);
$dataProvider = new CArrayDataProvider($data, array(
'keyField' => 0,
));?>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id' =>'BCImported-grid',
'dataProvider' => $dataProvider,
'columns' => $columns,
)); ?>
Please look into the code, now i need to add one more button column to $columns array
Add this to $column :
array(
'class'=>'CButtonColumn',
'template'=>'{delete}',
'deleteButtonUrl'=>'Yii::app()->controller->createUrl("delete",array("id"=>$data["id"]))',
),

Yii CGridView multiple dataProvider

It's possible put two o more parameters to the "dataProvider" attribute in CGridView?
Example:
$dataProvider=new CActiveDataProvider('Gallery', array(
'criteria'=>array(
'condition'=>'type=1',
),));
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'gallery-grid',
'dataProvider' => array($dataProvider, $model->search()),
'filter' => $model));
I want to put $dataProvider and $model->search() in the same filter, it is possible?
I guess no, but you can probably use another way:
$dataProvider = $model->search();
$dataProvider->criteria->addCondition('type=1');
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'gallery-grid',
'dataProvider' => $dataProvider,
'filter' => $model
));

Yii FullcalendarGraphWidget how to provide $model->search(); result to the FullCalendar?

Instead of CGridView I'm using fullCalendar extension to display my data in the calendar as an event. Along with that I've filter option.
In CGridView we can write 'dataProvider'=>$model->search(), to get the search data. Similarly how can I provide the same data to the fullCandar extension.
I'm doing it by fetching the data in the Controller as following and transforming it to an array in the shape which the fullcalendar expects:
public function actionCalendar()
{
$data=CalendarEntry::model()->findAll();
$model = array();
foreach ($data as $item) {
array_push($model, array(
'id' => $item->id,
'title' => $item->datetext,
'start' => $item->datedate,
'url' => CController::createUrl('calendar/view', array ('id' => $item->id)),
));
};
$this->render('calendar',array('model'=>$model));
}
And in my calendar view I'm then just setting the model:
$this->widget('application.extensions.fullcalendar.FullcalendarGraphWidget',
array(
'data'=> $model,
'htmlOptions'=>array(
'style'=>'width:800px;margin: 0 auto;'
),
)
);