Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[initialize = true] do not work sometimes #16

Closed
aleksanderd opened this issue Jun 3, 2015 · 14 comments
Closed

[initialize = true] do not work sometimes #16

aleksanderd opened this issue Jun 3, 2015 · 14 comments

Comments

@aleksanderd
Copy link

It work ok most times, but 1-2 times for ~10 tries I have empty value for last selector :( The list is loaded ok and able to select, but value of the field is lost :(

@kartik-v
Copy link
Owner

kartik-v commented Jun 3, 2015

This is resolved with latest release of kartik-v/dependent-dropdown plugin. Check the updated demos.

@kartik-v kartik-v closed this as completed Jun 3, 2015
@aleksanderd
Copy link
Author

Huge thnx, but now it is always empty value :( I noticed in my case work this line: return false since I use Select2.

@kartik-v
Copy link
Owner

kartik-v commented Jun 4, 2015

You need to check the documentation and demos closely again on HOW you need to set your nested dependency. Check the demo page for usage with Select2.

Also note that if you need to set a value via ajax ... you need to return the selected property with your json data.

@aleksanderd
Copy link
Author

I "smoking" the docs second day :) May be you can find a minute to review my code plz?

if (Yii::$app->user->identity->isAdmin) {
    $userData = ArrayHelper::map(User::find()->asArray()->all(), 'id', 'username');
    echo $form->field($model, 'user_id')->widget(Select2::className(), [
        'data' => $userData,
        'hideSearch' => true,
        'pluginOptions' => [
            'placeholder' => $userPlaceholder,
            'allowClear' => true,
        ],
    ])->label(Yii::t('app', 'User'));
} else {
    echo Html::activeHiddenInput($model, 'user_id');
}

echo $form->field($model, 'site_id')->widget(DepDrop::className(), [
    'type' => DepDrop::TYPE_SELECT2,
    'data' => [$model->site_id => 'xxx'],
    'pluginOptions' => [
        'placeholder' => $sitePlaceholder,
        'url' => Url::toRoute('client-site/select-list'),
        'depends' => ['variable-user_id'],
//        'initDepends' => ['variable-user_id'],
//        'initialize' => true,
    ],
    'select2Options' => [
        'hideSearch' => true,
        'pluginOptions' => [
            'allowClear' => true,
        ],
    ],
])->label(Yii::t('app', 'Site'));

echo $form->field($model, 'page_id')->widget(DepDrop::className(), [
    'type' => DepDrop::TYPE_SELECT2,
    'data' => [$model->page_id => 'xxx'],
    'pluginOptions' => [
        'placeholder' => $pagePlaceholder,
        'url' => Url::toRoute('client-page/select-list'),
        'depends' => ['variable-user_id', 'variable-site_id'],
        'initDepends' => ['variable-user_id', 'variable-site_id'],
        'initialize' => true,
    ],
    'select2Options' => [
        'hideSearch' => true,
        'pluginOptions' => [
            'allowClear' => true,
        ],
    ],
])->label(Yii::t('app', 'Page'));

The actions do not returns 'selected' at all, just 'output'...

After some modest research, I found if I change this line in init function:

self.setDep($id, depends, len, false);

to

self.setDep($id, depends, len, $el.val());

it work but excepting last child...

Also, some value issues in beforeSend callback of proccessDep. For ex., if I add:

var elVal = $el.val();

at the first line of processDep, and add:

if (selected === null) {
  selected = elVal;
}

at second to success callback of processDep, its also work, but except last child again...

thnx

@kartik-v
Copy link
Owner

kartik-v commented Jun 4, 2015

In the last child dropdown change your widget plugin configuration to the following:

[
'initDepends' => ['variable-user_id'],  // this should be the topmost parent
'depends' => ['variable-site_id'], // this should be the immediate parent
'initialize' => true
]

Also ensure you got the latest code for kartik-v/yii2-widget-select2, kartik-v/yii2-widget-depdrop, and kartik-v/dependent-dropdown

@aleksanderd
Copy link
Author

Huge thnx! It make understaning depends and initDepends more clear.
... And it works! But only if I change one line in init :)
from:

self.setDep($id, depends, len, false);

to:

self.setDep($id, depends, len, $el.val());

Could you explain what the false means here? The parameter seems to be for the value, but there is only one call with 'false' :o
Btw, why not to just store current value in a variable at begining of proccessDep and restore it if nothing other given at success callback, instead of passing the value as param thru several calls?

thnx!
// using dev-master for all 3 packages
// also, just deleted and regrabbed them with 'composer up'

@aleksanderd
Copy link
Author

Also, I just noticed, Select2 clear action do not fire the refreshing of deps. Just yerstarday it was ok.

@kartik-v
Copy link
Owner

kartik-v commented Jun 4, 2015

As mentioned before -

Ensure you got the latest code updated for kartik-v/yii2-widget-select2, kartik-v/yii2-widget-depdrop, and kartik-v/dependent-dropdown

There are updates to all the above extensions.

I fixed the above as well for $el.val() initialization - but note --- if you are passing the selected from your ajax server code it should get anyway overwritten and it is recommended if you want to set default value for each dropdown after every change to a dependent parent.

@aleksanderd
Copy link
Author

composer.json part:

...
        "kartik-v/yii2-widget-select2": "dev-master",
        "kartik-v/dependent-dropdown": "dev-master",
        "kartik-v/yii2-widget-depdrop": "dev-master",
...

is it ok?
Also, I see your just made changes by 'composer up'

Anyway, for now all ok... almost :) The 'clear' of DepDrop widgets work ok, but 'clear' of Select2 (user_id most parent widget in my case code abobe) - still not fire ajax connects for the childs :(

Also, I noticed(after today updates), there is 'open/close' of child selector's when 'clear' clicked.

thnx

@kartik-v
Copy link
Owner

kartik-v commented Jun 4, 2015

With regards Select2 --- refer this issue There is an issue in the Select2 plugin which is been reported to the Select2 Plugin team

I have provided a temporary workaround fix (hack) in the yii2-widget-select2 to overcome the above issue. It ideally should get resolved completely once the fix is provided in the plugin.

@kartik-v
Copy link
Owner

kartik-v commented Jun 4, 2015

Ok this is resolved now - update all packages via composer.

@aleksanderd
Copy link
Author

Huge thanks!

but It seems the very last update add new 'clear' related issue. Now it fire ajax, but with previous(not 0|null) id.

@kartik-v
Copy link
Owner

kartik-v commented Jun 4, 2015

This is resolved.

@aleksanderd
Copy link
Author

Seems to be all ok for now :)
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants