Datatables - Codeigniter 4.6.3 - and Links to another web page
Datatables - Codeigniter 4.6.3 - and Links to another web page

I am working on my first datatables code - and have the table working pefectly with records and images. I have been looking on how to make the first column a link to another page. I am doing this as I want to be able to click a row and see all the details and a full size image. Before deciding to use datatables I had it working in Codeigniter - but changing to datatables (which is better) - adds a few more challenges.
Here is my code for how I think it should work:
columnDefs: [
{
targets: 0,
data: 'link',
render: function (data, type, row, meta) {
return '<a href="<?= url_to('Tvdetail::details', ${data}) ?>">;
}
],
Once again the problem is the PHP code that must be used in Codeigniter to allow a link to go to the next page.
The url_to is telling the system what web page to use and then the function. The last argument is the record number which is from the datatables -- ${data} - but once again the data is an unkknown object due to the PHP code.
I tried using a Let statement to give it the PHP code - but - the part that I cannot get to work is how to include the argument part of the php statement (the ${data}) - since it is needed to complete the PHP statement.
Ideally I need to grab the record number that the ${data} holds and pass it to the PHP statement.
It could be I just can't get the syntax straight.
Thoughts anyone?
Thanks
Roger
Replies
Hi Roger,
Indeed, this is the same as yur other thread. You can't have PHP executing as part of a Javascript function. You can use PHP to output Javascript, but you can't execute a PHP function from Javascript.
The way to remember it is that PHP executes server-side, while the Javascript executes client-side.
If you need PHP to resolve the URL for the link, then you need to have that resolving done before you send the data to the client-side. Are you Ajax loading this data? If so, have the server-side send the already rendered link (or at least the href and the text to display), then you can simply display it.
Allan
So there is no way to click on a datatable link and go to another web page?
I might have to abandon datatables then.
Roger
Yes, of course DataTables can show links
. There are many ways to do it and the key will be how you are loading the data into your table.
Are you generating the HTML in PHP and then having DataTables run on that (what we call DOM sourced data)? Or are you using JSON / Ajax loaded data?
I'm going to assume the latter based on the fact that you are using
columns.data
. That being the case, you need the link (i.e. the href) in the data source (and if you want per row text for the link, that text as well).For example you might use:
If you have per-row text:
It would be useful if you could give a link to your page so I can see the configuration you are using.
Allan
I have successfully linked to another web page when I click on the 1st column which I made my clickable item on each row. It shows the record number from the database.
All I need to do now is figure out how to pass the row data that I selected to the other web page.
I've seen many examples - but my problem is where do I add the code to my existing datatable script?
By the way - when I use the Data: 'link' line - all it shows is undefined. Is it supposed to say undefined or am I missing something? If I leave that line out it shows my Record ID - which is a link I can click on.
Just not sure how to send the data to the linked PHP page.
Roger
Assuming the data you want is in the row's data object, then use the third parameter passed into the
render
function. See my last example above for an example of that, I would suggest reading over the rendering functions part of the manual as well.Seems to work okay here. If you are having problems with something, please link to a page showing the issue so I can take a look.
Allan