• Resolved magicpriest

    (@magicpriest)


    Hello!
    I’m wondering is there any way to load data as select field value through ajax or any other way. The idea is get all posts categories (already done) within select field and load all posts inside new select field according on selected category.

    PS:
    I’m using CF for a short time, but already amazed with this library!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor Atanas Angelov

    (@atanasangelovdev)

    Hi @magicpriest ,

    Thank you for the kind words!

    > I’m wondering is there any way to load data as select field value through ajax or any other way

    Currently, such functionality is not supported in core but if you are familiar with React you could write your own extension field for this purpose (https://carbonfields.net/docs/extending-extending/?crb_version=2-0-0).

    If you are looking for a quick and dirty solution, you could hack in a simple select using jQuery which updates its options when a field’s value changes by utilizing the appropriate event in the JavaScript API (https://carbonfields.net/docs/advanced-topics-javascript-api/?crb_version=2-0-0) but you’ll have to handle it’s own data saving and loading.

    Thread Starter magicpriest

    (@magicpriest)

    Hi @atanasangelovdev ,
    Thank you for fast reply.

    I’m here just to say thank you for your work and provide some thoughts during this work.
    So, the way I have chosen is constructing data and Fields. The idea is simple, first of all get what you need (for me it CustomPostType terms and posts which belongs to certain term and pack it in way you like), then construct arrays of fields, for example:

    
    $fieldConstructor[] = 
            Field::make( 'select', $term['term_slug'], $term['term_name'] )
                ->add_options( $term['term_posts'] )
    

    And then add conditional logic in your construction. It’s so awesome, to have all things in one place and manipulating data without annoyance.

    The last thing, is opinion of Devs of this library. Is this dirty or aboutToBeDirty hack?

    Plugin Contributor Atanas Angelov

    (@atanasangelovdev)

    I’m not exactly sure what you are doing exactly but keep in mind that conditionally created fields will be created conditionally when a user visits the site as well (not only for admin requests) and you cannot get a field’s value if the field has not been defined for the current request (2.0+).

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Ajaxify field’ is closed to new replies.