origgami / CMB2-grid

A grid system for Wordpress CMB2 library that allows columns creation
88 stars 21 forks source link

How to assign columns to repeatable groups #27

Open ShariqKhan2012 opened 8 years ago

ShariqKhan2012 commented 8 years ago

I was just wondering, is it possible to assign columns to repeatable groups ?

I did something like this:

            $prefix      = '_group_columns_test';
            $cmb_group   = new_cmb2_box( array(
                'id'             => $prefix . 'metabox',
                'title'          => __( 'Repeating Field Group using a Grid', 'cmb2' ),
                'object_types'   => array( 'page' ),
            ) );
            // $group_field_id is the field id string, so in this case: $prefix . 'demo'
            $group_field_id  = $cmb_group->add_field( array(
                'id'         => $prefix . 'demo',
                'type'       => 'group',
                'options'    => array(
                    'group_title'    => __( 'Entry {#}', 'cmb2' ), // {#} gets replaced by row number.
                    'add_button'     => __( 'Add Another Entry', 'cmb2' ),
                    'remove_button'  => __( 'Remove Entry', 'cmb2' ),
                    'sortable'       => true,
                ),
            ) );
            $gField1         = $cmb_group->add_group_field( $group_field_id, array(
                'name'   => __( 'Entry Title', 'cmb2' ),
                'id'     => 'title',
                'type'   => 'text',
            ) );
            $gField2         = $cmb_group->add_group_field( $group_field_id, array(
                'name'           => __( 'Description', 'cmb2' ),
                'description'    => __( 'Write a short description for this entry', 'cmb2' ),
                'id'             => 'description',
                'type'           => 'textarea_small',
            ));

            $cmb2Grid = new \Cmb2Grid\Grid\Cmb2Grid( $cmb_group );
            // Create now a Grid of group fields.
            $cmb2GroupGrid   = $cmb2Grid->addCmb2GroupGrid( $group_field_id );

            $row = $cmb2Grid->addRow();
            $row->addColumns( array(
                $cmb2GroupGrid
            ) );

This way the group comes up fine, but it takes up full width.

If I change the last line to :

            $row->addColumns( array(
                array( $cmb2GroupGrid, 'class' => 'col-md-6' ),
            ) );

then it gives me fatal error:

Fatal error: Cannot use object of type Cmb2Grid\Grid\Group\Cmb2GroupGrid as array

Now if I add another group to make my code look like this:

            $prefix      = '_group_columns_test';
            $cmb_group   = new_cmb2_box( array(
                'id'             => $prefix . 'metabox',
                'title'          => __( 'Repeating Field Group using a Grid', 'cmb2' ),
                'object_types'   => array( 'page' ),
            ) );
            // $group_field_id is the field id string, so in this case: $prefix . 'demo'
            $group_field_id  = $cmb_group->add_field( array(
                'id'         => $prefix . 'demo',
                'type'       => 'group',
                'options'    => array(
                    'group_title'    => __( 'Entry {#}', 'cmb2' ), // {#} gets replaced by row number.
                    'add_button'     => __( 'Add Another Entry', 'cmb2' ),
                    'remove_button'  => __( 'Remove Entry', 'cmb2' ),
                    'sortable'       => true,
                ),
            ) );
            $gField1         = $cmb_group->add_group_field( $group_field_id, array(
                'name'   => __( 'Entry Title', 'cmb2' ),
                'id'     => 'title',
                'type'   => 'text',
            ) );
            $gField2         = $cmb_group->add_group_field( $group_field_id, array(
                'name'           => __( 'Description', 'cmb2' ),
                'description'    => __( 'Write a short description for this entry', 'cmb2' ),
                'id'             => 'description',
                'type'           => 'textarea_small',
            ));

            $group_field_id2     = $cmb_group->add_field( array(
                'id'         => $prefix . 'demo2',
                'type'       => 'group',
                'options'    => array(
                    'group_title'    => __( 'DEntry {#}', 'cmb2' ), // {#} gets replaced by row number.
                    'add_button'     => __( 'Add Another DEntry', 'cmb2' ),
                    'remove_button'  => __( 'Remove Entry', 'cmb2' ),
                    'sortable'       => true,
                ),
            ) );
            $gField12        = $cmb_group->add_group_field( $group_field_id2, array(
                'name'   => __( 'DEntry Title', 'cmb2' ),
                'id'     => 'title2',
                'type'   => 'text',
            ) );
            $gField22        = $cmb_group->add_group_field( $group_field_id2, array(
                'name'           => __( 'Description', 'cmb2' ),
                'description'    => __( 'Write a short description for this entry', 'cmb2' ),
                'id'             => 'description2',
                'type'           => 'textarea_small',
            ));

            if ( ! is_admin() ) {
                return;
            }
            // Create a default grid.
            $cmb2Grid = new \Cmb2Grid\Grid\Cmb2Grid( $cmb_group );
            // Create now a Grid of group fields.
            $cmb2GroupGrid   = $cmb2Grid->addCmb2GroupGrid( $group_field_id );      

            $cmb2GroupGrid2  = $cmb2Grid->addCmb2GroupGrid( $group_field_id2 );

            $row = $cmb2Grid->addRow();
            $row->addColumns( array(
                $cmb2GroupGrid, // Can be $group_field_id also.
                $cmb2GroupGrid2
                //array( $cmb2GroupGrid, 'class' => 'col-md-8' ),
                //array( $cmb2GroupGrid2, 'class' => 'col-md-4' )
            ) );

then the groups come fine in 2 columns. However, the problem is that if I click on add button to create one more type of the group. then the new instance comes just below it, it does not come to the next available column.

Perhaps a screenshot would explain better:

gridcolumns

sabbiseaan commented 6 years ago

NO solution here???