Disable pagination in kartik-v/yii2-grid Kartik Gridview - yii2

How the pagination can be disabled in Kartik Gridview?
I am using the code like below
echo GridView::widget([
'dataProvider'=>$dataProvider,
'filterModel'=>$searchModel,
'showPageSummary'=>true,
'responsive'=>true,
'hover' => true,
'pjax'=>true,
'containerOptions'=>['style'=>'overflow: auto'], // only set when $responsive = false
'headerRowOptions'=>['class'=>'kartik-sheet-style'],
'filterRowOptions'=>['class'=>'kartik-sheet-style'],
'striped'=>true,
'hover'=>true,
'pager' => [
'options'=>['class'=>'pagination'], // set clas name used in ui list of pagination
'prevPageLabel' => 'Previous', // Set the label for the "previous" page button
'nextPageLabel' => 'Next', // Set the label for the "next" page button
'firstPageLabel'=>'First', // Set the label for the "first" page button
'lastPageLabel'=>'Last', // Set the label for the "last" page button
'nextPageCssClass'=>'next', // Set CSS class for the "next" page button
'prevPageCssClass'=>'prev', // Set CSS class for the "previous" page button
'firstPageCssClass'=>'first', // Set CSS class for the "first" page button
'lastPageCssClass'=>'last', // Set CSS class for the "last" page button
'maxButtonCount'=>10, // Set maximum number of page buttons that can be displayed
],
'panel'=>['type'=>'primary', 'heading'=>'Select Option to Publish Policy'],
'columns'=>[
['class'=>'kartik\grid\SerialColumn'],
['class' => 'kartik\grid\CheckboxColumn',
'rowSelectedClass' => GridView::TYPE_INFO,
'name' => 'id',
'contentOptions' => function ($model, $key, $index, $column){//print_r($model);
return ['username'=>$model->username];
},
'checkboxOptions' => function ($model, $key, $index, $column) {
return ['key' => $model->username, 'value' => $model->username];
},
],
],
]);
I tried adding pageSize and pagination to false, but didnt work.

No pagination mean show all the instance then you can set pageSize = 0
$dataProvider->pagination->pageSize=0;
or you can use
$dataProvider->pagination = false;

Related

How to update field in user table yii2

I have modified user table to add one column, the column's name is id_periode, I have advanced template, so from backend controler i want update that column value. I create controller like this
public function actionPindahPeriode($id)
{
$model2 = $this->findModel2($id);
if ($model2->load(Yii::$app->request->post()) ) {
$model2->save();
return $this->render('view',
'model2' => $this->findModel2($id),
]);
}
date_default_timezone_set('Asia/Makassar');
$jam_sekarang = date('h:i:s', time());
$tgl_sekarang=date('Y-m-d');
$model_periode = Periode::find()
->andWhere(['>','mulai_daftar_ulang',$tgl_sekarang ])
->asArray()
->all();
return $this->renderAjax('pindah_periode', [
'model_periode' => $model_periode,
'model2' => $this->findModel2($id),
]);
}
The findModel2 function is like this
protected function findModel2($id)
{
if (($model = User::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
I render that model into a form
<?php $form = ActiveForm::begin(); ?>
<?php $listData=ArrayHelper::map($model_periode,'id',function($model_periode){
return $model_periode['nama_periode'].' Tahun '.$model_periode['tahun'];});?>
<?= $form->field($model2, 'id_periode')->dropDownList($listData, ['prompt' => '']) ?>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-danger']) ?>
</div>
<?php ActiveForm::end(); ?>
The form is working but i can not update the value id_periode column in table user. There is not error showing, any suggestion?
Make sure your new attribute id_periode has a rule defined in the public function rules(){} function, this way $model2->load(Yii::$app->request->post()) will assign this value from the data submitted.
$model->save() returns a bool value whether the record was saved or not. You can use this to your advantage and check whether there are any validation errors.ie:
if($model2->save()) {
return $this->render('view',
'model2' => $this->findModel2($id),
]);
} else {
return $this->renderAjax('pindah_periode', [
'model_periode' => $model_periode,
'model2' => $this->findModel2($id),
]);
}
Option 1 : Check your column fields in User model's validation rules. You need to shift unwanted column fields from required attribute to safe attribute.
Option 2 : try $model2->save(false);. false will override your model rules.

Initial Values for an array-type attribute in Yii2 Model

If an attribute in a Model holds array data, say Dates, that are populated in a form with multiple rows for this attribute, how may I assign initial values to the array elements so that they display initially in the form when it appears.
In my example, my array-type attribute holds dates and I want each new date row in the form to have different values when the form loads.
<?= $form->field($model, 'datesToPay[]') ?>
I tried to use the DefaultValueValidator filter of Yii2 to assign initial value to the datesToPay array elements but it does not show the value when the form loads.
['datesToPay', 'each', 'rule' => ['default', 'value' => date('Y-m-d')]]
You can do in the controllerAction before render
public function actionCreate()
{
$model = new MyModel();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
$model->datesToPay[0] = 'YourValue';
return $this->render('create', [
'model' => $model,
]);
}
}

How to integrate ckfinder with yii2

I am trying to integrate ckeditor and ckfinder to a project using yii2.
I have placed both ckeditor and ckfinder folder in root/vendor and made necessary adjustments, ckeditor is working fine, ckfinder also showing the file browser popup with 'Browser server' button. But whenever I click on the browse button its not opening the file selector popup, instead showing a page not found error.
I have tried to integrate ckfinder writing following lines of code in ckeditor/config.js:
config.filebrowserBrowseUrl = 'hostname/vendor/ckfinder/ckfinder.html';
config.filebrowserImageBrowseUrl = 'hostname/vendor/ckfinder/ckfinder.html?type=Images';
config.filebrowserFlashBrowseUrl = 'hostname/vendor/ckfinder/ckfinder.html?type=Flash';
config.filebrowserUploadUrl = 'hostname/vendor/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files';
config.filebrowserImageUploadUrl = 'hostname/vendor/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images';
config.filebrowserFlashUploadUrl = 'hostname/vendor/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash';
But I haven't found a solution.
Can anyone please help me fix that issue?
I was create Custom CKEditorAsset in yii2 like this:
namespace vendor\yiif\ckeditor;
use iutbay\yii2kcfinder\KCFinder;
use iutbay\yii2kcfinder\KCFinderAsset;
class CKEditorAsset extends \dosamigos\ckeditor\CKEditorAsset
{
public $depends = [
'yii\web\YiiAsset',
'yii\web\JqueryAsset',
//'iutbay\yii2kcfinder\KCFinderAsset'
];
public function init()
{
$register = KCFinderAsset::register(\Yii::$app->view);
$kcfinderUrl = $register->baseUrl;
\Yii::$app->view->registerJs(<<<js
CKEDITOR.config.filebrowserBrowseUrl="$kcfinderUrl/browse.php?opener=ckeditor&type=files";
CKEDITOR.config.filebrowserUploadUrl="$kcfinderUrl/upload.php?opener=ckeditor&type=files";
js
);
// kcfinder options
// http://kcfinder.sunhater.com/install#dynamic
$kcfOptions = array_merge(KCFinder::$kcfDefaultOptions, [
'uploadURL' => \Yii::getAlias('#web/uploads/modules/ckfinder'),
'uploadDir'=>\Yii::getAlias('#app/web/uploads/modules/ckfinder'),
'access' => [
'files' => [
'upload' => true,
'delete' => false,
'copy' => false,
'move' => false,
'rename' => false,
],
'dirs' => [
'create' => true,
'delete' => false,
'rename' => false,
],
],
]);
// Set kcfinder session options
\Yii::$app->session->set('KCFINDER', $kcfOptions);
parent::init();
}
}

Yii2 karthik editable column first row not working

I used karthik grid view and editable column to edit my grid view data. it not work for the first row of the grid view and work for the other rows when i enter value to the first row it give error like this
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
my gridview code is.
<?= GridView::widget([
'dataProvider' => $dataProvider,
'pjax' => true,
'pjaxSettings' => [
'options' => [
'id' => 'grid',
]
],
'export'=>false,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'header'=>'Sub Category',
'value'=>'item.subCategory.name',
],
'item.code',
[
'header'=>'Brand',
'value'=>'item.brand.name',
],
'item.description',
'item.pack_size',
[
'header'=>'Unit',
'value'=>'item.unit',
],
[
'header'=>'Last Price',
'value'=>function($model){
$customerId = $model->customerOrderRequest->customer_id;
$lastPurchasedPrice = Item::getLastPurchasedPrice($customerId,$model->item_id);
return '$ ' . number_format($lastPurchasedPrice, 2);
}
],
[
'class' => 'kartik\grid\EditableColumn',
'attribute'=>'qty',
'editableOptions' =>
[
'formOptions' => ['action' => 'customer-order-request']
],
'header' => 'Qty',
],
[
'header'=>'Estimate',
'value'=>function($model)
{
return '123123.00';
}
],
[
'header'=>'Price (UNIT)',
'format'=>'raw',
'value'=>function($modal){
$salse_rep_id = Yii::$app->user->identity->ref_id;
$exsist_respond = Respond::find()->where(['sales_rep_id'=>$salse_rep_id,'customer_order_request_id'=>$modal->customer_order_request_id])->one();
if(!empty($exsist_respond)){
$exsist_respond_item = RespondItem::find()->where(['item_id'=>$modal->item_id,'respond_id'=>$exsist_respond->id])->one();
if(!empty($exsist_respond_item)){
$price = $exsist_respond_item->price;
} else {
$price = NULL;
}
} else {
$price = NULL;
}
if(!empty($exsist_respond) && $exsist_respond->status !="Pending"){
return $price;
}else{
return "<input type='text' class='respond-item' cor='$modal->customer_order_request_id' value='$price' item_id='$modal->item_id' />";
}
},
'visible'=>(Yii::$app->user->identity->ref_table =="sales_rep")? true:false
],
],
]); ?>
and my controller is
public function actionEditable()
{
if (Yii::$app->request->post('hasEditable'))
{
$customerItemsId = Yii::$app->request->post('editableKey');
print_r($customerItemsId);die();
$model = RequestItem::findOne($customerItemsId);
$out = Json::encode(['output'=>'', 'message'=>'']);
$post = [];
$posted = current($_POST['RequestItem']);
$post['RequestItem'] = $posted;
if ($model->load($post))
{
$model->save();
$output = '';
$out = Json::encode(['output'=>$output, 'message'=>'']);
}
echo $out;
return;
}
}
any one can help me with this problem.
Form tag nested.
a trick: Add a fake form to let browser remove it.
I meet this problem today, cause I put a form in another, they are nested.
So the browser remove the first nested form.
In the official W3C XHTML specification, Section B. "Element Prohibitions", states that:
"form must not contain other form elements."
official website
Following is sample code:
<form id="thisisMainForm">
<form id="dummy"> ->This will get Removed
<gridview id="thisisEditableGridView">
Gridviewcode.....
</gridview>
</form>
</form>

yii2 : make SerialColumn a link

How to make the numbering result of the SerialColumn a link. Normally it generates number starting from 1. I want to make it a link. What property to use?
'columns' => [
// ...
[
'class' => 'yii\grid\SerialColumn',
// you may configure additional properties here
],
]
You can't using the actual SerialColumn class.
That being said it should be fairly easy to do with a regular column. You can define a content callback that will receive all the necessary information to do this on its own:
'columns' => [
// ...
[
'content' => function($model, $key, $index, $column) {
$globalIndex = $index + 1;
$pagination = $column->grid->dataProvider->getPagination();
if ($pagination !== false) {
$globalIndex = $pagination->getOffset() + $index + 1;
return \yii\helpers\Html::a($globalIndex, ['/route/action', 'id' => $globalIndex]);
}
],
]
Note: I haven't tested this so might not fully work out of the box.