I have been wrangling Chumper's datatable and struggled to get an element in the datatable to be a link to a route with the elements id. e.g. user/25/show. I finally did it this way:
->addColumn('companyname',function($model)
{
$link = "<a href = 'company/" . $model->id ."/show' >".$model->companyname."</a>";
return $link;
})
sahamilton said:
I have been wrangling Chumper's datatable and struggled to get an element in the datatable to be a link to a route with the elements id. e.g. user/25/show. I finally did it this way:
->addColumn('companyname',function($model) { $link = "<a href = 'company/" . $model->id ."/show' >".$model->companyname."</a>"; return $link; });
->addColumn('companyname', function($model) {
return '<a href = "' . route('company.show', $model->id) . '">' . $model->companyname. '</a>';
});
Thanks Erik for that simplification.
Now I am trying to get the setUrl command to work.
I have created and tested a route to the api:
Route::get('api/location/{companyId}',array('as'=>'api/location', 'uses'=>'LocationsController@getDatatable'));
I know that works as I have entered the URL api/location/38 and receive the collection back.
and have the following in the blade template:
{{ Datatable::table()
->addColumn('Street','City','State','ZIP') // these are the column headings to be shown
->setUrl(route('api/location/'.$data['company']->id)) // this is the route where data will be retrieved
->render() }}
However I get a Laravel error:
ErrorException
Route [api/location/38] not defined. (View: D:\xampp\htdocs\mapminer\app\views\companies\newshow.blade.php)
I'm not sure what I am doing wrong here. :-(
{{ Datatable::table()
->addColumn('Street', 'City', 'State', 'ZIP')
->setUrl( route('api/location', $data['company']->id) ) // You used a dot instead of a coma
->render() }}
For an explanation of the route helper click here.
Thanks so much Erik .. As always the devil is in the details! You saved me a few hours and countless hairs on my head (which is good as I don't have countless!).
Well now I seem to find that if I make a column (e.g. add a link to the company name per above), this column now sorts (almost) by its id versus its name.
I added two columns both included in the orderColumns array. One column, company name is defined in showColumns the other is an add column of the company name with a route link added.
return Datatable::collection(Company::all())
->showColumns('id','companyname')
->addColumn('companynamelink',function($model)
{
return link_to('company/'.$model->id.'/show', $model->companyname, $attributes = array(), $secure = null);
})
->searchColumns('companyname')
->orderColumns('id','companyname','companynamelink','types')
->make();
The straight (unlinked) company name will sort as expected, however the linked company sorts by its id as follows: 1,10,11...2,20,21 ...3,30,31 i.e. not numerically but text / numerically!!
Is this a 'feature' or my misunderstanding?
Ok I have solved that issue. I added the ->setOrderStrip() to the datatable function
->searchColumns('branchname','branchaddress')
->orderColumns('branchname','city','state','region')
->setOrderStrip()
->make();
All appears well now.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community