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

Adds the trasform callback that create the option from key and value #57

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from

Conversation

fcamp
Copy link

@fcamp fcamp commented May 23, 2017

Can be used to modify the option.
By default, the options can have just a value and a label,
but in this way data properties can be added or we can apply other transformations.

For example when I change an option,
I could load the associated image, or check if that item is a leaf or it has children, without having to launch a request, but using the data from the json response I get on change.

Can be used to modify the option.
By default, the options can have just a value and a label,
but in this way `data` properties can be added or we can apply other transformations.
@tuupola
Copy link
Owner

tuupola commented Jun 2, 2017

I kind of like this, but I have hard time figuring out the use case you explained. Do you have an example code snippet how you use it at the moment?

@fcamp
Copy link
Author

fcamp commented Jun 5, 2017

I have a category tree which is not uniform.
Sometimes the leaf (a category with 0 children) are at level 0, sometimes they are at level 1 or 2.

Whenever I find a leaf, I set an hidden field (coming from a symfony form), with the category id.

Without the transform callback, I should request the server just to know if that category is a leaf or not.

In this way we can work with a richer json response, not just the {"key":"value"}

This is the callback applied to each item on the server side

<?php
    ...

    /**
     * @param Category $category
     * @return array
     */
    public function transform(Category $category)
    {
        return [
            $category->getId() => [
                'label' => $category->getTitle(),
                'children' => $category->getChildrenCount(),
            ]
        ];
    }

This is part of the js code

$("#js-category-1").remoteChained({
    parents: "#js-category-0",
    url: "/my/url",
    loading: {'label': 'Loading...'},
    transform: function (key, value) {
        return $('<option/>', {
            'data-has-children': value.children,
            'value': key
        }).append(value.label);
    }
});

@fcamp
Copy link
Author

fcamp commented Feb 13, 2018

Hi, Is there any chance that this change will be merged?

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

Successfully merging this pull request may close these issues.

2 participants