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');
$stats = (array)sp_array_value( $_POST, 'sp_stats', array() );
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 );
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 );
}
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++;
endforeach;
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'; ?>">
<td><strong><?php _e( 'Total', 'sportspress' ); ?></strong></td>
<?php for ( $j = 0; $j < sizeof( $columns ) - 1; $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; ?>
</tr>
<?php endif; ?>

View File

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