Rails7 ImportMaps
Rails7 ImportMaps
I followed the very useful discussion in this question and dataTables is working well . This is how I have setup the file where dataTables is imported:
import jquery from "jquery"
window.jQuery = jquery
window.$ = jquery
import DataTable from "datatables.net-bs5"
import Buttons from 'datatables.net-buttons-bs5'
import 'jszip'
import 'pdfmake'
window.DataTable = DataTable();
Buttons();
However, I still cannot get the datatables.net-buttons-bs5 plugin to work. It seems like I must have something wrong with the syntax because although dataTables itself recognizes that jQuery is available, for some reason Buttons does not and the browser reports these errors:
Uncaught ReferenceError: jQuery is not defined
at dataTables.buttons.min.js:5:283
at dataTables.buttons.min.js:5:308
Uncaught ReferenceError: jQuery is not defined
at buttons.bootstrap5.min.js:5:379
at buttons.bootstrap5.min.js:5:404
Any suggestions would be so very, very greatly appreciated.
Answers
My apologies, the error message re. JQuery is not defined were due to an error in another file. Those messages are gone now, but the buttons are still not being rendered in the table. I understand that having a test case would be the best way to debug this, but if there is anything obviously wrong with my syntax of the import statements or calling the Buttons() initialization, it would be great. Otherwise, I'll try to build a stripped down testcase.
Try:
That might resolve it. If it doesn't can you show me how you are initialising the DataTable. Then if there is nothing wrong with that, I'd need a test case.
Thanks,
Allan
Thanks Allan! Unfortunately, the buttons are still not rendered. Here's how I initialize the DataTable:
You either need to:
dom
option to have the Buttons display, as shown in this example, orAllan
Thank you, again! Unfortunately, the buttons are still not being rendered when I initialize with:
Can you give me a link to your page so I can take a look and see what is going wrong?
Thanks,
Allan
Working! Thank you Allan!
Here's what we had to do to get DataTables with Plugins working on Rails7.0.4 using import maps (this repository has a working rails 7 example)
Hi.
I try this repository https://github.com/rossatboulder/rails7_datatables_example and it work.
But when I add the plugin yadcf it doesn't work.
init_datatables.js
When I try
Error:
Uncaught TypeError: yadcf.init is not a function
YADCF is not supported by us I'm afraid - it is third party software. You'd need to ask the author if it supports ESM loading.
Allan
Allan, thanks. I read about SearchPanes, this works for me.
Hi.
I again try this repository https://github.com/rossatboulder/rails7_datatables_example and it work.
But when I add Editor, error
dataTables.editor-4f659a37df65b51caad5240bac45cd2f7c1ca2dd44629e560725ac83c7d3f9de.js:46 Uncaught ReferenceError: jQuery is not defined
Editor in local folder, import:
import 'editor/dataTables.editor'
This my importmap
You should probably be using the Editor
.mjs
files rather than the UMD loader in the.js
file.Allan
Allan, thanks.
One more problem was I can't load mjs files (only js) in Rails importmaps. But I simply rename mjs to js and code work fine.
mjs
is the accepted extension for Javascript modules (Node docs). I'd be surprised if an importer as common as Rails can't use mjs - although I haven't used it myself, so I don't know. Maybe as on SO or some other Rails support forum?At the moment, I don't plan to change the extension of the DataTables module files I'm afraid. The .js files for our distributions are UMD loaders (AMD, CommonJS and browser).
Allan