Passing data from gridview button to modal window - php

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

Related

Delete button in CGrid view in Yii

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

Yii 2 No Refresh on SideNav Click

I have a SideNav in my bizadmin.php view:
<div class="row">
<div class="col-xs-3" id="sidenav">
<?php
echo SideNav::widget([
'type' => 'default',
'encodeLabels' => false,
//'heading' => $heading,
'items' => [
['label' => 'Add Staff', 'icon' => 'user', 'url' => ['/user/index']],
['label' => 'Store Configuration', 'icon' => 'cog', 'url' => ['/store/index']],
['label' => 'Add Transaction', 'icon' => 'duplicate', 'url' => ['/transaction/index']],
['label' => 'Add Account', 'icon' => 'book', 'url' => ['/account/index']],
],
]);
?>
</div>
<div class="col-xs-7">
<h2 id="bizstorename">Store Name</h2>
<h5>This is a store description. You can put anything here as long as it describes your store.</h5>
</div>
</div>
It looks like this:
Is there a way that when I click an item on my SideNav, it will NOT redirect to another page, but instead, a page will just be loaded inside a div (for example, in my case, inside <div class="col-xs-7">) beside the SideNav without refreshing the entire page.
I think I need to use jQuery or Ajax of some sort but I don't know how. Please let me know your thoughts.
Use jQuery's .load method to fetch a page and then append to #result:
$script = "
$('#sidenav ul li a').on('click', function(e) {
e.preventDefault();
var url = $(this).attr('href');
$('#result').load(url);
});
";
$this->registerJs($script, $this::POS_READY);
See jQuery's docs for more info.

Remain Input Field Contents When Error Occurs Upon Submission or After Refreshing Page

I have a form where user can input Start and End Dates. I used a date picker for this. I want that when the user refreshes the page or when the user submitted the form but is redirected to the same page because of an error, those inputted Start and End Dates should remain in place. In my case, the inputted data disappear upon refresh or when error occurs upon submission.
Here's my view:
<div class="info">
<label class="col-lg-2 form-label"><b>Start Date</b></label>
<div class="col-lg-4" >
<?php $this->widget('zii.widgets.jui.CJuiDatePicker',
array('name' => 'FormDetails_date_fr',
'options' => array(
'showAnim' => 'blind',
'beforeShowDay' => 'js:$.datepicker.noWeekends',
'minDate' => 'date("Y-m-d", strtotime("+1 day")',
'changeMonth' => true,
'changeYear' => true,
'dateFormat' => 'yy-mm-dd',
),
'htmlOptions' => array(
'class' => 'form-control A',
//'id' =>'A',
),
)); ?>
</div>
</div>
<div class="info">
<label class="col-lg-2 form-label"><b>Start Date</b></label>
<div class="col-lg-4" >
<?php $this->widget('zii.widgets.jui.CJuiDatePicker',
array('name' => 'FormDetails_date_to',
'options' => array(
'showAnim' => 'blind',
'beforeShowDay' => 'js:$.datepicker.noWeekends',
'minDate' => 'date("Y-m-d", strtotime("+1 day")',
'changeMonth' => true,
'changeYear' => true,
'dateFormat' => 'yy-mm-dd',
),
'htmlOptions' => array(
'class' => 'form-control C',
//'id' =>'A',
),
)); ?>
</div>
</div>
I am using Yii 1.1 PHP Framework.
Any ideas anyone? Please help.
I've checked zii.widgets.jui.CJuiDatePicker functionality. Seems to me it is not designed for retaining inputted value.
Bypass solution might be:
Have an ajax call upon value input by user to save inputted value somewhere (as a model attribute or an app parameter or session parameter). So put something like this (taken from Select2 example) in the datepicker config:
'htmlOptions'=>array(
'onChange'=>CHtml::ajax(array('type'=>'POST',
'url'=>'/',
'data'=>'js:this.value',)),
),
In the datapicker config you define value based on saved parameter:
$this->widget('zii.widgets.jui.CJuiDatePicker',
array('name' => 'FormDetails_date_to',
'value'=>$model->attributeName,
// 'value'=>Yii->app()->params['datepickerTempValue'],
thus a saved value will be inserted upon page reload.

Multiple Submit buttons in cakephp form

I have a form in a cakephp view which saves well with a single button, here is the code in the view book_form.ctp
echo $this->Form->create
(
'Book',
array
(
'url' => array
(
'controller' => 'Books',
'action' => 'save_record'
),
'class' => 'span12 the_ajaxform',
'inputDefaults' => array
(
'label' => false,
'error' => false
)
)
);
.
.
// form fields
.
.
$options =array(
'label' => __('Save'),
'class' => 'btn btn-primary',
'id'=>'saveform'
);
echo $this->Form->end($options);
.
.
This works perfect! Now i wanted to add two buttons on that form and this is what i did
$options =array(array(
'label' => __('Save & Close'),
'class' => 'btn btn-primary',
'id'=>'saveform'
),
array(
'label' => __('Save & Create New'),
'class' => 'btn btn-primary',
'id'=>'saveformnew'
)
array(
'label' => __('Cancel'),
'class' => 'btn btn-primary',
'id'=>'formcancel'
));
echo $this->Form->end($options);
But this only brings one button which wont even submit the form,where am i going wrong?
and can each button call a different method in the controller?
Thanks in advance
If you set the name of the submit button, it will have that as a key in the post data, so you can redirect using that info at the start of your action. e.g.
<?php echo $this->Form->submit('btn1value', array('name'=>'btn1'))?>
<?php echo $this->Form->submit('btn2balue', array('name'=>'btn2'))?>
clicking the first button will give post data like:
array(
[btn1] => btn1value
[YourModel] => array(...)
)
Which makes it easy to do something like:
if (isset($this->request->data['btn1'])) {
// btn1 was clicked
} else if (isset($this->request->data['btn2'])) {
// btn2 was clicked
}
I am not sure whether it is "Technically Correct", HTML4, 5 compatible or not etc. but I have always done it something like this, without any problem so far:
<?php echo $this->Form->submit('Delete it', array('name'=>'User[formaction]')); ?>
<?php echo $this->Form->submit('Undelete Selected', array('name'=>'User[formaction]')); ?>
<?php echo $this->Form->submit('Purge Selected', array('name'=>'User[formaction]')); ?>
where "User" is the model name.
Usually one form can have just one action
this lmnitation is no longer true in HTML5 where you can set the form action for every button
so: the following code works only for HTML5 browsers
echo $this->Form->button(
'Your Action Description Here',
array(
'type' => 'submit',
'formaction' => 'yourActionHere' //
)
);
Try this, This is easy to do.
<div class="submit">
<?php echo $this->Form->submit(__('Submit', true), array('name' => 'ok', 'div' => false)); ?>
<?php echo $this->Form->button('Cancel', array('type' => 'button'));?>
Try using the FormHelper's button function to create the submit button and the other buttons and just call end after that without any options. This will output the buttons and end your form for you.
See: FormHelper::button
e.g.:
echo $this->Form->button('Save & Close', array('type' => 'submit'));
echo $this->Form->button('Save & Create New', array('type' => 'button'));
echo $this->Form->button('Cancel', array('type' => 'reset'));

links__system_main_menu in drupal

what is the use of links__system_main_menu in drupal?
<?php if ($main_menu): ?>
<div id="main-menu" class="navigation">
<?php print theme('links__system_main_menu', array(
'links' => $main_menu,
'attributes' => array(
'id' => 'main-menu-links',
'class' => array('links', 'clearfix'),
),
'heading' => array(
'text' => t('Main menu'),
'level' => 'h2',
'class' => array('element-invisible'),
),
)); ?>
</div> <!-- /#main-menu -->
This is a pattern for theme hook, in the form [base hook]__[context]. When links theme with theme('links__system_main_menu', $variables), theme() function search for *theme_links__system_main_menu()* and use it. Otherwise, if it doesn't find, it will use *theme_links()*. For more information check the theme Doc

Categories