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 datepicker show start date greater than current date
Yii2 : Model values not been not been saved in DB in
check a specific submit button value in controller (Yii2)
Load data to a form from a different model in yii2
How to use html tag for div, without ending div
How do I use opposite sign in where condition in dataProvider?
The view path doesnot exists in yii2 in server
Integrate PHPbb in YII2.0
How to integrate ckfinder with yii2
Remove encoding for popovers on labels
How can I set safe or allow empty captcha on scenario?
Kartiv yii2 datepicker with model and custom id
yii2: geometry type column in migration table
yii2 + kartik exportMenu not working on modal window
Yii2 redirecting to domain root
Yii2 Webvimark method not allowed

Categories

HOME
ms-access
gerrit
drivers
plone
onedrive
tesseract
yahoo-oauth
filtering
graphql
electronics
azure-media-services
django-imagekit
append
pheatmap
reveal.js
imacros
after-effects
invantive-sql
undefined
database-replication
dbext
xlsxwriter
physics-engine
extjs5
mustache.php
restful-authentication
claims-based-identity
rundeck
p-value
opennlp
vlsi
buildbot
tdd
phpfox
neo4j-spatial
replaceall
webkitspeechrecognition
tasklet
instant-messaging
data-manipulation
siesta-swift
code-contracts
protovis
http-referer
wixsharp
stacked
swisscomdev
xenforo
optix
nomethoderror
fakeiteasy
outlook-api
eclipse-gef
executenonquery
osx-mavericks
integrity
csound
directory-structure
gcsfuse
sqldf
iso8601
background-service
xcb
heidisql
infix-notation
database-optimization
asp.net-4.5
transmitfile
xml-attribute
jwplayer7
bluemix-app-scan
suffix-tree
revolution-r
associative-array
iad
thredds
purge
tableau-online
geonetwork
rdtsc
device-manager
tws
easy-install
limejs
flashvars
word-processor
dsn
adk
php-parser
gnustep
locationlistener
mongomapper
radcombobox
dbal
rescale
database-management
web-architecture
data-driven
simpletest
defensive-programming

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