• 1.
    /***  GF List View Fixed Rows  ***/
  • 2.
     
  • 3.
    class GWListFixedRows {
  • 4.
     
  • 5.
      private static $_is_script_output;
  • 6.
      
  • 7.
      public function __construct( $args = array() ) {
  • 8.
     
  • 9.
        // set our default arguments, parse against the provided arguments, and store for use throughout the class
  • 10.
        $this->_args = wp_parse_args( $args, array(
  • 11.
          'form_id'       => false,
  • 12.
          'rowCount'      => 1,
  • 13.
          'list_field_id' => false
  • 14.
        ) );
  • 15.
     
  • 16.
        // do version check in the init to make sure if GF is going to be loaded, it is already loaded
  • 17.
        add_action( 'init', array( $this, 'init' ) );
  • 18.
     
  • 19.
      }
  • 20.
     
  • 21.
      public function init() {
  • 22.
     
  • 23.
        // make sure we're running the required minimum version of Gravity Forms
  • 24.
        if( ! property_exists( 'GFCommon', 'version' ) || ! version_compare( GFCommon::$version, '1.8', '>=' ) ) {
  • 25.
          return;
  • 26.
        }
  • 27.
     
  • 28.
        // time for hooks
  • 29.
        add_filter( 'gform_pre_render', array( $this, 'load_form_script' ), 10, 2 );
  • 30.
        add_filter( 'gform_register_init_scripts', array( $this, 'add_init_script' ), 10, 2 );
  • 31.
     
  • 32.
      }
  • 33.
     
  • 34.
      public function load_form_script( $form, $is_ajax_enabled ) {
  • 35.
     
  • 36.
        if( $this->is_applicable_form( $form ) && ! has_action( 'wp_footer', array( $this, 'output_script' ) ) ) {
  • 37.
          add_action( 'wp_footer', array( $this, 'output_script' ) );
  • 38.
          add_action( 'gform_preview_footer', array( $this, 'output_script' ) );
  • 39.
        }
  • 40.
     
  • 41.
        return $form;
  • 42.
      }
  • 43.
     
  • 44.
      public function output_script() {
  • 45.
        ?>
  • 46.
     
  • 47.
        <script type="text/javascript">
  • 48.
     
  • 49.
                window.gwalfr;
  • 50.
     
  • 51.
                (function($){
  • 52.
     
  • 53.
                    gwalfr = function( args ) {
  • 54.
     
  • 55.
                        this.formId      = args.formId,
  • 56.
                            this.listFieldId = args.listFieldId,
  • 57.
                            this.rowCount = args.rowCount;
  • 58.
     
  • 59.
                        this.init = function() {
  • 60.
     
  • 61.
                            // update rows on page load
  • 62.
                            this.updateListItems( this.rowCount, this.listFieldId, this.formId );
  • 63.
     
  • 64.
                        }
  • 65.
     
  • 66.
                        this.updateListItems = function( count, listFieldId, formId ) {
  • 67.
     
  • 68.
                            var listField = $( '#field_' + formId + '_' + listFieldId );
  • 69.
                            rowCount = listField.find( 'table.gfield_list tbody tr' ).length,
  • 70.
                                diff = count - rowCount;
  • 71.
     
  • 72.
                            if( diff > 0 ) {
  • 73.
                                for( var i = 0; i < diff; i++ ) {
  • 74.
                                    listField.find( '.add_list_item:last' ).click();
  • 75.
                                }
  • 76.
                            } else {
  • 77.
     
  • 78.
                                // make sure we never delete all rows
  • 79.
                                if( rowCount + diff == 0 )
  • 80.
                                    diff++;
  • 81.
     
  • 82.
                                for( var i = diff; i < 0; i++ ) {
  • 83.
                                    listField.find( '.delete_list_item:last' ).click();
  • 84.
                                }
  • 85.
     
  • 86.
                            }
  • 87.
                        }
  • 88.
     
  • 89.
                        this.init();
  • 90.
     
  • 91.
                    }
  • 92.
     
  • 93.
                })(jQuery);
  • 94.
     
  • 95.
        </script>
  • 96.
     
  • 97.
        <?php
  • 98.
      }
  • 99.
     
  • 100.
      public function add_init_script( $form ) {
  • 101.
     
  • 102.
        if( ! $this->is_applicable_form( $form ) ) {
  • 103.
          return;
  • 104.
        }
  • 105.
     
  • 106.
        $args = array(
  • 107.
          'formId'      => $this->_args['form_id'],
  • 108.
          'listFieldId' => $this->_args['list_field_id'],
  • 109.
          'rowCount' => $this->_args['row_count']
  • 110.
        );
  • 111.
     
  • 112.
        $script = "new gwalfr(" . json_encode( $args ) . ");";
  • 113.
        $key = implode( '_', $args );
  • 114.
     
  • 115.
        GFFormDisplay::add_init_script( $form['id'], 'gwalfr_' . $key , GFFormDisplay::ON_PAGE_RENDER, $script );
  • 116.
      }
  • 117.
     
  • 118.
      public function is_applicable_form( $form ) {
  • 119.
     
  • 120.
        $form_id = isset( $form['id'] ) ? $form['id'] : $form;
  • 121.
     
  • 122.
        return empty( $this->_args['form_id'] ) || $form_id == $this->_args['form_id'];
  • 123.
      }
  • 124.
     
  • 125.
    }
  • 126.
     
  • 127.
    # The Field
  • 128.
    new GWListFixedRows( array(
  • 129.
      'form_id' => X,
  • 130.
      'list_field_id' => XXX,
  • 131.
      'row_count' => X
  • 132.
    ) );
  • 133.
     
  • 134.
     
  • 135.
    /***  END GF List View Fixed Rows  ***/
  • 136.
     
/*** GF List View Fixed Rows ***/ class GWListFixedRows { private static $_is_script_output; public function __construct( $args = array() ) { // set our default arguments, parse against the provided arguments, and store for use throughout the class $this->_args = wp_parse_args( $args, array( 'form_id' => false, 'rowCount' => 1, 'list_field_id' => false ) ); // do version check in the init to make sure if GF is going to be loaded, it is already loaded add_action( 'init', array( $this, 'init' ) ); } public function init() { // make sure we're running the required minimum version of Gravity Forms if( ! property_exists( 'GFCommon', 'version' ) || ! version_compare( GFCommon::$version, '1.8', '>=' ) ) { return; } // time for hooks add_filter( 'gform_pre_render', array( $this, 'load_form_script' ), 10, 2 ); add_filter( 'gform_register_init_scripts', array( $this, 'add_init_script' ), 10, 2 ); } public function load_form_script( $form, $is_ajax_enabled ) { if( $this->is_applicable_form( $form ) && ! has_action( 'wp_footer', array( $this, 'output_script' ) ) ) { add_action( 'wp_footer', array( $this, 'output_script' ) ); add_action( 'gform_preview_footer', array( $this, 'output_script' ) ); } return $form; } public function output_script() { ?> <script type="text/javascript"> window.gwalfr; (function($){ gwalfr = function( args ) { this.formId = args.formId, this.listFieldId = args.listFieldId, this.rowCount = args.rowCount; this.init = function() { // update rows on page load this.updateListItems( this.rowCount, this.listFieldId, this.formId ); } this.updateListItems = function( count, listFieldId, formId ) { var listField = $( '#field_' + formId + '_' + listFieldId ); rowCount = listField.find( 'table.gfield_list tbody tr' ).length, diff = count - rowCount; if( diff > 0 ) { for( var i = 0; i < diff; i++ ) { listField.find( '.add_list_item:last' ).click(); } } else { // make sure we never delete all rows if( rowCount + diff == 0 ) diff++; for( var i = diff; i < 0; i++ ) { listField.find( '.delete_list_item:last' ).click(); } } } this.init(); } })(jQuery); </script> <?php } public function add_init_script( $form ) { if( ! $this->is_applicable_form( $form ) ) { return; } $args = array( 'formId' => $this->_args['form_id'], 'listFieldId' => $this->_args['list_field_id'], 'rowCount' => $this->_args['row_count'] ); $script = "new gwalfr(" . json_encode( $args ) . ");"; $key = implode( '_', $args ); GFFormDisplay::add_init_script( $form['id'], 'gwalfr_' . $key , GFFormDisplay::ON_PAGE_RENDER, $script ); } public function is_applicable_form( $form ) { $form_id = isset( $form['id'] ) ? $form['id'] : $form; return empty( $this->_args['form_id'] ) || $form_id == $this->_args['form_id']; } } # The Field new GWListFixedRows( array( 'form_id' => X, 'list_field_id' => XXX, 'row_count' => X ) ); /*** END GF List View Fixed Rows ***/

Copy Code to Clipboard Raw

Sign-in or Create a Free Account.

Snippi uses Twitter or Facebook to register and create accounts. Registration will enable you to keep track of your snippets. We never spam your timeline or wall. Ever.

Sign-in with Facebook Sign-in with Twitter