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

Proposal to change SelectArray plugin #490

Closed
Antikon opened this issue May 14, 2020 · 3 comments
Closed

Proposal to change SelectArray plugin #490

Antikon opened this issue May 14, 2020 · 3 comments

Comments

@Antikon
Copy link
Contributor

Antikon commented May 14, 2020

The SelectArray plugin has bad behavior, which leads to a change in the model attribute (see #439).
This behavior can be disabled by novel assignAfterFind option (#444). Howewer, this option disables autoconversion of data in a list view also.

I suggest changing renderList method of SelectArray or Select class and introducing new AngularJs directive selectArraySpan in the following way.

public function renderList($id, $ngModel)
    {
        if ($this->scheduling && $this->renderContext->canUpdate()) {
            return $this->createSchedulerListTag($ngModel, $this->getData(), 'item');
        }

        $options = Angular::optionsFilter([
            'options' => $this->getServiceName('selectdata'),
        ]);

        return $this->createTag('select-array-span', null, ArrayHelper::merge(['model' => $ngModel], $options));
    }
zaa.directive('selectArraySpan', function () {
    return {
        restrict: 'E', //E = element, A = attribute, C = class, M = comment
        scope: { //@ reads the attribute value, = provides two-way binding, & works with functions
            'model': '=',
            'options': '=',
            'optionsvalue': '@optionsvalue',
            'optionslabel': '@optionslabel',
        },
        controller: ['$rootScope', '$scope',  function ($rootScope, $scope) {
            if ($scope.optionsvalue === undefined) {
                $scope.optionsvalue = 'value';
            }
            if ($scope.optionslabel === undefined) {
                $scope.optionslabel = 'label';
            }

            $scope.getSelectedLabel = function () {
                // Keep raw value by default
                var selectedLabel = $scope.model;
                angular.forEach($scope.options, function (item) {
                    if ($scope.model == item[$scope.optionsvalue]) {
                        defaultLabel = item[$scope.optionslabel];
                    }
                });

                return defaultLabel;
            };
        }],


        template: function () {
            return '<span>{{getSelectedLabel()}}</span>';
        }
    };
});

I intentionally do not create PR, want to discuss first.

If such a solution does not find objection, I suggest changing a default behavior of SelectArray plugin in the next major version.

For now, we can inroduce SelectArray2 plugin, for example.

@nadar
Copy link
Member

nadar commented May 15, 2020

SelectArray was one of the first plugins in LUYA and is therefore used very often, so we should keep it like this, but i total agree, introducing a new/other SelectArray would be great. Maybe could name it dropdownSelect like yii active field does, but one of the big issues there is also: how to load the data and should the plugin auto observe/change the original value from the database.

@Antikon
Copy link
Contributor Author

Antikon commented May 15, 2020

I don't mind "dropdownSelect" name.

The data is loaded via

$options = Angular::optionsFilter([
    'options' => $this->getServiceName('selectdata'),
]);

It will be updated each time we load or reload CRUD data through api request.

The displayed value in the CRUD list will change of course when the model changes someway.
In the CRUD create or update forms the form field will be the same as current in SelectArray plugin.

What about the name of AngularJs directive?

@Antikon
Copy link
Contributor Author

Antikon commented Sep 1, 2021

Closed by #658

@Antikon Antikon closed this as completed Sep 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants