Force total values to update on row input keyup

This commit is contained in:
Takumi
2013-08-04 22:56:55 +10:00
parent 5ffb31e415
commit 7db8616cab
4 changed files with 18 additions and 39 deletions

View File

@@ -106,23 +106,7 @@ function sp_save_post( $post_id ) {
delete_post_meta( $post_id, 'sp_result'); delete_post_meta( $post_id, 'sp_result');
$stats = (array)sp_array_value( $_POST, 'sp_stats', array() ); $stats = (array)sp_array_value( $_POST, 'sp_stats', array() );
foreach ( $stats as $table ) { foreach ( $stats as $table ) {
// Add values from each row where total is blank
$total = sp_array_value( sp_array_value( $table, 0, null ), 0, null ); $total = sp_array_value( sp_array_value( $table, 0, null ), 0, null );
if ( $total == null ):
$values = array();
foreach ( $table as $row ):
$value = sp_array_value( $row, 0, null );
if ( is_numeric( $value ) ):
$values[] = $value;
endif;
endforeach;
if ( sizeof( $values ) ):
$total = array_sum( $values );
endif;
endif;
// Save first column of total row as result
add_post_meta( $post_id, 'sp_result', $total ); add_post_meta( $post_id, 'sp_result', $total );
} }
sp_update_post_meta_recursive( $post_id, 'sp_team', sp_array_value( $_POST, 'sp_team', array() ) ); sp_update_post_meta_recursive( $post_id, 'sp_team', sp_array_value( $_POST, 'sp_team', array() ) );

View File

@@ -384,14 +384,14 @@ if ( !function_exists( 'sp_stats_table' ) ) {
$i++; $i++;
endforeach; endforeach;
if ( $total ): if ( $total ):
$values = array_key_exists( 0, $stats ) ? $stats[0] : array(); $values = sp_array_value( $stats, 0, array() );
?> ?>
<tr class="sp-row sp-total<?php if ( $i % 2 == 0 ) echo ' alternate'; ?>"> <tr class="sp-row sp-total<?php if ( $i % 2 == 0 ) echo ' alternate'; ?>">
<td><strong><?php _e( 'Total', 'sportspress' ); ?></strong></td> <td><strong><?php _e( 'Total', 'sportspress' ); ?></strong></td>
<?php for ( $j = 0; $j < sizeof( $columns ) - 1; $j ++ ): <?php for ( $j = 0; $j < sizeof( $columns ) - 1; $j ++ ):
$value = sp_array_value( $values, $j, '' ); $value = sp_array_value( $values, $j, '' );
?> ?>
<td><input type="text" name="<?php echo $slug; ?>[<?php echo $index; ?>][0][]" value="<?php echo $value; ?>" /></td> <td><input type="text" name="<?php echo $slug; ?>[<?php echo $index; ?>][0][]" value="<?php echo $value; ?>" placeholder="0" /></td>
<?php endfor; ?> <?php endfor; ?>
</tr> </tr>
<?php endif; ?> <?php endif; ?>

View File

@@ -34,28 +34,23 @@ jQuery(document).ready(function($){
}); });
// Total stats calculator // Total stats calculator
$('.sp-stats-table').on('updateTotals', function() { $('.sp-stats-table .sp-total input').on('updateTotal', function() {
$self = $(this); index = $(this).parent().index();
$self.find('.sp-total input').each(function(i) {
var sum = 0; var sum = 0;
$self.find('.sp-post').each(function() { $(this).closest('.sp-stats-table').find('.sp-post').each(function() {
$el = $($(this).find('input')[i]); val = $(this).find('td').eq(index).find('input').val();
if($el.val() != '') { if($.isNumeric(val)) {
if($.isNumeric($el.val())) sum += parseInt($el.val(), 10); sum += parseInt(val, 10);
} else {
sum += parseInt($el.attr('placeholder'), 10);
} }
}); });
$(this).attr('placeholder', sum); $(this).val(sum);
});
}); });
// Activate total stats calculator // Activate total stats calculator
$('.sp-stats-table .sp-post input').on('keyup', function() { if($('.sp-stats-table .sp-total').size()) {
$(this).closest('.sp-stats-table').trigger('updateTotals'); $('.sp-stats-table .sp-post td input').on('keyup', function() {
$(this).closest('.sp-stats-table').find('.sp-total td').eq($(this).parent().index()).find('input').trigger('updateTotal');
}); });
}
// Trigger total stats calculator
$('.sp-stats-table').trigger('updateTotals');
}); });

View File

@@ -63,7 +63,7 @@ function sp_team_stats_meta( $post ) {
$placeholders[ $league_id ] = sp_get_stats_row( $args ); $placeholders[ $league_id ] = sp_get_stats_row( $args );
endforeach; endforeach;
sp_stats_table( $data, $placeholders, 0, array( 'League', 'P', 'W', 'D', 'L', 'F', 'A', 'GD', 'Pts' ), true, 'sp_league' ); sp_stats_table( $data, $placeholders, 0, array( 'League', 'P', 'W', 'D', 'L', 'F', 'A', 'GD', 'Pts' ), false, 'sp_league' );
sp_nonce(); sp_nonce();
} }
?> ?>