www.webdeveloper.com
Results 1 to 2 of 2

Thread: New rows in tabular form won't decrement

  1. #1
    Join Date
    Jul 2014
    Posts
    1

    New rows in tabular form won't decrement

    The following code runs on a page that gives you the ability to add and delete rows from a table of input fields. It works for existing fields, but when I add new rows and try to delete them in an order that requires replacing the ID and name attributes it doesnt work - it will not replace the ID and name attributes. Any ideas what's going on?

    The following function is supposed to decrement the remaining rows after the current row that is going to be deleted has been passed in the loop, and replace the number in the ID and name attributes. Then delete the row.

    Code:
    $("#tblData").delegate("button", "click", function()
           {
    
               var id1 = $(this).closest('tr').find('td input').attr('id');
    
    
               id1 = parseInt(id1.match(/\d+/));
    
    
             var count = 0;
             var ID;
             var name;
             var str;
             var str2;
             var n1, n2;
                $('#tblData > tbody > tr').each(function() {
                  $(this).find('td').each(function(){
                        //do your stuff, you can use $(this) to get current cell
    
                        if ($(this).children().children().next().prop("tagName") == "INPUT")
                        {                            
                            if (count > id1){
                                // get id and name attributes
                                ID = $(this).children().children().next().attr("id");
                                name = $(this).children().children().next().attr("name");
                                // match number and replace number in ID
                                str=ID;
                                n1=parseInt(str.match(/\d+/));
                                n1 = n1-1;           
                                ID = ID.replace(/\d+/,n1);
    
    
                                // match number and replace number in name
                                str2=name;
                                n2=parseInt(str2.match(/\d+/));
                                n2 = n2-1;    
                                name = name.replace(/\d+/,n2);
    
    
                                $(this).children().children().next().attr("id",ID);
                                $(this).children().children().next().attr("name",name);
                            }                        
                        } else if ($(this).children().children().next().prop("tagName") == "SELECT") {
    
                            if (count > id1){
                                ID = $(this).children().children().next().attr("id");
                                name = $(this).children().children().next().attr("name");
                                // match number and replace number in ID
                                str=ID;
                                n1=parseInt(str.match(/\d+/));
                                n1 = n1-1;            
                                ID = ID.replace(/\d+/,n1);           
    
                                // match number and replace number in name
                                str2=name;
                                n2=parseInt(str2.match(/\d+/));
                                n2 = n2-1;      
                                name = name.replace(/\d+/,n2);
    
                                $(this).children().children().next().attr("id",ID);
                                $(this).children().children().next().attr("name",name);
                            }          
                        }
                    });                                     
                    count = count + 1;
    
               });  
    
            $(this).closest('tr').remove();
           }
        );
    Append row function

    Code:
    function Add(){
    
    var id = $( "#tblData tbody tr:last-child td input" ).attr('id');
    
    id = parseInt(id.substring(12, 13)) + 1;
    
    //alert(id);
        $("#tblData tbody").append(
                "<tr>"+
                "<td><label for='BookingRoom_Room_No'>Room  No</label><select single='single' size='1' name='BookingRoom["+id+"][roomId]' id='BookingRoom_"+id+"_roomId'><option value='1'>1</option><option value='2'>2</option></select></td>"+
                "<td><label for='BookingRoom_startDate' class='required'>Start Date <span class='required'>*</span></label><input name='BookingRoom["+id+"][startDate]' id='BookingRoom_"+id+"_startDate' type='text' /></td>"+
                "<td><label for='BookingRoom_endDate' class='required'>End Date <span class='required'>*</span></label><input name='BookingRoom["+id+"][endDate]' id='BookingRoom_"+id+"_endDate' type='text'  /></td>"+
                "<td><label for='BookingRoom_adults' class='required'>Adults <span class='required'>*</span></label><select single='single' size='5' name='BookingRoom["+id+"][adults]' id='BookingRoom_"+id+"_adults'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option></select></td>"+
                "<td><label for='BookingRoom_children' class='required'>Children <span class='required'>*</span></label><select single='single' size='5' name='BookingRoom["+id+"][children]' id='BookingRoom_"+id+"_children'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option></select></td>"+
                "<td><button type='button'>Delete</button></td>"+
                "</tr>");
    
    };

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,348
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles