Populate the caption of a table from json
Populate the caption of a table from json
iecwebmast
Posts: 66Questions: 9Answers: 1
Hello,
I have a table which will display a customer's data after login. How can I populate the caption of the table with the username supplied in the json data?
{
"username": [
[
"item1",
"item2",
"item3"
]
]
}
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
What is your data source, and how are you building your HTML?
You need to add it yourself using jQuery or DOM methods. DataTables does not provide an API to add the
caption
tag.Allan
I've built a jsfiddle of my table, which includes the caption tag.
http://jsfiddle.net/lbriquet/p2wffs95/
I couldn't figure out how to make a json call work in a jsfiddle though.
On my server I am calling the json like this:
I would like to pick up the username, from the json file. For example: "Mike Smith" and populate the caption tag with it. My json is built like this for now:
Could you help me to get the username to populate the table caption?
Thank you in advance for your help!
Use
initComplete
to which the JSON is passed. Then grab the object key from it (presumably you know it in advance since you'll need to tell DataTables what it is usingajax.dataSrc
- I don't really understand why you haveMike Smith
in your data andmydata
in the configuration). Then just create a tag an insert it using jQuery.Allan
Hi Allan,
I can include the username in the json file generated after authentication... so I will not know in advance the username. I am not sure how to structure the json to provide something that I can pick up for the caption. So I put
Mike Smith
in the json as an example of a username I'd like to pick up. My thought was to be able to call the json with a variable like "username" and then use the value for the caption. Since I didn't know how to do this in the javascript I I usedmydata
. I've look at all the examples I could find, but I haven't been able to figure it out... which is why I decided to submit a question to the forum. I would really appreciate your help... I'm so close to having a great solution with your great datatables tool!Why not just add
"caption": "Mike Smith"
to the JSON? Then you can usejson.caption
to access the caption property.Allan
Thank you Allan!
I've adjusted my json accordingly:
I'm accessing my data with
"dataSrc": "data"
Can you tell me how to use
json.caption
to populate the table caption?Yes, is is just basic jQuery
Allan
Hi Alan,
Thank you very much for your help!
I've added the appendTo in a initComplete, but it is not functioning. I'm wondering whether it requires that everything uses the new naming conventions and .DataTable?
Here is the code I am using... could you take a look and let me know what I'm doing wrong?
My json looks like this:
You can't have both initComplete and fnInitComplete . You must use only one (since one is just an alias of the other it will overwrite the other).
Try removing your "Setup DataTable Defaults" block and see if it works. If it doesn't please link to a test page showing the issue.
Allan
Hi Allan,
I've removed the "Setup DataTable Defaults" block, but I am still not able to populate the caption. I've created a new jsfiddle calling the json file.
http://jsfiddle.net/lbriquet/xmbz73ev/
Could you please take a look and let me know what I've done wrong?
Thank you so much for your help!!
The caption is being shown at the top of the table in your example.
Allan
I don't understand... the caption is not shown.
The caption tag should be populated with the json caption data "Mike Smith"
Picking up the json data for the caption:
The caption is there. If you look at your jsfiddle it doesn't update html window in fiddle itself. If you use inspect in chrome or other developer tools suite in another browser on the preview window you will see it indeed placed mike smith in the caption... I don't think jsfiddle updates the html window portion if you manipulate dom after load etc.
There are two captions in the HTML. The first one empty. You could use jQuery to remove that, or just not put it there in the first place.
Allan
I found the problem... it needt to be prependTo, instead of appendTo, since the caption tag is at the top of the table.
Thank you so much for your help Alan!!!
Strictly by HTML specs, yes it should be the first element. I doubt any browser would struggle with it anywhere in the table tag though.
However, good to hear you've got it working now.
Allan
Hi again Alan,
Just one more question...
How can I include my reset search button in the
initComplete
?I was calling it from
fnInitComplete
using a$.extend($.fn.dataTable.defaults,
at the end of my code.Here is a link to my jsfiddle:
http://jsfiddle.net/lbriquet/q572fud9/
This is the old
fnInitComplete
I would like to include in theinitComplete
.Thank you again for your help!!!!!!!!
I've managed to successfully add the reset search button this with.
but I've not been able to get the click function working:
$('.btnClearDataTableFilter').click(function () {
oTable.search( '' ).draw();
})
Here is a my revised jsfiddle... could you please take a look. I think the reset seach click function must be very close to working.
http://jsfiddle.net/lbriquet/8hhyxka1/1/
This code is currently being executed before the clear button has been added to the document. It runs because
initComplete
since you are using async loading of data.Either make it a delegated event or just stick it inside the
initComplete
function as well.Allan
I've moved the code inside the
initComplete
function... but the click function is still not working. I get a Firebug errorTypeError: oTable.search is not a function
I've my updated jsfiddle.
http://jsfiddle.net/lbriquet/gjaumzqc/3/
Could you please take a look?
I think the problem has to do with converting everything to datatables 1.10. I tried changing the datatables Initialise call to use the new camelCase
And now my reset search button! But... my filters don't.
http://jsfiddle.net/lbriquet/rd8egf83/
I've tried to convert everything to the new new camelCase for datatables 1.10... but then everything breaks.
http://jsfiddle.net/lbriquet/rd8egf83/1/
How can I resolve this?
Looks like you've updated the
rowCallback
parameters:But not now those parameters are called (
nRow
specifically should berow
now): http://jsfiddle.net/rd8egf83/2/Allan