View A Single Record
View A Single Record
I suspect the answer to this is 'no' having read what I can found on the forum but I thought I would ask anyway.
I am creating a JSON data source which is accessed using the server side method as per one of the standard ways to use DataTables. It works incredibly well and my php file usually contains a number of joins and filters which allow me to request all the necessary data I require when viewing multiple records.
Normally I implement a 'View Record' button which allows the user to view a single record from the table. It seems a shame that in reality I already have the data for the single record available from my JSON data source but I cannot access it in a single record format, i.e. I don't want to display it as a table.
So, can you access the JSON data source in any other way to allow a single record to be viewed or is it not possible?
Thanks
Chris
This question has an accepted answers - jump to answer
Answers
I might be misunderstanding your question but you can use the Select extension to easily get the data of a selected row.
Here is an example:
https://datatables.net/extensions/select/examples/api/get.html
Kevin
Hi Kevin,
What I want to do though is use that data and pass it to a new page which displays the 'detailed' record, i.e. the data from the single row.
I have found this on Stackoverflow which looks like it could send the row data?
https://stackoverflow.com/questions/8599595/send-json-data-from-javascript-to-php
What do you think?
Thanks
Chris
Well, I don't know anything about PHP but I may be able to get you to step 2. Built a simple example using the Select extension. Click a row and the data in that row is put into a JSON string.
http://live.datatables.net/helayoni/1/edit
I would think that you can then use AJAX to POST the JSON string to your PHP script.
Note that I removed the 'Salary' column from the table to show that all the data returned for that row, whether in the table or not, can be put into the JSON string.
Also used
toArray()
to put the data into an array before converting to JSON string.Hope this gets you started. Others may have alternate / better ideas.
Kevin
I think there are basically two options here:
row().data()
as Kevin suggests to generate the new page with a custom Javascript function. You don't need to hit the server-side at all for this. That might be an advantage to you, it might not!row().data()
could be used for that) and use that to open a new window with the id in the URL. If you want to reuse the Editor PHP libraries to get the data, you could add a condition so that only the data for that row is fetched. This way you can generate the page on the server-side.Its really up to you if you want to generate it on the client or the server-side.
Allan
Many thanks for your extremely useful answers.
If possible I would prefer to be able to access the data on the server side. For example if currently I do something like this using the URL:
/customer/profile/[customer_id]
I then extract the ID and use that to get the data, however, as already indicated I have to rewrite the library to access the data again as I don't know how to utilise the Datatables library again.
Allan, I don't really understand what you mean by 'reuse the Editor PHP' libraries to get the data. Well, I know what you mean but I don't know how you would reuse the libraries. I know it is probably outside the remit of the Editor documentation but is there anywhere I can see information/help about your concept in more detail. I.e. what files do I need to access and how do I parse the ID to the fle?
Many thanks
You'd basically create another
Editor::inst()
but just with a->where()
condition attached to it, which would limit the data selected to just the required customer id.Another option would be to use a simple SQL
SELECT
for that one record. That would actually make things easier for a single record and also offer more flexibility than the Editor libraries can offer.Allan
Hi Allan,
Thanks for your response. I have had a go and it seems to work. This is just some quick proof of concept code but it works so shows the approach is possible.
Thanks for your help. I can now present my 'Detail' view using the same code which is used to generate the summary datatable so this should help keep things efficient and tidy.