I have a form field and a button in a form. The button should get the data from the form field and pass the value as parameter. Please tell me how can I get the data from the form field and pass the value.
form field -
<?= $form->field($model, 'productname')->textInput(['maxlength' => true]) ?>
button
<?= Html::a('Search', ['/stock/sellitem/printproductledger3', 'productname' => '8904187001305'], ['class'=>'btn btn-primary']) ; ?>
The above example work fine because the value is already given here 'productname' => '8904187001305'
What I need to do is get the value '8904187001305' from the form field. Please help.
actionIndex
public function actionIndex2()
{
$searchModel = new SellitemsbdtSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('_formbtdt', [
'model' => $searchModel,
]);
}
This index leads to _formdtbt
'action' => ['/stock/sellitem/printproductledger3',],
<?= $form->field($model, 'productname')->textInput(['maxlength' => true,]) ?>
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
Controller action printproductledger3
public function actionPrintproductledger3() {
$searchModel1 = new SellitemsbdtSearch();
$dataProvider1 = $searchModel1->search(Yii::$app->request->get());
$searchModel2 = new PuritemsbdtSearch();
$dataProvider2 = $searchModel2->search(Yii::$app->request->get());
return $this->render('_printproductledgerbtdt', [
'dataProvider1' => $dataProvider1,
'searchModel1' => $searchModel1,
'searchModel2' => $searchModel2,
'dataProvider2' => $dataProvider2,
]);
}
Well, I think I've pointed to wrong issue. The issue is model sellitemsbdtSearch is getting filtered but puritemsbdtSearch is not getting filtered.
Search Model sellitemsbdtSearch
public function search($params)
{
//$query = Sellitem::find();
$query = Sellitem::find()
->joinWith(['siSs'])
->select(['sellsum.ss_date as date','si_iupc','si_idesc', 'concat("By sales to Tax Invoice ", sellsum.ss_invno) as particular', 'si_qty as sellqty','(si_qty * si_rate) as value'])
->orDerBy([
'sellsum.ss_date'=>SORT_DESC,
]);
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => ['pageSize' => 10000000000,],
]);
if (!($this->load($params) && $this->validate())) {
return $dataProvider;
}
if($this->productname){
$query->andFilterWhere(['si_iupc'=> $this->productname]);
return $dataProvider;
}
}
Search Model puritemsbdtSearch
public function search($params)
{
$query = Puritem::find()
->joinWith(['psi'])
->select(['pursum.ps_date as date','pi_upc','pi_desc', 'concat("By purchase to Tax Invoice ", pursum.ps_invno) as particular', 'pi_qty as buyqty','(pi_qty * pi_rate) as value'])
->orDerBy([
'pursum.ps_date'=>SORT_DESC,
]);
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => ['pageSize' => 10000000000,],
]);
if (!($this->load($params) && $this->validate())) {
return $dataProvider;
}
if($this->productname){
$query->andFilterWhere(['pi_upc'=> $this->productname]);
return $dataProvider;
}
}
You could use this way for sending the value of the model to your action
<?php $form = ActiveForm::begin([
'action' => ['your_action'],
'method' => 'your_method ', /// get or post
]); ?>
<?= $form->field($model, 'productname')->textInput(['maxlength' => true]) ?>
<div class="form-group">
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
<?= Html::resetButton('Reset', ['class' => 'btn btn-default']) ?>
</div>
<?php ActiveForm::end(); ?>
once submit you cant get the value in the normale yii2 way
you can see this for more .. http://www.yiiframework.com/doc-2.0/guide-input-forms.html
for the second Model that don't have the same realetd model and filed as the first you should use
// in $get try retrive the content of $get (in this case i related your yourModel1)
$get = Yii::$app->request->get(['yourModel1'])
// and the use the value for searching in your model2 with the proper value
// obviously you should adatp the name in this sample with the proper ones
$searchModel2 = new PuritemsbdtSearch();
$dataProvider2 = $searchModel2->search(['YourModelSearch2Name'=>['yourAttributeYouNeed'=>$get['productname']]]);
Related
I have a form page in which that there are 3 input fields connected to database, now i need to add a new input field and get the values in variable in my controller page.
I have tried with constants my function is working properly but i need variables to do in my function
my form page
<?= $form->field($model, 'idnew_table')->textInput(['maxlength' => 5,'style'=>'width:100px']) ?>
<?= $form->field($model, 'adcaddress1')->textInput(['maxlength' => 5,'style'=>'width:200px']) ?>
<?= $form->field($model, 'adcaddress2')->textInput(['maxlength' => 5,'style'=>'width:200px']) ?>
<label>Value:</label><br>
<input type="text" name="submitvalue" value=""><br><br>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
my controller function is,
public function actionCreate($data=null,$data1=null,$data2=null)
{
$model = new Adcaddress();
$this->layout='admin';
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->idnew_table]);
} else {
$data2 = "variable";
$my_file1 = 'test.txt';
$handle1 = fopen($my_file1, 'r');
$data = $handle1;
$data = fread($handle1,filesize($my_file1));
$my_file = 'file.txt';
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
//$data = $handle;
fwrite($handle, "$data2");
// return $this->render('create', [ 'model' => $model, ]);
return $this->render('create', array('data' => $data,'data1' => $data1,'data2'=>$data2,
'model' => $model));
}
i need the value of name=submitvalue in my variable $data2 in my controller page.
the $model->load(Yii::$app->request->post())
load only the attribute related to the model contained in $_POST
the input you added named 'submitvalue' is not declared as a part of the model
(the name of attrribute model is by default as ModelName[attribute])
and then is not automatically loaded in the $model
if you need the content of the $_POST and work on it for inspecting you could use
$post = Yii::$app->request->post();
var_dump($post);
Friends,
I have an INDEX page with 3 GRIDVIEW components (model Keys, model Products, model Indicators). And on that same page I have a search form with a dropdownlist (company).
I need to: When you select for example company 02 the 3 GRIDVIEW are filtered only company records 02.
_search.php
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use kartik\date\DatePicker;
?>
<div class="ideas-search">
<?php $form = ActiveForm::begin([
'options' => [
'class' => 'form-inline',
],
'action' => ['index'],
'method' => 'get',
]); ?>
<?= $form->field($model, 'pa')->dropdownList([..])?>
...
index.php
<?php echo $this->render('_search', ['model' => $searchBase]); ?>
<?php Pjax::begin(['id' => '1']) ?>
<?= GridView::widget([
'id' => 'grid1',
'dataProvider' => $dataProviderKey,
'columns' => [
...
],
]); ?>
<?php Pjax::end() ?>
<?php Pjax::begin(['id' => '2']) ?>
<?= GridView::widget([
'id' => 'grid2',
'dataProvider' => $dataProviderProduct,
'columns' => [
...
],
]); ?>
<?php Pjax::end() ?>
<?= GridView::widget([
'dataProvider' => $dataProviderIndicators,
'summary' => false,
'columns' => [
...
],
]); ?>
== UPDATE==
BaseController
public function actionIndex()
{
$searchBase = new BaseSearch();
$searchBase->pa = 0;
$searchBase->data = date("Y-m-d");
$dataProviderBase = $searchBase->search(Yii::$app->request->queryParams);
$searchMobilizadores = new MobilizadoresSearch();
$dataProviderMobilizadores = $searchMobilizadores->search(Yii::$app->request->queryParams);
$searchIndicadores = new IndicadoresSearch();
$dataProviderIndicadores = $searchIndicadores->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchBase' => $searchBase,
'dataProviderBase' => $dataProviderBase,
'searchMobilizadores' => $searchMobilizadores,
'dataProviderMobilizadores' => $dataProviderMobilizadores,
'searchIndicadores' => $searchIndicadores,
'dataProviderIndicadores' => $dataProviderIndicadores,
]);
}
dont forget define filterModel in your Gridview Widget
view file
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
1st Approach :
You may use a single model that extend from model that joint all 3 table and create a search model for it. If u insist on separating all 3 table in the view, you may just declare different column for each grid.
a tip to achieve this, you may create a view and use gii tool to generate view file.
the tricky part is that 1 model can only hold one table except you joint. But, by joining table, you may get extra rows that are not necessarily needed for lets say your $dataProviderKey grid.
2nd Approach :
manually check for params from Yii::$app->request->get() add filter for each $dataprovider.
controller file
if(isset($id = Yii::$app->request->get('company_id'))) {
$dataProviderKey->query->andFilterWhere(['company.id' => $id]);
$dataProviderProduct->query->andFilterWhere(['company.id' => $id]);
$dataProviderIndicators->query->andFilterWhere(['company.id' => $id]);
}
== UPDATE ==
to answer your update question, you may assign your base search attributes to the respective attributes from others searchModel like this :
public function actionIndex()
{
$searchBase = new BaseSearch();
$searchBase->pa = 0;
$searchBase->data = date("Y-m-d");
$dataProviderBase = $searchBase->search(Yii::$app->request->queryParams);
$searchMobilizadores = new MobilizadoresSearch();
$searchMobilizadores->company = $searchBase->company; -->add this
$dataProviderMobilizadores = $searchMobilizadores->search(Yii::$app->request->queryParams);
$searchIndicadores = new IndicadoresSearch();
$searchIndicadores->company = $searchBase->company; -->add this
$dataProviderIndicadores = $searchIndicadores->search(Yii::$app->request->queryParams);
I have two dropDownlist in index, when I select the submit button it will get action in controller, and it performs some operations and again render the index with gridview, but the Selected dropDownList become blank, how can I set the default in dropDownllist??
Any suggestion should be appreciatable...
this is my view
<div class="col-sm-12" align="center">
<?= $form->field($model, 'fk_int_payroll_month')->dropDownList(
ArrayHelper::map(TblPayrollMonth::find()->all(), 'pk_int_payroll_month_id','vchr_month'),
['prompt'=> 'Select...'])
?>
</div>
<div class="col-sm-12" align="center">
<?= $form->field($model, 'fk_int_payroll_year')->dropDownList(
ArrayHelper::map(TblPayrollYear::find()->all(), 'pk_int_payroll_year_id','year'),
['options' => [isset($_POST['fk_int_payroll_year'])?'fk_int_payroll_year':'' => ['Selected'=>true]]],
['prompt'=> 'Seect...'])
?>
</div>
this is my controller
public function actionDisplay()
{
$model = new TblPayroll();
if(Yii::$app->request->post()!=null)
{
$data = Yii::$app->request->post();
// var_dump($data);
// die;
$month = $data['TblPayroll']['fk_int_payroll_month'];
$year = $data['TblPayroll']['fk_int_payroll_year'];
/* Be careful with this! */
$dataProviderSearch = new ActiveDataProvider
([
'query' => TblPayroll::find()->where(['fk_int_payroll_month'=>$month, 'fk_int_payroll_year'=> $year]),
'pagination' => ['pageSize' => 5],
]);
if($dataProviderSearch)
{
return $this->render('index', [
'dataProviderSearch' => $dataProviderSearch,
'model' => $model,
]);
}
}
else{
return $this->render('index', ['model' => $model]);
}
when i select dropdown it will gott actionDisplay, and find the dataprovider, and then it again render to index, so that time how can i show default selected value in dropDownlist?
If you are using an activeRecord then in controllerAction you should assign to the $model->your_fiedl the value you need for default
else{
$model->fk_int_payroll_month = 3
return $this->render('index', ['model' => $model]);
}
if you don't are using an active record you could use
->dropDownList($yourlist ,$selection)
eg
->dropDownList(ArrayHelper::map(TblPayrollMonth::find()->all() ,3)
I have one search form with a field and a button. On the button click I want to search 2 searchmodel - sellitembtdtSearch and puritembtdtSearch. The result will be shown in one view. I can display the view without any problem. The problem is when I'm searching only one searchmodel is getting searched. Please let me know how can I search both the searchModel at the sametime.
First I land on index2.php page where the form is.
'action' => ['/stock/sellitem/printproductledger3',],
'method' => 'get',
<?= $form->field($model, 'productname')->textInput(['maxlength' => true,]) ?>
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
actionIndex2.php
public function actionIndex2()
{
$searchModel1 = new SellitemsbdtSearch();
$dataProvider1 = $searchModel1->search(Yii::$app->request->queryParams);
$searchModel2 = new PuritemsbdtSearch();
$dataProvider2 = $searchModel2->search(Yii::$app->request->queryParams);
return $this->render('_formbtdt', [
'model' => $searchModel2,
//'searchModel1' => $searchModel2,
]);
}
public function actionPrintproductledger3() {
$searchModel1 = new SellitemsbdtSearch();
$dataProvider1 = $searchModel1->search(Yii::$app->request->get());
$searchModel2 = new PuritemsbdtSearch();
$dataProvider2 = $searchModel2->search(Yii::$app->request->get());
//$productname = yii::$app->request->get('productname');
//$prodesc = yii::$app->request->get('prodesc');
$content = $this->renderPartial('_printproductledgerbtdt', [
//'productname' => $productname,
'searchModel1' => $searchModel1,
'dataProvider1' => $dataProvider1,
'searchModel2' => $searchModel2,
'dataProvider2' => $dataProvider2,
//'prodesc' => $prodesc,
]);
return $this->render('_printproductledgerbtdt', [
'dataProvider1' => $dataProvider1,
'searchModel1' => $searchModel1,
'searchModel2' => $searchModel2,
'dataProvider2' => $dataProvider2,
]);
This code only searchs puritemdtdtSearch. I want to search puritembtdtSearch and sellitembtdtSearch at the same time. Thanks.
The issue is you are using searchModel2 in your form so you are just getting the result of searchModel2. And not getting the result of searchModel1.
What you can do is send searchModel1 to your _search file.
<?php echo $this->render('_search', ['model1' => $searchModel1, 'model2' => $searchModel2]); ?>
Now in your form use a hidden input.
<?php echo $form->field($model1, 'productName')->textInput(['id' => 'model1']); ?>
<?php echo $form->field($model2, 'prodcutName')->hiddenInput(array('id' => 'model2')); ?>
Now its that we have two model fields we need to populate the hidden field, this can be done using jquery.
<?php $this->registerJs('
//add the .search class name for your search button
jQuery("body").on("click", ".search", function() {
alert("Hello");
var a = $("#model1").val();
$("#model2").attr("value", a);
});
');?>
Try this..tested completely and works fine!!
I need to put on the page to search by protocol number, and display only one result when the entered protocol number is exactly the same. If possible the results to be shown, it is a DetailView
My model OccurrenceSearch:
public function searchprotocol($params)
{
$query = Occurrence::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
return $dataProvider;
}
$query->andFilterWhere([
'protocol' => $this->protocol,
]);
return $dataProvider;
}
}
My view search:
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
<div class="occurrence-search">
<?php $form = ActiveForm::begin([
'action' => ['search'],
'method' => 'get',
]); ?>
<?= $form->field($model, 'protocol') ?>
<div class="form-group">
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
My controller actionSearch() :
public function actionSearch()
{
$searchModel = new OccurrenceSearch();
$dataProvider = $searchModel->searchprotocol(Yii::$app->request->queryParams);
return $this->render('search', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
The guest user can see only the record for its protocol number
The a detailView use model (and not dataProvider) for show the data instance ..
then in you controller you should obtain the model
public function actionSearch()
{
$searchModel = new OccurrenceSearch();
$dataProvider = $searchModel->searchprotocol(Yii::$app->request->queryParams);
$model = $dataProvider->query->one();
return $this->render('search', [
'searchModel' => $searchModel,
//'dataProvider' => $dataProvider,
'model' => $model,
]);
}
But in you view the code proposed don't show a detailView widget .. you should probably add
I decided to use the GRIDVIEW, and so changed the MymodelSearch
if (isset($_GET['AuthorSearch']) && !($this->load($params) && $this->validate())) {
return $dataProvider;
}