yii2


How to create a new input field which would offer values stored in different database table? Yii2


It's pretty hard to formulate the question correctly, but I'll try to explain it more here. I have an Item, Category and Sale models. In category and Item models I manually stored data. Now I've created a form, which has some input fields and dependent drop-down, which is:
According to the selected category, items are loaded.
Now I need to create a new input field, which would be price and it has to be taken from the database as a offer (f.e if the user selects Item1 -> Item1 price has to be loaded up as a offer in the input field, which I could edit it and store in different database table.
How should I do that?
Here is my SaleController action:
/**
* Creates a new sale
*
* #return string
*/
public function actionCreate()
{
$model = new Sale();
$model->scenario = Sale::SCENARIO_CREATE;
$category = new Category();
$category->scenario = Category::SCENARIO_CREATE;
$categoryList = ArrayHelper::map(Category::find()->all(), 'id', 'name');
if ($model->load(Yii::$app->request->post())) {
if ($model->save()) {
return $this->redirect(['index']);
}
}
return $this->render('create', [
'model' => $model,
'category' => $category,
'categoryList' => $categoryList,
]);
}
And here is my view:
<?= $form->field($category, 'id')->dropDownList($categoryList, [
'id' => 'category-id',
'prompt' => 'Choose a category',
]); ?>
<?= $form->field($model, 'item_id')->widget(DepDrop::classname(), [
'options'=>['id'=>'item-id'],
'pluginOptions'=>[
'depends'=>['category-id'],
'placeholder'=> 'Choose an item',
'url'=>Url::to(['/sale/subcat'])
]
]); ?>
<?= $form->field($model, 'price') ?>
Thank you for any help. I hope you understand the question
First you must add an input field in your view from model:
<?= $form->field($model, 'item_price')->textInput([
'maxlength' => true,
'id' => 'input-price-id'
]) ?>
Then you need to add some javascript:
<?php
$item_price=<<<JS
$('#item-id').on('change', function(event) {
var id = $(this).attr('id');
var val = $(this).val();
.get(
// you must code function actionRefreshPrice wtih parameter named item_id in
// your controller with fetch from any table you want
'refresh-price',
{
item_id: val // this is id from your item
},
function (data) {
// here you set a value returned from ajax call
// you must have an input element with id input-price-is (change as you like)
$('#input-price-id').val(data);
}
);
});
JS;
$this->registerJs($item_price);
?>
Then you must add a controller action something like this:
public function actionRefreshPrice($item_id) {
// I asume you have table with Price for items prices
$price = Price::findOne(['item_id'=>$item_id]);
return ($price?0:$price->price_field);
}
I hope I have given you enough guidelines. And a comment learn more about models and relations. I think you overthought problem a little bit. Happy learning and coding.

Related Links

Yii2 - how to get the selected value from DepDrop
How to use models from different domain in frontend's site controller?
frontend login is not working properly in yii2
Yii Gridview show/hide columns from interface when filtering the records
Yii2 skip to other URL route if <controller>/<action> is not found
Include additional library to yii2
Yii2 make session works on all actions of one controller?
Yii2 display data from phpexcel in view before saving to database
yii2 dependant dropdown reset on edit action due to onchange function
Yii2 non-DB (or virtual) attribute isn't populated during massive assignment?
Yii2 Join multiple tables
Codeception Adding custom assert actions (functional testing)
Selected values in yii2
Yii queryBuilder; If dbexpression is not being quoted well
Time attribute validation in Yii2 framework
Yii2 kartik-datecontrol extension

Categories

HOME
log4j
winforms
bluetooth
testng
cookies
openstack
isabelle
gremlin
plone
homebrew
read-eval-print-loop
google-docs
setup-deployment
survey
uber-api
gorm
spring-xd
dax
resize
size
static-libraries
highlight.js
nhibernate-envers
samoa
lucene.net
clickonce
opencover
dcevm
extjs5
rundeck
web-api-testing
opentype
quote
preg-match
protovis
mozilla
libraries
libvpx
theano.scan
splice
modelmapper
long-polling
outlook-2013
azure-application-gateway
jquery-validate
yii2-extension
crypt
hittest
pintos
jquery-filter
android-fonts
file-writing
nsfilemanager
ctest
bstr
skype4py
ready-api
sdf
suffix-tree
php-parse-error
design-by-contract
asp.net5
rtbkit
srand
r-tree
teamcity-8.0
collapse
python-3.2
map-projections
riak-cs
0xdbe
deis
unity5.2.3
website-monitoring
rake-task
gwidgets
codeigniter-url
gadt
websocket4net
eol
typeof
contenttype
jubula
braille
ivyde
enterprisedb
ocunit
typoscript2
bitsharp
labwindows
jquery-ui-layout
semantic-diff
punbb
cufon
window-management
pydot
spyware
external-accessory
celltable
actionview
sproutcore-2
mirah
perfect-hash
kdbg
photoshop-cs4
spec#
scripting-languages
windows-live-messenger
lzh

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App