skip to Main Content

In this my case in yajara table return row column. i add one extra column if condition true . can i make it?

Here my code

`return DataTables::of($data)
    if(Here my condition statment){
        ->addColumn('show_date', function ($row) {
            return '<span class="w-full">' . $row->name . '</span>';
        })
    }
                ->addColumn('show_date', function ($row) {
                    return '<span class="w-full">' . $row->show_date . '</span>';
                })
                ->addColumn('action', function ($row) {
                    $viewUrl = route('', ['date' => $row->show_date]);
                    $buttons = '';

                    return $buttons;
                })
                if(){
                   ->rawColumns(['show_date', 'action'])
                }else{
                    ->rawColumns(['name','show_date', 'action'])
                }`your text`
                
                ->make(true);`

In above can , in yajara table return statement if condition can not work on addcolumn , What alternative way to implement my code.
Please support me , Thank you in advance.

2

Answers


  1. You can use a variable

    $result = DataTables::of($data);
    if(Here my condition statment){
        $result = $result->addColumn('show_date', function ($row) {
            return '<span class="w-full">' . $row->name . '</span>';
        })
    }
    $result = $result
        ->addColumn('show_date', function ($row) {
            return '<span class="w-full">' . $row->show_date . '</span>';
        })
        ->addColumn('action', function ($row) {
            $viewUrl = route('', ['date' => $row->show_date]);
            $buttons = '';
    
            return $buttons;
        });
    if(){
        $result = $result->rawColumns(['show_date', 'action'])
    }else{
        $result = $result->rawColumns(['name','show_date', 'action'])
    }
    
    return $result->make(true);
    
    Login or Signup to reply.
  2. You can structure you Datatable like this and add conditional columns.

    $dt = DataTables::of($data);
    if(x == 1){
      $dt = $dt->addColumn('show_date', function ($row) {
                return '<span class="w-full">' . $row->name . '</span>';
            });
    }
    $dt = $dt->addColumn('show_date', function ($row) {
                        return '<span class="w-full">' . $row->show_date . 
                      '</span>';
                    })
    return $dt->make(true);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search