Delete button in CGrid view in Yii - php

I tried to create a Delete button in CButtonColumn,The problem is when I triggered the delete,It shows me an error Your request is invalid..Can anyone tell me why this is happening....
My controller
public function actionDelete($id)
{
$this->loadModel($id)->delete();
// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if(!isset($_GET['ajax']))
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
}
And my view
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'replay-comment-grid',
'dataProvider'=>$model->search(),
// 'filter'=>$model,
'columns'=>array(
//'id',
//'comment_id',
'admin_replay',
array(
'class' => 'CButtonColumn',
'htmlOptions'=>array('width'=>'180px'),
'template' => '{view} {update} {delete}',
'buttons' => array(
'view'=>array(
'imageUrl' =>false,
'label' => 'View',
'url'=>'Yii::app()->createUrl("replayComment/view/$data->id")',
'options' => array('title'=>'view','class'=>'btn btn-success btn-xs'),
),
'update'=>array(
'imageUrl' =>false,
'label' => '',
'url'=>'Yii::app()->createUrl("replayComment/update/$data->id")',
'options' => array('title'=>'update','class'=>'btn btn-info btn-xs fa fa-pencil-square-o'),
),
'delete'=>array(
'imageUrl' =>false,
'label' => 'delete',
'url'=>'Yii::app()->createUrl("replayComment/delete/$data->id")',
'options' => array('title'=>'delete','class'=>'btn btn-danger btn-xs'),
),
)
),
),
'itemsCssClass'=>'table table-striped table-bordered table-hover',
'pagerCssClass'=>'pagination',
'pager'=>array( 'header' => '','lastPageLabel'=>'<span class="glyphicon glyphicon-chevron-right"></span><span class="glyphicon glyphicon-chevron-right"></span>','firstPageLabel'=>'<span class="glyphicon glyphicon-chevron-left"></span><span class="glyphicon glyphicon-chevron-left"></span>','prevPageLabel'=>'<span class="glyphicon glyphicon-chevron-left"></span>','nextPageLabel'=>'<span class="glyphicon glyphicon-chevron-right"></span>','header' => '','cssFile' => Yii::app()->baseUrl . '/css/pager.css','htmlOptions'=>array('class'=>'pagination'),'selectedPageCssClass'=>'active'),
)); ?>

Your code seems fine except for the url attribute, can you try:
'delete'=>array(
'imageUrl' =>false,
'label' => 'delete',
'url' =>'Yii::app()->createUrl("replayComment/delete", array("id"=>$data->id))',
'options' => array('title'=>'delete','class'=>'btn btn-danger btn-xs'),
),
the id parameter value will be passed in an array.
Yii create url
Also you shouldn't add in
'template' => '{view} {update} {delete}', use the button classes instead.

Did u properly setup your application CUrlManager to understand how to parse such urls ? test the controller and action fire correctly by directly type the url at the address bar rather try it from grid delete button.
ocalhost/sanmrc/replayComment/delete/2
something like
array(
'replayComment/delete/<id:\d+>'=>'replayComment/delete',
)
http://www.yiiframework.com/doc/guide/1.1/en/topics.url#user-friendly-urls

Related

YII how to set a custom delete button for ButtonColumn?

I would like to change the custom implementation of the delete button in gridview for buttoncolumn. I want to add a custom icon and a custom css class to the delete button. When i add the option parameter it dose not work any more. So i decided to create a custom link to the delete function but it gives me a 400 error when i click on it.
Any ideas ?
Below code
array(
'header' => __('Manage'),
'class' => 'booster.widgets.TbButtonColumn',
// 'htmlOptions'=>array('style'=>'white-space: nowrap;'),
'template' => '{approve} {details} {erase}',
'htmlOptions' => array('style' => 'white-space: nowrap;'),
'buttons' => array(
'approve' => array(
'label'=>__('Approve'),
'icon'=>'pencil',
// 'options' => array('target' => '_blank'),
'url' => 'Yii::app()->createUrl(\'tours/updateadmin/\'. $data->tour_id)',
'options' => array(
'class' => 'btn btn-small btn-info',
),
),
'details' => array(
'label'=>__('View Details'),
'icon'=>'check',
'url' => 'Yii::app()->createUrl(\'tours/view/\'. $data->tour_id)',
'options' => array(
'class' => 'btn btn-small btn-info',)
// 'options' => array('target' => '_blank'),
// 'url' => 'Yii::app()->createUrl(\'tours/updateadmin/\'. $data->tour_id)',
),
'erase' => array(
'label'=>__('Delete'),
'icon'=>'trash',
'url'=>'CController::createUrl("/tours/delete", array("id"=>$data->tour_id))',
'options' => array(
'class' => 'btn btn-small btn-info',)
),
),
),
What's your 400 error details? I think the problem is your link. Try to remove first / from your link:
'erase' => array(
'label'=>__('Delete'),
'url'=>'Yii::app()->createUrl("tours/delete", array("id"=>$data->tour_id))',
'options' => array(
'class' => 'btn btn-small btn-info'
)
),
),
If the problem did not resolve, you should edit your question and add error details for better helping.
Also, note that CButtonColumn#buttons does not have icon field. Here is a link that shows possible options for CButtonColumn#buttons . So you should remove icon field from all of your buttons.

Disabled button em CakePHP

[below translated from translate.google.com]
I'm looking for the syntax of how to make a button disabled in CakePHP and I can not get a result; My application need to first save a field for a button to finish the whole process after another button. The first button is a submit and redirects to the same page. The second button performs a function of the controller and go to the next process. I want to prevent the user to go to the next procedure without saving the first; I already have a variable that defines whether it is safe or not, just do not know how to make the Finish button is disabled;
Button code:
echo $this->Html->link(
$this->Html->tag('span', '', array('class' => 'glyphicon glyphicon-ok')) . " Finalizar",
array('controller' => 'Questoes','action' => 'limparSession'),
array('role' => 'button', 'class' => 'btn btn-success', 'escape' => false)
);
Add the disabled class to your button:
<?php
echo $this->Html->link(
$this->Html->tag('span', '', array('class' => 'glyphicon glyphicon-ok')) . " Finalizar",
array(
'controller' => 'Questoes',
'action' => 'limparSession'
),
array(
'role' => 'button',
'class' => 'btn btn-success disabled',
'escape' => false
)
);
?>
This is a bootstrap feature related to the given class.
If you want to do it without bootstrap:
<?php
echo $this->Html->link(
$this->Html->tag('span', '', array('class' => 'glyphicon glyphicon-ok')) . " Finalizar",
array(
'controller' => 'Questoes',
'action' => 'limparSession'
),
array(
'role' => 'button',
'class' => 'btn btn-success',
'disabled' => 'disabled',
'escape' => false
)
);
?>
echo $this->Form->button(
$this->Html->tag('span', '', array('class' => 'glyphicon glyphicon-ok'))." Finalizar",
array('type' => 'submit','onclick' => 'this.disabled=true;return true;',
'class' => 'btn disabled', 'escape' => false)
);

Listing only the particular data value in Tbgridview of yii

I have an attribute, idC, which may contain any positive integer number.
So, If idC has values like 2,3, and so on such that 3 comes 4 times and 2 comes 2 times.
Now, I want to list out data values with only idC=3 for one page
and idC=2 for another page and so on.
But how to do this.
Here is the listing code:
<?php
$this->widget('bootstrap.widgets.TbGridView',array(
'id'=>'Subjects-grid',
'dataProvider'=>$model->search(),
'type'=>'striped bordered condensed',
'template'=>'{summary}{items}{pager}',
'columns'=>array(
array('header'=>'<a>S.N.</a>',
'value'=>'$data->id_subjects',
'htmlOptions'=>array('width'=>'70px'),
),
'idC=$idC',
array(
'header'=>'<a>Subject</a>',
'value'=>'$data->subject',
),
'staff_type',
'sub_subject',
array(
'class'=>'bootstrap.widgets.TbButtonColumn',
'header'=>'<a>Actions</a>',
'template' => '{view} {update}',
'buttons' => array(
'view' => array(
'label'=> 'View',
'options'=>array(
'class'=>'btn btn-small view'
) ),
'update' => array(
'label' => 'Update',
'options' => array(
'class' => 'btn btn-small update'
)
),
),
'htmlOptions'=>array('nowrap'=>'nowrap'),
)
),
)); ?>
and here's the view page.

when I click view button in CGridView, it is opened in a new window

I used from CGridView in framework Yii, I want when I click view button , it is opened in a new window
how can I add of "_new" of target ?
Add 'options' => array('target'=>'_new') to CButtonColumn configuration array in CGridView
array(
'class'=>'zii.widgets.grid.CButtonColumn',
'template' => '{view}',
'buttons'=>array(
'view' => array(
'url' => '', // view url
'options' => array('target' => '_new'),
),
),
),
You can provide the html properties by using 'options' property.
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $dataProvider,
'columns' => array(
'table_field_1',
'table_field_2',
'table_field_3',
array(
'class' => 'CButtonColumn',
/* ===== Template to set the buutons. Ex: If you dont want delete link, remove {delete} */
//'template' => '{view} {update} {delete}',
'buttons' => array(
'view' => array(
'options' => array('class' => 'newWindow'),
),
),
),
),
));
?>
But, Opening a new window is browser dependent. With target="_blank" and target="_new" link will be opened in new tab in Mozilla, But in IE you will get new window. So user javascript to generate new window.
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $dataProvider,
'columns' => array(
'table_field_1',
'table_field_2',
'table_field_3',
array(
'class' => 'CButtonColumn',
/* ===== Template to set the buutons. Ex: If you dont want delete link, remove {delete} */
//'template' => '{view} {update} {delete}',
'buttons' => array(
'view' => array(
'options' => array('class' => 'newWindow'),
),
),
),
),
));
?>
Keep this jQuery in your .js file
<script>
$(document).ready(function()
{
$(".newWindow").click(function(e)
{
e.preventDefault();
var url=$(this).attr('href');
window.open(url, "_blank", "toolbar=no, scrollbars=yes, resizable=yes, top=100, left=100, width=1020, height=500");
});
});
</script>
You can use this
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $dataProvider,
'columns' => array(
'table_field_1',
'table_field_2',
'table_field_3',
array(
'class' => 'CButtonColumn',
/* ===== Template to set the buutons. Ex: If you dont want delete link, remove {delete} */
//'template' => '{view} {update} {delete}',
'buttons' => array(
'view' => array(
'options' => array('target' => '_blank'),
),
),
),
),
));
?>

Passing data from gridview button to modal window

I am trying to pass data from a button within a gridview to a modal window. I need to pass the ID of the record in order to be able to reference it after submitting the form within the modal window.
I am struggling with this quite a bit. First I need to be able to pass the ID variable to the modal, and then upon clicking the submit button make an ajax call to create a new record within the DB.
The Gridview
if(isset($results)){
$this->widget('bootstrap.widgets.TbExtendedGridView', array(
'id'=>'searchgrid',
'fixedHeader' => true,
'headerOffset' => 40, // 40px is the height of the main navigation at bootstrap
'type'=>'condensed',
'dataProvider'=>$results,
'responsiveTable' => true,
'template'=>"{items}",
'columns'=>array(
array('name'=>'title', 'header'=>'Name'),
array('name'=>'city', 'header'=>'City'),
array('name'=>'state', 'header'=>'State'),
array('name'=>'leads', 'header'=>'Leads', 'value'=>'Parkslist::model()->leadRange($data["leads"])'),
array('name'=>'pastbid', 'header'=>'Previous', 'value'=>'Parkslist::model()->pastBid($data["pasthighbid"])'),
array('name'=>'currentbid', 'header'=>'Current', 'value'=>'Parkslist::model()->highBid($data["currenthighbid"], $data["secondhighbid"], $data["countcurrenthighbid"])'),
array('name'=>'minimumbid', 'header'=>'Minimum', 'value'=>'Parkslist::model()->minimumBid($data["currenthighbid"], $data["secondhighbid"], $data["countcurrenthighbid"])'),
array('name'=>'userhighbid', 'header'=>'Your Bid'),
array('name'=>'placebid', 'header'=>'Bid', 'value'=>'CHtml::textField("bid" . $data["id"])', 'type'=>'raw'),
array('name'=>'report', 'header'=>'Report',
'value'=>function($data){
$this->widget('bootstrap.widgets.TbButton', array(
'label' => 'Click me',
'type' => 'primary',
'htmlOptions' => array(
'data-toggle' => 'modal',
'data-target' => '#myModal',
'data-id' => '$data["id"]',
),
));
}
),
),
));
}
The Modal
<?php
$this->beginWidget('bootstrap.widgets.TbModal', array('id' => 'myModal')); ?>
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h4>Why should this park be removed?</h4>
</div>
<form>
<div class="modal-body">
<select>
<option>Duplicate</option>
<option>Closed</option>
</select>
</div>
<div class="modal-footer">
<?php $this->widget('bootstrap.widgets.TbButton', array(
'type' => 'primary',
'buttonType'=>'submit',
'label' => 'Save changes',
'url' => '#',
'htmlOptions' => array('data-dismiss' => 'modal'),
)); ?>
<?php $this->widget('bootstrap.widgets.TbButton', array(
'label' => 'Close',
'url' => '#',
'htmlOptions' => array('data-dismiss' => 'modal'),
)); ?>
</div>
</form>
<?php $this->endWidget(); ?>
I was able to get this working. I would assume there might be a better solution but this seems to work.
First, inside of the button in the gridview I made the button ID = to the id of the record. Next, I created a javascript function called includeData and included the button ID.
Button Code
array('name'=>'report', 'header'=>'Report',
'value'=>function($data){
$this->widget('bootstrap.widgets.TbButton', array(
'label' => 'Click me',
'type' => 'primary',
'htmlOptions' => array(
'id'=>$data["id"],
'data-toggle' => 'modal',
'data-target' => '#myModal',
'data-id' => '$data["id"]',
'onClick' => 'includeData(this.id);'
),
));
}
),
JS Code
<script type='text/javascript'>
function includeData(parkid){
$('#reportparkid').val(parkid);
}
</script>
The JS function just sets the value of a hidden field equal to the buttonid. I would love to see some better ways to handle this.
Thanks

Categories