从窗体字段获取数据到按钮值yii2
问题描述:
我有一个表单域和一个窗体中的按钮。该按钮应从表单字段获取数据并将该值作为参数传递。请告诉我如何从表单字段获取数据并传递值。从窗体字段获取数据到按钮值yii2
表单字段 -
<?= $form->field($model, 'productname')->textInput(['maxlength' => true]) ?>
按钮
<?= Html::a('Search', ['/stock/sellitem/printproductledger3', 'productname' => '8904187001305'], ['class'=>'btn btn-primary']) ; ?>
上面的例子做工精细,因为该值已经在这里给出'productname' => '8904187001305'
我需要做的是从表单字段中获得的价值'8904187001305'
。请帮忙。
的actionIndex
public function actionIndex2()
{
$searchModel = new SellitemsbdtSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('_formbtdt', [
'model' => $searchModel,
]);
}
该指数导致_formdtbt
'action' => ['/stock/sellitem/printproductledger3',],
<?= $form->field($model, 'productname')->textInput(['maxlength' => true,]) ?>
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
控制器动作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,
]);
}
嗯,我想我已经指出了错误的问题。问题是模型sellitemsbdtSearch
正在过滤,但puritemsbdtSearch
没有得到过滤。
搜索型号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;
}
}
搜索型号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;
}
}
答
你可以用这种方式为模型的值发送到你的动作
<?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(); ?>
一次提交你不能以normale yii2的方式获得价值
你可以看到本作更多.. http://www.yiiframework.com/doc-2.0/guide-input-forms.html
对于不具有相同的realetd模型,并提交作为第一个你应该使用
// 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']]]);
谢谢你的回答第二个模型。但它不能解决问题。我试过了 - 'action'=> ['/ stock/sellitem/printproductledger3','productname'=> $ model-> productname],但该值不是以'param'=>'value'的形式传递的。我收到错误 - 缺少必需的参数:来自控制器操作的产品名称。 – Tanmay
在我的答案张贴的方式价值作为后或传递,所以你可以使用$ post = Yii :: $ app-> request-> post()和$ post ['your_field_name']来obatin值如果您需要seacrh的参数,请尝试查看http://www.yiiframework.com/doc-2.0/guide-output-data-widgets.html#filtering-data分离的过滤器表单... – scaisEdge
此链接有帮助。我会研究它。谢谢。 – Tanmay