How to dynamically set the title of a file.
How to dynamically set the title of a file.
I have an input field and a print button in a collection dropdown. The input field helps users to set their preferred title dynamically.
Since there are a couple of tables on the page, I was struggling to get the input field's value. But I managed to get it working with below code.
config.parent._collection[0].firstElementChild.value.trim();
Please is it how something like this should be handled or there is a better way?
Thanks.
Test Case: https://live.datatables.net/gidakera/1
The actual code is similar to this.
var table = new DataTable('#example', {
layout: {
topStart: {
buttons: [
{
extend: 'collection',
text: 'Export',
buttons: [
'<input type="text" maxlength="100" placeholder="Enter title">',
{
extend: 'print',
title: function (config, dt) {
return config.parent._collection[0].firstElementChild.value.trim();
}
}
]
}
]
}
}
});
This question has an accepted answers - jump to answer
Answers
That works well, nice job,
Colin
Rather than using the private variables (
_collection
), since it is possible that they might change between versions (although unlikely in this case I think), I'd be tempted to assign an id to theinput
and then query that directly:Updated example here: https://live.datatables.net/geqeyizi/1/edit .
Allan
Thanks for the replies.
@allan that is what I did initially but I wanted to avoid
id
s andclass
es so I can extract the collection into a custom button which can be used on multiple tables on the same page.With
id
s andclass
es it's hard to get the correct title related to the clicked button. That is why I wanted to selected the actual DOM element.But I guess I should rethink my approach.
Thanks a lot.
Please ignore my earlier comment. I tested @allan's answer, it works well.
Thanks
Awesome - thanks for the update. I'll look into a way via the API to address the button without needing to have a static / manual value like that.
Allan