Support

Account

Forum Replies Created

  • 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');
    
  • I use the Code Snippets plugin to add it to the functions.php file

  • unfortunately its still not working. I have tested with different fields just to be sure but it doesn’t work

  • Thanks! This is my filed key “field_5ca90e913bb43” Can you show me how do I add it?

  • I tried this with no difference

    function my_acf_update_totalexpenses($post_id)
    {
    
       $total = get_sub_field('list'); $totaltasks = count($tasks);
    	$totaltaskpercentage = 0;
    		while ( have_rows('list') ) : the_row(); 
    			$totaltaskpercentage += intval( get_sub_field('expense_'));
    		endwhile; 
    	$grouptaskpercentage = $totaltaskpercentage / $totaltasks;
    	echo round($grouptaskpercentage);
    	$value = $total;
        $field_name = "_totalexpenses";
        update_field($field_name, $value, $post_id);
    }
    add_action('acf/save_post', 'my_acf_update_totalexpenses', 20);
  • Thanks for the answer, but the change didn’t work.

Viewing 6 posts - 1 through 6 (of 6 total)