praveenkumar N
praveenkumar N

Reputation: 19

How to get all data from html table to controller

I have a form with multiple html table in my view. How do I get all data from the html table to my controller by request. The following code is my view.

<table id="mastertree-table" class="table table-bordred table-striped">
    <thead>
    <th style="">Id</th>
    <th>Introducer Id</th>
    <th>Reference ID</th>
    <th>Amount</th>
    <th>Calculation</th>
    <th style="text-align: right;">Percentage</th>
    <th style="text-align: right;">Value</th>
    </thead>
    <tbody>
    @foreach($TreePayroll as $row)
        {!! Form::open(array('method'=>'POST', 'route' => array('payroll.update',$approveMasterPayroll[0]->EmpId, $approveMasterPayroll[0]->FYId), 'class' => 'form-horizontal', 'id' => 'payroll-form')) !!}
        {{ csrf_field() }}
        <tr>
            <td style="width: 80px; height: 5px">{!! Form::text('PHId',$row->PHId, ['class'=>'form-control', 'readonly', 'style'=>'text-align:right;']) !!}</td>
            <td>{{ $row->PayHead }}</td>
            <td>{{ $row->PayHeadType }}</td>
            <td>{{ $row->Computation }}</td>
            <td>{{ $row->Calculation }}</td>
            <td style="width: 100px; height: 5px">{!! Form::text('Rate',$row->Rate, ['class'=>'form-control', 'readonly', 'style'=>'text-align:right;']) !!}</td>
            @if($row->CalcId == 2)
                <td style="width: 150px; height: 5px">{!! Form::text('Value',$row->Value, ['class'=>'form-control', 'style'=>'text-align:right;']) !!}</td>
            @else
                <td style="width: 150px; height: 5px">{!! Form::text('Value',$row->Value, ['class'=>'form-control', 'readonly', 'style'=>'text-align:right;']) !!}</td>
            @endif
        </tr>
        {!! Form::close() !!}
    @endforeach
    </tbody>
</table>

Upvotes: 1

Views: 316

Answers (1)

Suraj15689
Suraj15689

Reputation: 29

You can use jquery and ajax to post your table values to controller...like below

var TableData;
TableData = saveTblValues()
TableData = $.toJSON(TableData);

function saveTblValues()
{
  var TableData = new Array();

  $('#your_table_id tr').each(function(row, tr){
     TableData[row]=
   {
        "column1_name" : $(tr).find('td:eq(0)').text() //for first column value
        , "column2_name" :$(tr).find('td:eq(1)').text()  //for second column value
        , "column3_name" : $(tr).find('td:eq(2)').text() //for third column value
        , "column4_name" : $(tr).find('td:eq(3)').text() // for fourth column value
    }    
}); 
 TableData.shift();  // first row will be empty - so remove
 return TableData;
}



$.ajax({
type: "POST",
url: "your route URL",
data: "TableData=" + TableData,
success: function(data){
    // return value 
   }
});

Upvotes: 0

Related Questions