重力表格列表的动态下拉列表

时间:2017-05-03 12:43:54

标签: wordpress gravity-forms-plugin

我想让Gravity Forms中的一个列表字段成为动态下拉列表。我有一个代码,使其中一个字段成为下拉列表和代码,使下拉动态。有人可以帮我合并两段代码吗?

以下是动态下拉列表的代码

add_filter( 'gform_pre_render_17', populate_dropdown_operations_global_tag1 );
add_filter( 'gform_admin_pre_render_17', 
populate_dropdown_operations_global_tag1 );

function populate_dropdown_operations_global_tag1( $form ) {

global $wpdb;
global $loc_id;

$results = $wpdb->get_results( "SELECT name FROM custom_tag WHERE loc_id=$loc_id AND category='global_tag1' AND status='active' ORDER BY name ASC" );
$category = $wpdb->get_row( "SELECT name FROM custom_category WHERE loc_id=$loc_id AND category='global_tag1'" );

$choices = array();
$choices[] = array( "text" => "Select ".$category->name, "value" => " " );

foreach( $results as $result ) :
    $choices[] = array( "text" => $result->name, "value" => $result->name );
endforeach;

foreach( $form["fields"] as &$field ) :
    if( $field["id"] == '36' ) :
        $field["choices"] = $choices;
    endif;
endforeach;

return $form;
}

以下是列表中下拉列表的代码

add_filter( 'gform_column_input_17_36_1', 'set_column1', 10, 5 );
function set_column1( $input_info, $field, $column, $value, $form_id ) {
 return array( 'type' => 'select', 'choices' => 'NEED TO ADD CHOICES HERE' );
}

谢谢!

1 个答案:

答案 0 :(得分:0)

根据您的评论,我将向您展示如何使用数据库中的选项填充下拉列表:

           add_filter("gform_pre_render_17", "example_pre_render", 10 , 3);

            function example_pre_render($form, $ajax, $field_values) {
              global $wpdb;
              global $loc_id;
              $results = $wpdb->get_results( "SELECT name FROM custom_tag WHERE loc_id=$loc_id AND category='global_tag1' AND status='active' ORDER BY name ASC" );
              $category = $wpdb->get_row( "SELECT name FROM custom_category WHERE loc_id=$loc_id AND category='global_tag1'" );

              $choices = array();
              $choices[] = array( 
                                "text" => "Select ".$category->name, 
                                "value" => " ",
                                "isSelected" => true,
                                "price" => ""
                          );

              foreach( $results as $result ) {
                  $choices[] = array( 
                                    "text" => $result->name, 
                                    "value" => $result->name,
                                    "isSelected" => false,
                                    "price" => ""  
                                );
              }
             $form["fields"][36]->choices = $choices

             return $form;
            }

如果没有填充下拉列表,我会调试代码,看看$ results是否有值以及$ form对象返回时的样子。

看起来应该是这样的:

Debug Example

我知道这是一个复选框,但它的工作方式几乎相同。