index.php
<?php
Yii::app()->clientScript->registerScript('items_update',
"$('#dropit').change(function(){
$.ajax({
url: '".Yii::app()->createUrl('survey/questions')."',
data:
{
_designation : test,
},
type: 'POST',
success:function()
{
console.log('success');
},
error:function()
{
console.log('failure');
}
});
return false;
});
");
?>
<div id="surveyupdate">
<?php
echo CHtml::dropDownList('dropit', '',
array(
'Reception'=>'Service Reception',
'SA'=>'ServiceAdvisor','Billing'=>'Billing Staff',
'Parts'=>'Parts Counter','Cashier'=>'Cashier','Cwd'=>'Customer Welfare Desk'),
array('prompt'=>'- select -'));
?>
</div>
<?php
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
'id'=>'ajaxListView',
));
?>
controller 'survey.php'
public function actionQuestions()
{
//debugging purposes
echo "<pre>";
print_r($_POST);
exit;
}
To Do's
What I'm trying to do is to update the CListView by getting the selected data from dropdownlist via POST that to be evaluated by the controller, get the data from database and update the CListview.
Please help me. I'm just newly learning Yii. Thanks!
You need to add Id for dropdownList, like below
echo CHtml::dropDownList('dropit', '',
array(
'Reception'=>'Service Reception',
'SA'=>'ServiceAdvisor','Billing'=>'Billing Staff',
'Parts'=>'Parts
Counter','Cashier'=>'Cashier','Cwd'=>'Customer Welfare
Desk'),
array('prompt'=>'- select -', 'id'=>'dropit')); // added I'd here
Related
This is my view page
<?php echo $form->dropDownListRow($order,'area_id',Area::model()->getActiveAreaList(),
array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('currentController/getdeliveryforarea'),
'update'=>'#pickup_time_slot', //selector to update
'data' => array('area_id' => 'js:this.value',),
)));
?>
<?php echo $form->dropDownListRow($order,'pickup_time_slot',array(),array('prompt'=>'Select time')); ?>
and in my controll the getdeliveryforarea is like
public function actionGetdeliveryforarea()
{
$data=Areatimeslot::model()->findAll('area_id=:area_id',
array(':area_id'=>(int) $_POST['id']));
$data=CHtml::listData($data,'id','name');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
}
here my dependent dropdown is not working ,from getActiveAreaList i will get the area list in first dropdown and once i choose an area it must show corrosponding time list in second dropdown,i hope someone will help me out,thanks in advance
Use Juqery migrate plugin or try something like,
jQuery.browser = {};
(function () {
jQuery.browser.msie = false;
jQuery.browser.version = 0;
if (navigator.userAgent.match(/MSIE ([0-9]+)\./)) {
jQuery.browser.msie = true;
jQuery.browser.version = RegExp.$1;
}
})();
I am rendering an ActiveForm dynamically via ajax, however because the form isn't there on page load, the necessary JS files yiiActiveForm.js, yii.validation.js) and the associated validation triggers aren't there when the form is echoed out.
Here is some sample code:
JS:
$('.btn-edit').on('click', function() {
var id = $(this).attr('data-id');
var url = base_url + '/account/editreview/' + id;
$.ajax({
type: 'post',
url: url,
dataType: 'json',
success: function(result) {
$('.popup').html(result.html);
}
});
});
Controller:
public function actionEditReview($id)
{
$return_array = [
'html' => null,
];
$review = $this->findReview($id);
$return_array['html'] = $this->renderPartial('review-popup', [
'review' => $review,
]);
echo json_encode($return_array);
}
View (review-popup.php):
<?php
use yii\widgets\ActiveForm;
?>
<?php $form = ActiveForm::begin([
'id' => 'review-form',
'enableAjaxValidation' => false,
'enableClientValidation' => true,
]); ?>
<?php echo $form->field($review, 'title')->textInput(); ?>
<button type="submit" class="btn-edit" data-id="<?php echo $review->id; ?>">Submit</button>
<?php ActiveForm::end(); ?>
I have read the notes on this page https://yii2-cookbook.readthedocs.io/forms-activeform-js/ but this talks about adding validation to single attributes, and not to an entire form.
Does anyone know how to do this?
I found the solution: use renderAjax() instead of renderPartial() when echoing out the view.
http://www.yiiframework.com/doc-2.0/yii-web-view.html#renderAjax()-detail
I get an error 200. Form never gets posted, all fields return blank. I also checked if the columns are required, i set everything to null. I have another form that looks like this and it works fine. Any ideas?
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'thisForm',
)); ?>
//form
<?php
echo CHtml::ajaxSubmitButton('Add',
Yii::app()->createUrl("url/controller"),
array(
'type'=>'POST',
'dataType'=>'text json',
'data'=>'js:$("#thisForm").serialize()',
'success'=>'js:function(data) {
if(data.status=="success")
$.fn.yiiGridView.update("osb123");
}',
'error'=>'function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}'
));
?>
public function actionController()
{
$model=new Model;
if($_POST['Model'])
{
$model->attributes=$_POST['Model'];
$model->temporary_id = Yii::app()->user->user_id;
$model->cost = floatval($_POST['Model']['cost']);
$model->active = "Y";
if($model->validate()){
echo CJSON::encode(array('status'=> 'success',
'data'=>var_dump($_POST['Model'])
));
}
else{
$error = CActiveForm::validate($model);
echo CJSON::encode(array('status'=> 'error', 'error'=>var_dump($_POST['Model'])));
}
}else echo CJSON::encode(array('status'=>'error','error'=>'Not Set'));
}
In the ajax button for url you set: Yii::app()->createUrl("url/controller") while usually the right syntax is Yii::app()->createUrl("controller/action") Could you with debugging tool (Ctrl+Shift+I or F12) check the POST-request and see the actual url that has been generated by yii?
You have beginWidget, do you also have endWidget ?
I will appreciate if somebody could help me to find how to solve out one problem, I have a checkbox in my create form. If i pushed the create button I want to have a popup window if the checkbox is checked and do nothing if the checkbox is unchecked.
my codes in _form
<?php echo $form->checkBoxRow($model, 'default'); ?>
<div class="form-actions">
<?php $this->widget('bootstrap.widgets.TbButton', array(
'buttonType'=>'submit',
'type'=>'primary',
'label'=>$model->isNewRecord ? 'Create' : 'Save',
)); ?>
</div>
in my create controller
public function actionCreate()
{
$model=new EmpSched;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['EmpSched']))
{
$model->attributes=$_POST['EmpSched'];
if($model->save())
$this->redirect(array('view','id'=>$model->id_empsched));
}
$this->render('create',array(
'model'=>$model,
'emp'=> new CActiveDataProvider('schedule'),
));
}
in my create.php
<?php
$this->breadcrumbs=array(
'Emp Scheds'=>array('index'),
'Create',
);
$this->menu=array(
array('label'=>'List EmpSched','url'=>array('index')),
array('label'=>'Manage EmpSched','url'=>array('admin')),
);
?>
<h1>Create EmpSched</h1>
<?php echo $this->renderPartial('_form', array('model'=>$model)); ?>
<?php $this->widget('bootstrap.widgets.TbGridView',array(
'id'=>'schedule-grid',
'dataProvider'=>$emp,
'columns'=>array(
'id_sched',
'sched_name',
array(
'name'=>'mon',
'value'=>'$data->fkidday->monday->name'
),
array(
'name'=>'tues',
'value'=>'$data->fkidday->tuesday->name'
),
array(
'name'=>'wed',
'value'=>'$data->fkidday->wednesday->name'
),
array(
'name'=>'thurs',
'value'=>'$data->fkidday->thursday->name'
),
array(
'name'=>'fri',
'value'=>'$data->fkidday->friday->name'
),
/*'sat',
'sun',
*/
),
));
?>
<script>
$(function() {
// when row is clicked
$('#schedule-grid tbody:first').on('click', 'tr', function() {
// get the ID
var id = $(this).find('td:first').text();
// select the same option in dropdown list with same value
$('#EmpSched_fk_id_sched')
.find("option[value="+ id +"]")
.prop("selected", "selected");
});
});
</script>
Add custom javascript (supposing the create button will submit the form)
$('#form-id').submit(function() {
if $(this).find('#check-box-id').is(':checked') {
// open popup here
}
return false;
});
I use CGridView to render a result (invoice rows) from a db query. This is done inside a <div> on html page that is later converted to a PDF using wkhtmltopdf and printed. The problem is that today I have no support for page break so if number of invoice rows are greater than 10 the remaining rows are not visible. I can't let the div flow since I have an invoice footer with an absolute position that can't move.
Is there a way to force a page break and continue rendering rows on a separate page?
Below is part of the CGridView code used.
<...plenty of html---->
<div class="invoiceRow">
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $dpRows,
'cssFile' => Yii::app()->request->baseUrl . '/css/invoice.css',
'summaryText' => '',
'enablePagination' => false,
'columns' => array(
'number',
'name',
'description',
'amount',
array (
'name'=>'value',
'header' => 'netprice',
'value' => 'number_format($data->value,2,","," ")'
),
));
?>
</div>
<---more html ----->
If wkhtml is able to parse css (it seems so) you could try:
<div style="page-break-after:always">
//X invoices here
</div>
if(!empty($model))
{
if($model->content_type==1)
{
$sentences = explode ('<div style="page-break-after: always;"><span style="display:none"> </span></div>',$model->content);
?>
<div id="datashow">
<?php
echo $sentences[0];
?>
</div>
<?php
$dummy=array();
$kcnt=preg_match_all('/page-break-after: always/', $model->content,$dummy);
// echo $model->content;
}
<script>
$("#pagebreakdiv").find(":button").click(function () {
var datashow=$('#datashow');
var a=this.value;
var bid='<?php echo $bid; ?>';
var id='<?php echo $id; ?>';
var pdata = {};
pdata["bitt"] = a;
pdata["bid"] = bid;
pdata["id"] = id;
$.ajax(
{
url : "<?php echo Yii::app()->request->baseUrl; ?>/UserSimple/Contentpagebreak",
type: "POST",
data : pdata,
success:function(data, textStatus, jqXHR)
{
datashow.html(data);
},
error: function(jqXHR, textStatus, errorThrown)
{
}
});
});
</script>