Friday, 24 October 2014

How to get the number of rows in a table - tbodyin jquery?

<table id="myTable" cellpadding="0" cellspacing="0">
  <thead>
    <tr>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Birth Date</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>John</td>
      <td>Smith</td>
      <td>03-11-1980</td>
    </tr>
  </tbody>
</table>

Solution:
$("#myTable tbody tr").length - Its wrong.
$("#myTable > tbody > tr").length; - Its right to get solution.

Thursday, 16 October 2014

How To Get a Multiple Rows From JSON Object and Append to the Table

$("#table-list tr").detach();
    $.get(url+"payroll/payroll-ajax.php","id="+arg+"&param=view_loan",function(data){
    var objJSON = eval("(function(){return " + data + ";})()");
    var i=objJSON.length;
    for (var j=0;j<i;j++) {
        k=j+1;
        var newRow =
        "<tr>"
        +"<td>"+k+"</td>"
        +"<td>"+objJSON[j].deduction_date+"</td>"
        +"<td>&#2352;  "+objJSON[j].deduction_amt+"</td>"
        +"</tr>" ;
        $(newRow).appendTo("#table-list tbody");
    }
    $('#view-loan-table-modal').modal('toggle');
    var objJSON="";
    });

This above code is used to get multiple rows from json object.

If the variable data is contains following values.

data=[{"id":"3","loan_id":"8","deduction_date":"2014-07-16 12:54:14","deduction_amt":"16667"},{"id":"9","loan_id":"8","deduction_date":"2014-08-12 10:25:36","deduction_amt":"16667"},{"id":"10","loan_id":"8","deduction_date":"2014-08-12 02:22:53","deduction_amt":"16667"}]

This is JSON object value.

The out put is: