Home › Forums › Add-ons › Repeater Field › Save sum of repeater filed into another filed › Reply To: Save sum of repeater filed into another filed
This code solved my need. The repeater filed is “expense” and the filed to which the total sum was saved is “total_Expenseslist”
function my_acf_input_admin_footer() {
echo "<!--";
$posts = get_posts(array(
'post_type' => 'time_sheet',
'post_status' => 'publish',
'numberposts' => -1
));
$records = Array();
foreach ($posts as $post) {
$id = $post->ID;
$userdata = get_user_meta($post->post_author);
var_dump($userdata);
}
echo "-->";
?>
<script type="text/javascript">
(function($) {
function sumExpenses() {
var $fields = acf.findFields({
name: 'expense'
});
totalExpenses = 0;
$fields.each(function(dummy, field) {
var v = parseFloat($(field).find('input').val().replace(/[\$\,\s]/g, ""));
if (!isNaN(v)) {
totalExpenses += v;
}
});
var $total = acf.findFields({
name: 'total_Expenseslist'
});
$total.each(function(dummy, t) {
$(t).find('input').val(totalExpenses);
});
}
acf.addAction('ready_field/name=expense_', function(field) {
field.$input().on('change', sumExpenses);
});
acf.addAction('append_field/name=expense_', function(field) {
field.$input().on('change', sumExpenses);
});
// Make total expenses read-only
acf.addAction('ready_field/name=total_Expenseslist', function(field) {
field.$input().prop('readonly', true);
});
})(jQuery);
</script>
<?php
}
add_action('acf/input/admin_footer', 'my_acf_input_admin_footer');
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.