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: registerJs in view which is rendered without layout
yii2 How to transfer post data from one view to two?
Difference between afterSave and a Event in yii2?
Yii2 karthik editable column first row not working
Yii2 Select only few columns from related model
Yii2 Logout link issue
Displaying subdirectory name in the url in Yii2 for static pages
Perform calculations before recording in the archive
How to get params in action passed via runAction in Yii2
Issue in yii2 timezone formatter
Pushstate in pjax only when redirecting
How do you make a image as your submit Button in yii2
Open in a new window in yii2 [For a button dropdown]
Display HAS_MANY relation data in DetailView yii2
Yii2 kartik datetime picker widget doesn´t function?
Use built-in rbac or build own?

Categories

HOME
twitter
image
hook
angular-material
hashmap
routes
gps
google-project-tango
uber-api
cross-validation
amazon-cloudformation
wheelnav.js
quicklook
gz
dtrace
designer
netflix
sms-gateway
dbext
emulator
custom-wordpress-pages
pass-by-reference
cultureinfo
poltergeist
bootstrap-duallistbox
traffic
filezilla
applozic
xacml
srcset
airconsole
normal-distribution
android-browser
ansible-playbook
splice
particles.js
google-closure
eigenvalue
fakeiteasy
no-www
apple-news
taffy
apache-fop
blogengine.net
minimization
grails-tomcat-plugin
home-directory
file-writing
persist
prettytensor
rvest
hexdump
suffix-tree
wyam
drawbitmap
microbenchmark
method-parameters
fluid-dynamics
cisco-ios
directoryservices
formatjs
facebook-graph-api-v2.4
c3
mdt
rebol3
html5-notifications
runtime.exec
odata4j
opcache
excel-2003
type-equivalence
clipper
path-separator
coderush
regsvr32
ohm
boost-filesystem
window-management
psi
javax.script
createwindow
zend-translate
database-management
multi-tier
kdbg
exchange-server-2003
swing-app-framework
boost-smart-ptr
nerddinner
wsdl.exe





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