yii2 girdview pagination id list are showing but not showing link - yii2-advanced-app

I am New in yii2 going to add pagination it is showing list of id only botstrap and link are not showing what is problem anyone explain me this is gridview
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'id' => 'grid',
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'user_fname',
'user_id',
'user_lname',
'user_mobile',
['class' => 'yii\grid\ActionColumn'],
[ 'class' => 'yii\grid\CheckboxColumn'],
],
]);
?>
<?php
echo yii\widgets\ListView::widget([
'dataProvider' => $dataProvider,
'View' => 'usermaster',
'layout' => "{pager}\n{items}\n{pager}",
]);
?>
This is search controller which is i have added pagignation class
public function search($params)
{
$query = UsermasterModel::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 10,
],
'sort' => [
'defaultOrder' => [
'user_fname' => SORT_DESC,
'user_fname' => SORT_ASC,
]
],
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'user_id' => $this->user_id,
]);
$query->andFilterWhere(['like', 'user_fname', $this->user_fname])
->andFilterWhere(['like', 'user_lname', $this->user_lname])
->andFilterWhere(['like', 'user_mobile', $this->user_mobile]);
return $dataProvider;
}
}

Related

Magento 2 admin custom save button send request twice

Basically, I want to send 'store' as a parameter when I save the form. For this, I customized the save button and added store_id there, but now save method is called twice. Any idea why this is happening and how can I fix it?
This is the button:(Vendor\Module\Block\Adminhtml\Entity\Edit\SaveButton.php)
public function getButtonData()
{
return [
'label' => __('Save'),
'class' => 'save primary',
'data_attribute' => [
'mage-init' => [
'buttonAdapter' => [
'actions' => [
[
'targetName' => 'vendor_module_entity_form.vendor_module_entity_form',
'actionName' => 'save',
'params' => [
true,
['store' => 5]
]
]
]
]
]
],
'sort_order' => 90,
];
}
and this is the ui_component (vendor_entity_entity_form.xml):
......
<settings>
<buttons>
<button class="Vendor\Module\Block\Adminhtml\Entity\Edit\SaveButton" name="save"/>
</buttons>
</settings>
<dataSource name="entiity_form_data_source">
<settings>
<submitUrl path="*/*/save"/>
<validateUrl path="*/*/validate"/>
</settings>
</dataSource>
......
Please try:
public function getButtonData(): array
{
return [
'label' => __('Save'),
'class' => 'save primary',
'data_attribute' => [
'mage-init' => ['button' => ['event' => 'save']],
'form-role' => 'save',
],
'sort_order' => 10
];
}
please try the below code.
$this->buttonList->add(
'select_all',
[
'label' => __('Select All Rates and Save'),
'class' => 'save',
'onclick' => "jQuery('#testhidden').val(1)",
'data_attribute' => [
'mage-init' => ['button' => ['event' => 'save', 'target' => '#edit_form']],
]
],
10
);
Create a hidden field in the form.php where fields are defined
$fieldset->addField(
'testhidden',
'hidden',
['name' => 'testhidden', 'value' => 0, 'no_span' => true]
);

edit action similar to tutorial doesn't work

I followed the ZF3 tutorial and I'm quite satisfied, but I'm something missing with the edit action in my controller. the add action works fine while the edit action doesn't. Probably I don't see it by myself, so I post it here, asking for help, even if it is really basic.
My model is nearly the same as in the tutorial, here a part of it:
public function exchangeArray(array $data)
{
$this->ProjectID= !empty($data['ProjectID']) ? $data['ProjectID'] : null;
$this->CI_Number= !empty($data['CI_Number']) ? $data['CI_Number'] : null;
$this->Description= !empty($data['Description']) ? $data['Description'] : null;
$this->Projectname= !empty($data['Projectname']) ? $data['Projectname'] : null;
$this->Shortcut= !empty($data['Shortcut']) ? $data['Shortcut'] : null;
$this->Component_Class= !empty($data['Component_Class']) ? $data['Component_Class'] : null;
}
public function getArrayCopy()
{
// echo var_dump(get_object_vars($this)
// );
//return get_object_vars($this);
return [
'ProjectID' => $this->ProjectID,
'CI_Number' => $this->CI_Number,
'Description' => $this->Description,
'Projectname' => $this->Projectname,
'Shortcut' => $this->Shortcut,
'Component_Class' => $this->Component_Class,
];
}
and here also my controlleraction:
public function editAction()
{
$id = (int) $this->params()->fromRoute('id', 0);
echo $id;
if (0 === $id) {
return $this->redirect()->toRoute('project', ['action' => 'add']);
}
else {
try {
$project = $this->projectTable->getProject($id);
} catch (\Exception $e) {
return $this->redirect()->toRoute('project', ['action' => 'index']);
}
$form = new ProjectForm();
$form->bind($project);
//$form->bind($project->current());
$form->get('submit')->setAttribute('value', 'save changes');
$request = $this->getRequest();
$viewData = ['ProjectID' => $id, 'form' => $form];
if (! $request->isPost()) {
return $viewData;
}
$form->setInputFilter($project->getInputFilter());
$form->setData($request->getPost());
if (! $form->isValid()) {
echo "nicht valide";
return $viewData;
}
else{
echo $project;
$this->projectTable->saveProject($project);
}
}
// Redirect to album list
// return $this->redirect()->toRoute('project', ['action' => 'index']);
}
here for completion reasons my view edit.phtml:
<?php
$title = 'projects';
$this->headTitle($title);
?>
<h1><?= $this->escapeHtml($title) ?></h1>
<?php
// This provides a default CSS class and placeholder text for the artist element:
$ProjectID= $form->get('ProjectID');
$ProjectID->setAttribute('class', 'form-control');
$ProjectID->setAttribute('placeholder', 'ProjectID');
$Projectname= $form->get('Projectname');
$Projectname->setAttribute('class', 'form-control');
$Projectname->setAttribute('placeholder', 'Projectname');
$CI_Number= $form->get('CI_Number');
$CI_Number->setAttribute('class', 'form-control');
$CI_Number->setAttribute('placeholder', 'CI_number');
$Shortcut= $form->get('Shortcut');
$Shortcut->setAttribute('class', 'form-control');
$Shortcut->setAttribute('placeholder', 'Shortcut');
$Description= $form->get('Description');
$Description->setAttribute('class', 'form-control');
$Description->setAttribute('placeholder', 'Description');
$Component_Class= $form->get('Component_Class');
$Component_Class->setAttribute('class', 'form-control');
$Component_Class->setAttribute('placeholder', 'Component_Class');
// This provides CSS classes for the submit button:
$submit = $form->get('submit');
$submit->setAttribute('class', 'btn btn-primary');
//$form->setAttribute('action', $this->url('project', ['action' => 'edit',])); //,'id'=> $id
$form->setAttribute('action', $this->url('project', [
'action' => 'edit',
'ProjectID' => $id,
]));
$form->prepare();
echo $this->form()->openTag($form);
?>
<?php // Wrap the elements in divs marked as form groups, and render the
// label, element, and errors separately within ?>
<div class="form-group">
<?= $this->formElement($ProjectID) ?>
<?= $this->formElementErrors()->render($ProjectID, ['class' => 'help-block']) ?>
</div>
<div class="form-group">
<?= $this->formLabel($CI_Number) ?>
<?= $this->formElement($CI_Number) ?>
<?= $this->formElementErrors()->render($CI_Number, ['class' => 'help-block']) ?>
</div>
<div class="form-group">
<?= $this->formLabel($Description) ?>
<?= $this->formElement($Description) ?>
<?= $this->formElementErrors()->render($Description, ['class' => 'help-block']) ?>
</div>
<div class="form-group">
<?= $this->formLabel($Projectname) ?>
<?= $this->formElement($Projectname) ?>
<?= $this->formElementErrors()->render($Projectname, ['class' => 'help-block']) ?>
</div>
<div class="form-group">
<?= $this->formLabel($Shortcut) ?>
<?= $this->formElement($Shortcut) ?>
<?= $this->formElementErrors()->render($Shortcut, ['class' => 'help-block']) ?>
</div>
<div class="form-group">
<?= $this->formLabel($Component_Class) ?>
<?= $this->formElement($Component_Class) ?>
<?= $this->formElementErrors()->render($Component_Class, ['class' => 'help-block']) ?>
</div>
<?php
echo $this->formSubmit($submit);
//echo $this->form->get('DCLID');
echo $this->formHidden($form->get('ProjectID'));
echo $this->form()->closeTag();
It will show the recordset properly, but won't save the changes to the database and afterwards redirects to my add action. I hope somebody sees what I'm missing, even it might be a stupid error.
EDIT1: Here is my routing link from index.phtml
Edit
EDIT2: Screenshot to show the routing parameter
EDIT3: describing test issues
here again my controller edit action, to follow up:
public function editAction()
{
$id = (int) $this->params()->fromRoute('id', 0);
echo "variable id: ". $id;
if (0 === $id) {
//return $this->redirect()->toRoute('project', ['action' => 'index']);
echo "id = 0";
}
else {
try {
$project = $this->projectTable->getProject($id);
} catch (\Exception $e) {
return $this->redirect()->toRoute('project', ['action' => 'index']);
}
$form = new ProjectForm();
$form->bind($project);
//$form->bind($project->current());
$form->get('submit')->setAttribute('value', 'save changes');
var_dump(get_object_vars($project));
$request = $this->getRequest();
$viewData = ['ProjectID' => $id, 'form' => $form];
if (!$request->isPost()) {
return $viewData;
}
else {
$form->setInputFilter($project->getInputFilter());
$form->setData($request->getPost());
if (!$form->isValid()) {
echo "nicht valide";
return $viewData;
}
else{
echo "valide";
echo $project;
$this->projectTable->saveProject($project);
}
}
}
// Redirect to album list
// return $this->redirect()->toRoute('project', ['action' => 'index']);
}
I get the output of the vardump, so the project to edit will be delivered correctly. If I send the form with the recordchanges the controller won't catch it, the controller action sees id=0 then, so the method saveProject will be never called. In my understanding it might has to do with the formvalid something, because after sending the data I come never further than if (0 === $id).
EDIT 4: After some actual tests I think it must be a routing issue. If I give the route manually via browser, I won't get the target page at all. If I dump the value, it is always NULL. So I think it could be a bracket to much/to less issue in my module.config.php I show it here because I couldn't finde the problem, I countedt the braskets several times and I won't get it. So any help is appreciated, it must be a very simple topic:
<?php
namespace Import;
use Zend\Router\Http\Segment;
use Zend\Router\Http\Literal;
use Zend\ServiceManager\Factory\InvokableFactory;
//use Zend\ServiceManager\Factory\InvokableFactory;
return [
/* 'controllers' => [
'factories' => [
Controller\ImportController::class => InvokableFactory::class,
],
], */
// hier die Einstellungen für die Routen
'router' => [
'routes' => [
'index' => [
'type' => Segment::class,
'options' => [
'route' => '/import[/:action[/:id]]',
'constraints' => [
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
],
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
],
],
'import' => [
'type' => Segment::class,
'options' => [
'route' => '/import[/:action[/:id]]',
'constraints' => [
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
],
'defaults' => [
'controller' => Controller\ImportController::class,
'action' => 'index',
],
],
],
'importdcl' => [
'type' => Segment::class,
'options' => [
'route' => '/importdcl[/:action[/:id]]',
'constraints' => [
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
],
'defaults' => [
'controller' => Controller\ImportdclController::class,
'action' => 'index',
],
],
],
'project' => [
'type' => Segment::class,
'options' => [
'route' => '/project[/:action[/:id]]',
'constraints' => [
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
],
'defaults' => [
'controller' => Controller\ProjectController::class,
'action' => 'index',
],
],
],
'unit' => [
'type' => Segment::class,
'options' => [
'route' => '/unit[/:action[/:id]]',
'constraints' => [
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
],
'defaults' => [
'controller' => Controller\UnitController::class,
'action' => 'index',
],
],
],
'index' => [
'type' => Segment::class,
'options' => [
'route' => '/index[/:action[/:id]]',
'constraints' => [
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
],
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
],
],
'user' => [
'type' => Segment::class,
'options' => [
'route' => '/user[/:action[/:id]]',
'constraints' => [
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
],
'defaults' => [
'controller' => Controller\UserController::class,
'action' => 'index',
],
],
],
'followup' => [
'type' => Segment::class,
'options' => [
'route' => '/followup[/:action[/:id]]',
'constraints' => [
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
],
'defaults' => [
'controller' => Controller\FollowupController::class,
'action' => 'index',
],
],
],
],
],
'view_manager' => [
'template_path_stack' => [
'import' => __DIR__ . '/../view',
],
],
/* ... */
'navigation' => [
'default' => [
[
'label' => 'Dashboard',
'route' => 'home',
],
[
'label' => 'Project',
'route' => 'project',
'pages' => [
[
'label' => 'Add',
'route' => 'project',
'action' => 'add',
],
[
'label' => 'Edit',
'route' => 'project',
'action' => 'edit',
],
[
'label' => 'Delete',
'route' => 'project',
'action' => 'delete',
],
],
],
[
'label' => 'Unit',
'route' => 'unit',
'pages' => [
[
'label' => 'Add',
'route' => 'unit',
'action' => 'add',
],
[
'label' => 'Edit',
'route' => 'unit',
'action' => 'edit',
],
[
'label' => 'Delete',
'route' => 'unit',
'action' => 'delete',
],
],
],
[
'label' => 'Importlog',
'route' => 'importdcl',
'action' => 'index',
'pages' => [
[
'label' => 'Add',
'route' => 'unit',
'action' => 'add',
],
[
'label' => 'Edit',
'route' => 'unit',
'action' => 'edit',
],
[
'label' => 'Delete',
'route' => 'unit',
'action' => 'delete',
],
],
],
[
'label' => 'Follow up',
'route' => 'followup',
'action' => 'index',
'pages' => [
[
'label' => 'Add',
'route' => 'unit',
'action' => 'add',
],
[
'label' => 'Edit',
'route' => 'unit',
'action' => 'edit',
],
[
'label' => 'Delete',
'route' => 'unit',
'action' => 'delete',
],
],
],
[
'label' => 'User',
'route' => 'user',
'pages' => [
[
'label' => 'Add',
'route' => 'unit',
'action' => 'add',
],
[
'label' => 'Edit',
'route' => 'unit',
'action' => 'edit',
],
[
'label' => 'Delete',
'route' => 'unit',
'action' => 'delete',
],
],
],
[
'label' => 'Logout',
'route' => 'user',
'action' => 'logout',
'pages' => [
[
'label' => 'Add',
'route' => 'unit',
'action' => 'add',
],
[
'label' => 'Edit',
'route' => 'unit',
'action' => 'edit',
],
[
'label' => 'Delete',
'route' => 'unit',
'action' => 'delete',
],
],
],
],
],
/* ... */
];
I'm testing in the usercontroller at the moment, but I have had the same problem in others routes, so I think the error might be in this file.
Based on your code, seem the problem is on $id. It mean the value is 0, that's why it is redirected to project/add.
$id = (int) $this->params()->fromRoute('id', 0);
echo $id;
if (0 === $id) {
return $this->redirect()->toRoute('project', ['action' => 'add']);
}
In your view script, the form action is set to project/edit/ProjectID/$id
$form->setAttribute('action', $this->url('project', [
'action' => 'edit',
'ProjectID' => $id,
]));
That's why your controller cannot retrieve $this->params()->fromRoute('id', 0), because the form send ProjectID param instead of id.
So, the solution please adjust your form (using id) or your controller (using ProjectID).
I at last solved it. My suggestion was right, it was a problem with the module.config.php, I have set an endbracket at the wrong place.
After writing it in parts new, tsting and complete the file, it now works.

How to hide delete icon in kartik Detail view yii2?

I am using Kartik/Detail View. I only want to show the edit button in the panel and hide delete icon in the panel.My code is :
<?= DetailView::widget([
'model' => $model,
'mode' => 'view',
'bordered' => true,
'striped' => true,
'panel' => [
'heading' => $this->title,
'type' => DetailView::TYPE_INFO,
],
'container' => ['id'=>'kv-demo'],
'responsive' => true,
'hover' => true,
'hAlign'=>true,
'vAlign'=>true,
'fadeDelay'=>true,
'attributes' => [
'business_name',
'address2',
'city',
'state',
'zip',
'telephone',
'fax',
'email:email',
],
]) ?>
How can I hide delete icon ?
http://demos.krajee.com/detail-view#option-buttons1
Use 'buttons1' => '{update}', to hide delete.
<?= DetailView::widget([
'model' => $model,
'mode' => 'view',
'bordered' => true,
'striped' => true,
'panel' => [
'heading' => $this->title,
'type' => DetailView::TYPE_INFO,
],
'buttons1' => '{update}',
You can do this from _columns.php file of view folder with following option
Here you can add template with action that you need to show in the detail view (in this example view action is used and Assign with fa-gear icon is displayed in view)
[
'class' => 'kartik\grid\ActionColumn',
'dropdown' => false,
'vAlign'=>'middle',
'template' => '{view}',
'urlCreator' => function($action, $model, $key, $index) {
return Url::to([$action,'id'=>$key]);
},
'buttons'=>[
'view' => function ($url, $model, $key) {
return Html::a('<span class="fa fa-gears"> Asign</span>', ['view', 'id'=>$model->id],['title'=>'Asign','role'=>'modal-remote','data-toggle'=>'tooltip']);
},
]
],

How to make row in gridview clickable?

I am very new to yii2. I have created a gridview using CRUD Generator. I want to make the gridview's rows to be clickable like when we click on view. It should navigate to the view page for that row.
My gridview code is as follows:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'Task_ID',
'Task_Title',
'Description',
//'employee.employee_name',
//'Assign_task_to',
'start_date',
'due_date',
'priotiy_level',
// 'complexity_level',
// 'upload_documents',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
below code added Task_Title as clickable:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'attribute'=>'Task_Title',
'format'=>'raw',
'value' => function($data)
{
return
Html::a($data->Task_Title, ['task/view','id'=>2], ['title' => 'View','class'=>'no-pjax']);
}
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'rowOptions' => function($model,$index,$key){
return ['id' => $model['id'], 'onclick' => 'alert("Row clicked")'];
}
'columns' => [] ]); ?>

Saving manyToMany data cakephp3

I'm struggling about this issue for a while. I have three tables prestation, tools and prestation_tools. And my entities are linked as follow :
#ToolsTable
public function initialize(array $config)
{
parent::initialize($config);
$this->table('tools');
$this->displayField('id');
$this->primaryKey('id');
$this->belongsToMany('Prestation', [
'joinTable' => 'prestation_tools',
'foreignKey' => 'tools_id'
]);
}
#PrestationTable
public function initialize(array $config)
{
parent::initialize($config);
$this->table('prestation');
$this->displayField('id');
$this->primaryKey('id');
$this->belongsToMany('Tools', [
'through' => 'PrestationTools'
]);
$this->belongsToMany('Competences', [
'through' => 'PrestationCompetences'
]);
}
#PrestationToolsTable
public function initialize(array $config)
{
parent::initialize($config);
$this->table('prestation_tools');
$this->displayField('prestation_id');
$this->primaryKey(['prestation_id', 'tools_id']);
$this->belongsTo('Prestation', [
'foreignKey' => 'prestation_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Tools', [
'foreignKey' => 'tools_id',
'joinType' => 'INNER'
]);
}
And of my form is :
<?= $this->Form->input("Prestation.Tools.1.tools_id", [
'label' => false,
'type' => 'select',
'options' => $tools,
'empty' => "Sélectionner l'outil 1",
'templates' => [
'inputContainer' => '{{content}}'
],
'class' => 'form-control',
]) ?>
<label >Spécifications de l'outil 1</label>
<?= $this->Form->textarea('Prestation.Tools.1.details', [
'templates' => [
'inputContainer' => '<div class="form-group">{{content}}</div>'
],
'class' => 'form-control',
'placeholder' => 'Spécifications de l\'outil',
'label' => 'Spécifications de l\'outil'
]) ?>
When trying to save from PrestationController, on prestation record is saved. Can anyone help me ?
Update
My PrestationController's add() action looks like this :
public function add(){
#...
$prestation = $this->Prestation->patchEntity($prestation, $this->request->data, [
'associated' => [
'Tools',
'Competences'
]
]);
#...
}