0

I am using laravel 5 and I want to show field in jenis_surat column in database to my table using datatables. I want to using JSON, do you know how to show the number depends on the entries of the data.
here is my controller (It's just shows data in jenis_surat):

public function jenissurat()
{return view('jenissurat');}

  public function datajenissurat()
{
    $jenissurat = new JenisSurat();
    $jenissurats = $jenissurat->select('jenis_surat')->get();   
    return $jenissurats;
}

And how to display it on datatables? example:

No. Jenis Surat. .Action
1. . .A . . . . . . . . . Edit | Delete
2. . .B . . . . . . . . . Edit | Delete
3. . .F . . . . . . . . . Edit | Delete

<table id="jenissurat" class="table table-bordered table-striped" cellspacing="0" widht="100%">
          <thead>
            <tr>
              <th width="4%">No.</th>
              <th width="80%">Jenis Surat</th>
              <th width="16%">Action</th>
            </tr>
          </thead>
        </table>
        <script type="text/javascript">

          $(document).ready(function(){
            $('#publish').DataTable({
              'columns' : [
                {'data' : 'jenis_surat'}
              ]
            });
          });

        </script>

Here is my route:

Route::get('/jenissurat', [
'uses' => 'SuratController@jenissurat',
'as' => 'jenissurat'
]); Route::get('/datajenissurat', [
'uses' => 'SuratController@datajenissurat',
'as' => 'datajenissurat'
]);here

2 Answers 2

1

Try this, I am not sure this is a best solution from the performance point of view.

public function jenissurat()
 {
    $returnValue = $this->datajenissurat();
    return view('jenissurat', compact('returnValue'));
 }

 public function datajenissurat()
 {
    $jenissurat = new JenisSurat();
    DB::statement(DB::raw('set @rownum=0'));

    $jenissurats = $jenissurat->select([DB::raw('@rownum  := @rownum  + 1 AS rownum'), 'jenis_surat'])->get();   
    return $jenissurats;
 }

And regarding to the Datatables you can add something like this.

  <script>

    $(function() {

       var oTable = $('#publish').DataTable({
           bProcessing    : true,
           serverSide     : true,
           /* sDom           : 'p', */
           dom            : 'Bfrtip',
           ajax: {
           url: '{!! route("datajenissurat") !!}',
           data: function (d) {

           }
        },        
      columns: [
        { data: 'rownum', name: 'rownum' ,orderable: false, searchable:  false },
        { data: 'jenis_surat', name: 'jenis_surat' },
        { data: 'action', name: 'action', orderable: false, searchable:  false }
        ]
     });
   });
  </script>

I see a edit/delete column that can be used by the action column. So, the ajax call to your controller function datajenissurat() will return a json array, which can be parsed using the datatable script.

Sign up to request clarification or add additional context in comments.

16 Comments

I've got an error "Class 'App\Http\Controllers\DB' not found"
add 'Use DB; ` at top of your controller OR use \DB in your query.
yeah it works, thanks. And do you know how to display the json data on the datatables?
Can you show the code you have done so far, with integrating datatables
<table id="publish" class="table table-bordered table-striped" cellspacing="0" widht="100%"> <thead> <tr> <th>No.</th> <th>Publish</th> <th>Action</th> </tr> </thead> </table> <script type="text/javascript"> $(document).ready(function(){ $('#publish').DataTable({ 'columns' : [ {'data' : 'publish'} ] }); }); </script>
|
0
public function datajenissurat()
{
    $jenissurats = $jenissurat->select('jenis_surat')->get();   
    return view('view.show', compact('jenissurats '));
}

and also make view

1 Comment

it's not working. I'm type the link for show the json but got an error. My function was already working but I want to show the number column no, which is not in database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.