重力表单:使用以前的下拉选择作为Gform_Pre_Render中的变量?

时间:2013-01-28 作者:Joshc

我有个问题gform_pre_render?

我有经销商表格。基本上你可以选择你的郡,然后选择你的经销商。

Dropdown A = Dealer Country
Dropdown B = Dealer Name

我有大约15个国家/地区,我正在使用gform\\u pre\\u render函数中的get\\u术语列出我分配给经销商帖子的所有国家/地区。。。

// Dropdown A - Dealer Country
add_filter("gform_pre_render", "dropdown_dealer_country");
add_filter("gform_admin_pre_render", "dropdown_dealer_country");
function dropdown_dealer_country($form){

    if($form["id"] != 3)
       return $form;

    $terms = get_terms("dealer-country");

    $items = array();
    $items[] = array( "text" => __(\'Select country...\',\'mission-theme\'), "value" => 0 );

    foreach($terms as $term)
        $items[] = array( "text" => $term->name, "value" => $term->slug );

    foreach($form["fields"] as &$field)
        if($field["id"] == 6){
            $field["choices"] = $items;
        }

    return $form;
}
好的,那么上面的函数在下拉菜单A中工作得很好。

现在我想在下拉列表B中显示我所有的经销商名称。

我已经用经销商的名称为每个经销商自定义帖子类型标题命名,这是我在第二个下拉列表中预先填充的内容。。。

// Dropdown B - Dealer Name
add_filter("gform_pre_render", "dropdown_dealer_name");
add_filter("gform_admin_pre_render", "dropdown_dealer_name");
function dropdown_dealer_name($form){

    if($form["id"] != 3)
       return $form;

    $dealers = get_posts(array(
        "post_type" => "dealer",
        "dealer-country" => $dealerCounty,
        "post_status" => "publish",
        "orderby" => "title",
        "order" => "ASC",
        "posts_per_page"  => -1
    ));

    $items = array();
    $items[] = array( "text" => __(\'Select dealer...\',\'mission-theme\'), "value" => 0 );

    foreach($dealers as $dealer)
        $items[] = array( "text" => $dealer->post_title, "value" => $dealer->post_name );

    foreach($form["fields"] as &$field)
        if($field["id"] == 7){
            $field["choices"] = $items;
        }

    return $form;
}
。。。如第11行所示,我的get\\u posts数组中有一个变量"dealer-country" => $dealerCounty

我的问题是,在Dropdown A 在我的Dropdown B 作用

如有任何提示或帮助,我将不胜感激Dropdown B 目前很长,我需要按国家筛选。


如果有人知道一种方法那就太好了。提前谢谢。

2 个回复
最合适的回答,由SO网友:Joshc 整理而成

最终,我使用的解决方案是这样的。更改下拉列表A后,我有一个ajax函数请求,它使用基于下拉列表A中的选择的过滤选项重新填充下拉列表B。

请参阅ajax jquery脚本。。。

countryFilter = function () {

    var countryClass = \'.dealer-country select\',
        dealerClass  = \'.dealer-name select\';

    $(countryClass).change(function(){

        var countrySelect = $(this),
            country = countrySelect.val(),
            dealerSelect = countrySelect.parents(\'form\').find(dealerClass);

        if(country != "default") {

            $.ajax({
                type: \'POST\',
                url: \'<?php echo admin_url(\'admin-ajax.php\'); ?>\',
                data: { dealerCountry : country, action: \'get_dealer_name\' },
                success: function(data){
                    dealerSelect.empty();
                    var options = $.parseJSON(data);
                    for(i=0;i<options.length;i++){
                        dealerSelect.append(\'<option value="\'+options[i].value+\'">\'+options[i].text+\'</option>\');
                    }
                    dealerSelect.removeAttr(\'disabled\');
                }
            });

        }

    });

}
我用。。。

$(document).ready(function () { 

    countryFilter();

});

$(document).bind(\'gform_post_render\', function(event, form_id){

    if(form_id == 3) {

        countryFilter();

    }

});
然后我将原来的函数修剪为。。。

    // Dropdown A - Dealer Country
add_filter("gform_pre_render", "dropdown_dealer_country");
add_filter("gform_admin_pre_render", "dropdown_dealer_country");
function dropdown_dealer_country($form){
        if($form["id"] != 3)
           return $form;
        $terms = get_terms("dealer-country");
        $items = array();
        $items[] = array( "text" => __(\'Select country...\',\'theme\'), "value" => \'default\' );
        foreach($terms as $term)
            $items[] = array( "text" => $term->name, "value" => $term->slug );
        foreach($form["fields"] as &$field){
            if($field["id"] == 6 ){
                $field["cssClass"] = \'dealer-country\';
                $field["choices"] = $items;
            }  
        }
        return $form;  
    }

    // Dropdown B - Dealer Name
add_filter("gform_pre_render", "dropdown_dealer_name");
add_filter("gform_admin_pre_render", "dropdown_dealer_name");
function dropdown_dealer_name($form){
        if($form["id"] != 3)
           return $form;
        $items = array();
        $items[] = array( "text" => __(\'Select dealer...\',\'theme\'), "value" => \'default\' );
        foreach($form["fields"] as &$field){
            if($field["id"] == 7){
                $field["cssClass"] = \'dealer-name\';
                $field["choices"] = $items;
            }
        }
        return $form; 
    }
最后的点睛之笔是功能,它也包含在功能中。php-这是由ajax请求调用的。。。

function get_dealer_name_fn(){
    $dealerCountry = $_POST[\'dealerCountry\'];
    $dealers = get_posts(array(
        "post_type" => "dealer",
        "dealer-country" => $dealerCountry,
        "post_status" => "publish",
        "orderby" => "title",
        "order" => "ASC",
        "posts_per_page"  => -1
    ));
    $items = array();
    $items[] = array( "text" => __(\'Select dealer...\',\'theme\'), "value" => \'default\' );
    foreach($dealers as $dealer){
        $items[] = array( "text" => $dealer->post_title, "value" => $dealer->post_title );
    }
    echo json_encode($items);
    die;
}
add_action(\'wp_ajax_get_dealer_name\', \'get_dealer_name_fn\');
add_action(\'wp_ajax_nopriv_get_dealer_name\', \'get_dealer_name_fn\');

SO网友:webaware

一种简单的方法(无论是否使用AJAX或JavaScript)是在一个页面上显示dealer county下拉列表,在下一个页面上显示dealers。然后您可以将其放入您的函数中:

foreach ($form[\'fields\'] as $field) {
    if ($field[\'id\'] == 6) {
        // found dealer county, get value and exit loop
        $dealerCounty = rgpost(\'input_\' . $field[\'id\']);
        break;
    }
}

结束

相关推荐