Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i have a form with table like this:

<table id="preview">
<tbody>
<tr>
<td>01/01/2010</td>
<td>Credit</td>
<td>1000</td>
</tr>
<tr>
<td>01/05/2010</td>
<td>Debit</td>
<td>200</td>
</tr>
<tr>
<td>01/09/2010</td>
<td>Debit</td>
<td>400</td>
</tr>
<tr>
<td>01/11/2010</td>
<td>Debit</td>
<td>100</td>
</tr>
</tbody>
</table>

Now I need to create an array to send (ajax/php) like this:

$ajaxArray = array(   0 => array(From   => "01/01/2010",  //1st td of the 1st Row
                                    To     => "01/05/2010",  //1st td of the 2nd Row
                                    Type   => "Credit",
                                    Amount => 1000.00),
                         1 => array(From   => "01/05/2010",  //1st td of the 2nd Row
                                    To     => "01/09/2010",  //1st td of the 3th Row
                                    Type   => "Debit",
                                    Amount => 200.00),
                         2 => array(From   => "01/09/2010",  //1st td of the 3th Row
                                    To     => "01/11/2010",  //1st td of the 4th Row
                                    Type   => "Debit",
                                    Amount => 400.00),
                         3 => array(From   => "01/11/2010",  //1st td of the 4th Row
                                    To     => "01/01/2012",  //Value in $last_date var
                                    Type   => "Debit",
                                    Amount => 100.00)
        );

I tried with this code:

 $('#preview > tbody  > tr').each(function() {
            var from = $('td:eq(0) ', this).text();
            var type = $('td:eq(1) ', this).text();
            var amount = $('td:eq(2) ', this).text();
            ajaxArray.push({
                From: from,
                Type: type,
                Amount: amount
            });
        });

As you can see i can't get the "To" date value. The "To" date value is the date contained in the 1st TD of the Next row except for the last Row, where this value is in the $last_date variable.

Thanks in advance

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

This should do it Fiddle Demo

var array = [];
var rows  = $("#preview tbody tr");

$.each( rows, function(index, row) {   
    var columns = $(row).find("td");

    array[index] = {};             
    array[index].from = columns[0].innerHTML;    
    array[index].type = columns[1].innerHTML;
    array[index].amount = columns[2].innerHTML;

    if(index > 0){
        array[index - 1].to = columns[0].innerHTML;
    }      
});

$("#result").text(JSON.stringify(array));
share|improve this answer
    
Thanks so much. this code work perfectly for each row except for the last array. How to get the last To date from the $last_date variable? –  fasenderos Apr 20 at 14:54
    
That I don't know, no experience in php sorry. –  Hatsjoem Apr 20 at 14:56
    
Ok no problem. I'm able ti solve by adding an hidden row with the last "To" date. –  fasenderos Apr 20 at 15:03
1  
this can be written much more elegantly with a map: jsfiddle.net/6q8cS/2 –  kritzikratzi Apr 20 at 15:08
    
@kritzikrarzi.. you're right, it's much more elegant ;) Thanks –  fasenderos Apr 20 at 15:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.