locked
Waiting for functions to complete RRS feed

  • Question

  • User-1826049516 posted

    Hey,

    I'm doing this:

    var rowCount;
    var opt = action === "delete" ? "restore" : "delete";
    
    $( "table.data" ).each( function( event ) {
    
    var table = $( this );
    
    $( "input:checkbox:checked" ).each( function( event ) {
    
    	target = $( this );
    	var row = target.closest( "tr, li" );
    	var tag = row.prop( "tagName" );
    	row.fadeOut( "slow", function( event ) {
    
    		switch( tag.toLowerCase() ) {
    
    			case "tr":
    
    				var container = $( this ).find( "div." + action );
    				container.find( "a" ).attr( "title", opt + " this link" );
    				container.find( "a > img" ).attr( "src", "/images/" + opt + "-no.png" );
    				container.find( "a > img" ).attr( "alt", opt );
    				container.find( "input:first" ).attr( "name", opt );
    				container.find( "input:last" ).val( "/images/" + opt + "-no.png" );
    				container.removeClass( action ).addClass( opt );
    				switch( action ) {
    
    					case "delete":
    
    						$( this ).addClass( "delete" );
    						break;
    
    					case "restore":
    
    						$( this ).removeClass( "delete" );
    						row.fadeIn( "fast" );
    						break;
    
    				}
    				break;
    
    			case "li":
    
    				$( this ).remove();
    				break;
    
    		}
    
    	} );
    
    } );
    
    rowCount = table.find( "tbody > tr:visible" ).length;
    table.closest( "div.content" ).prev( "div.header" ).find( "h2 > span" ).html( rowCount );
    

    No matter where I put the last 2 lines, they're firing before the row is hidden so the rowCount never changes.

    Monday, November 23, 2015 6:27 PM

Answers

  • User281315223 posted

    You could add a check within your loop to determine if the current element was the last one and then execute your code :

    $( "input:checkbox:checked" ).each( function( event ) {
    	target = $( this );
    	// Omitted for brevity
            if(target.is("input:checkbox:checked:last")){
                rowCount = table.find( "tbody > tr:visible" ).length;
                table.closest( "div.content" ).prev( "div.header" ).find( "h2 > span" ).html( rowCount );
            }
    } );
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 23, 2015 6:35 PM

All replies

  • User281315223 posted

    You could add a check within your loop to determine if the current element was the last one and then execute your code :

    $( "input:checkbox:checked" ).each( function( event ) {
    	target = $( this );
    	// Omitted for brevity
            if(target.is("input:checkbox:checked:last")){
                rowCount = table.find( "tbody > tr:visible" ).length;
                table.closest( "div.content" ).prev( "div.header" ).find( "h2 > span" ).html( rowCount );
            }
    } );
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 23, 2015 6:35 PM
  • User61956409 posted

    Hi ldoodle,

    Please share us your html and CSS code, then we will reproduce your problem based on your code.

    Best Regards,

    Fei Han

    Tuesday, November 24, 2015 5:45 AM