With JQuery Data tables, is it possible to split a single column up into parts purely for readability? For example, I have a databases of URLs split into the domain and the path. I want to keep the path whole, but for readability I would like each part of the path split up into columns. For example:
DOMAIN | PATH
www.xxx| /p | /rty | |
www.zzz| /o | | |
www.yyy| /yu| /x | /t|
I have tried using render to split up each URL with spaces, but this is less than ideal as everything is within the same column and rather an eyesore.
EDIT Just to be clear, I would really like to keep the path as one data entry. I am aware this is an odd choice, but in my case it is preferable.
EDIT Code to fill in the table
Javascript
<script>
$(function() {
$('#ut-table').DataTable({
processing: true,
serverSide: true,
ajax: '{!! url('/all') !!}',
columns: [
{ data: 'frequency'},
{ data: 'occurrences'},
{ data: 'protocol'},
{ data: 'domain'},
{ data: 'path', render: function(data, type, row){
var newchar = ' | ';
return data.split('/').join(newchar);
}
},
],
});
});
HTML
<table class="table table-bordered" id="ut-table">
<thead>
<tr>
<th>frequency</th>
<th>occurrences</th>
<th>protocol</th>
<th>domain</th>
<th>path</th>
</tr>
</thead>
</table>
Laravel Back End
public function all()
{
Debugbar::warning('query fired');
return Datatables::of(
Unknown_Tag::query()
->orderBy('frequency', 'desc')
->groupBy('urlTrimmed')
)->make(true);
}
EDIT: Thanks a million to Louys and his answer, however I still have a problem. I need to split up my path, not my url. EX I need to split this: "/this/is/my/path" into this: this is my path I tried the below code:
<script>
$(function() {
var table = $('#ut-table');
table.dataTable({
processing: true,
serverSide: true,
ajax: '{!! url('/all') !!}',
columns: [
{ data: 'domain'},
{ data: 'path', render: function(data, type, row){
var regexp = /^\/.*?(?=\/|$)/g;
var match = regexp.exec(data);
return match[0];
}
},
{ data: 'path', render: function(data, type, row){
var regexp = /^\/.*?(?=\/|$)/g;
var match = regexp.exec(data);
return match[1];
}
},
],
});
});
However, since both calls to my regex are in different scopes, only my first match will be returned. Any idea how I can get around this without having to run a loop for each iteration? Actually, this thought just came to me, is there anyway I can edit the JSON from my PHP call to include the split up path?