根据其他选择字段(ACF)的选择更新转发器中的选择字段 [英] Update select field in repeater based upon the choice of other select field (ACF)

查看:89
本文介绍了根据其他选择字段(ACF)的选择更新转发器中的选择字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经寻找了一段时间,但我找不到真正的解决方案。我已经读了很多文章以及关于动态填充选择字段的所有文章,但是实现它们并不是我真正想要的。



我有页面上的转发器字段,用户可以在其中选择多个行为(从行为CPT中选择)。



在这些行为中,有一个转发器字段,在其中可以添加行为将要执行的不同时间。



我的选择行为中继器就是这样。




  • 选择行为(选择字段,显示行为cpt的所有帖子)

  • 选择时间(选择字段,需要在选择行为更改时从所选行为帖子中加载自定义元数据)。



我要寻找的是,当用户选择一个动作时,我希望选择时间选择字段加载时间该用户添加在该法案帖子中。



因此,假设我有《 A法》。我在帖子中添加了四次。 21.00、22.00、23.00和24.00。



我也有B法:社会扭曲。我在帖子中添加了三遍。 15.00、16.00和17.00。



当我选择 Bodom的孩子行为时,我想要的是我的中继器中的内容,我希望该中继器部件的选择时间随着我添加的时间而更新博多之子法案。



然后在行为中继器中添加另一个行为,在此选择社交扭曲。然后,我希望用我添加到社会扭曲法中的时间来更新各个时间。



到目前为止,我一直在尝试添加一些JS来执行select更改,我不知道具体触发他各自的时间来获取更新字段,因为中继器是动态设置的,而我没有为该字段选择ID或密钥。



什么很酷,但是我不知道如何实现,是触发 acf / load_field / key = blabla 函数从我的JS文件中获得,在这里我可以通过在触发更改事件的选择之后找到第一个选择来查找右键。然后,在触发该加载字段动作时执行一个函数,并在我选择的相应时间中加载正确的时间。



抱歉,这有点令人困惑,我真的不知道如何用不同的方式来描述它,但是如果您想了解更多信息,请询问!



谢谢。

  jQuery(' act_select_field')。change(function(){
jQuery.ajax({
url: /wp-admin/admin-ajax.php,
类型:'POST',
数据:{
操作:'get_related_act_times',
act_id://通过操作ID值发送,
},
dataType:'html',
success:function(result){
//使用html填充时间选择字段
},
错误:function(errorThrown){
console.log(errorThrown);
}
});
})

然后,在您的functions.php,注册一个函数来处理此调用,如下所示:

  add_action('wp_ajax_get_related_act_times','get_act_times') ; 
add_action('wp_ajax_nopriv_get_related_act_times','get_act_times');
/ **
*从清除预订中获取事件列表
* /
function get_act_times(){
$ act_id = $ _POST [’act_id’];
$ related_times = get_field(’times_repeater_field’,$ act_id);
foreach($ realted_times as $ time){
//在此处建立您的选择html
}
echo select_html
die();
}

如果您想进一步了解Admin Ajax方面,法典有很多信息: https://codex.wordpress.org/AJAX_in_Plugins 。希望这可以使您开始动态填充第二个下拉菜单-尽管您需要调整代码才能在自己的系统上工作


I've been looking for this a while and I can't really find a solution. I've read plenty of articles and all about dynamically populating select fields, but the way they're implemented isn't really what I'm looking for.

I have a repeater field on a page where the user can select multiple acts (from the Act CPT).

In these acts there is a repeater field in which they can add the different times the act is going to perform.

My 'choose acts' repeater is something like this.

  • Choose Act (select field, shows all the posts from the act cpt)
  • Choose time (select field, needs to load custom meta data from the chosen act post on the 'choose act' change).

What I'm looking for is, when the user chooses an act I want the 'Choose Time' select field to load the times the user added in the Act post.

So let's say I have Act A. Children of Bodom. I added four times to the post. 21.00, 22.00, 23.00 and 24.00.

I also have Act B. Social Distortion. I added three times to the post. 15.00, 16.00 and 17.00.

What I want is in my repeater when I select the act Children of Bodom, I want the time select for that repeater part to be updated with the times I've added to the Children of Bodom act.

Then I add another act in the acts repeater, where I select Social Distortion. I then want the respective time select to be updated with the times I've added to the Social Distortion act.

What I've tried so far is to add some JS to execute on a select change, I don't know to specifically trigger his respective time select to get updated with the fields, as the repeater is dynamically set up and I've not choosen an ID or key for that field.

What would be cool, but I don't know how to implement this, is to trigger the acf/load_field/key=blabla function from my JS file, where I can look up the right key by finding the first select after the one that triggered the change event. Then execute a function when that load field action gets triggered and load up the correct times in my time select for that respective act.

Sorry if this is a bit confusing, I don't really know how to describe this differently, but if you want more info, please ask!

Thanks.

解决方案

I feel that in this case, because what you need is dynamic, you could look into using Wordpress's built in Ajax to handle this one. So, what you could do is to trigger an ajax call on change for that select field to do something like this

jQuery('act_select_field').change(function(){
    jQuery.ajax({
        url: "/wp-admin/admin-ajax.php",
        type: 'POST',
        data: {
            action: 'get_related_act_times',
            act_id: //send through the act ID value,            
        },
        dataType: 'html',
        success: function (result) {
            //Use the html to populate your time select field
        },
        error: function (errorThrown) {
            console.log(errorThrown);
        }
    });
})

Then, in your functions.php, register a function to handle this call, like this:

add_action('wp_ajax_get_related_act_times', 'get_act_times');
add_action('wp_ajax_nopriv_get_related_act_times', 'get_act_times');
/**
 * Get a list of events from Clear Bookings
 */
function get_act_times(){
    $act_id = $_POST['act_id'];
    $related_times = get_field('times_repeater_field',$act_id);
    foreach($realted_times as $time){
        //Build up your select html here
    }
    echo select_html
    die();
}

If you want to read a bit more about the Admin Ajax side of things, the Codex has a lot of information: https://codex.wordpress.org/AJAX_in_Plugins. This should hopefully get you started on dynamically populating your second dropdown - although you'll need to tweak the code to work on your own system

这篇关于根据其他选择字段(ACF)的选择更新转发器中的选择字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆