Vue3 + Vite

Vue3 + Vite

btreebtree Posts: 99Questions: 14Answers: 11
edited February 2022 in Free community support

Hello everyone,

I know there are already a few topics about importing DataTables and get it working inside Vue3, but I seem to struggle with the extensions.

So far I successfully imported a module which loads jQuery and Datatables:

# jquery.module.js
import jQuery from "jquery";
Object.assign(window, { $: jQuery, jQuery });
import * as DataTable from "datatables.net-dt";
import "datatables.net-dt/css/jquery.dataTables.min.css";
new DataTable(window, $);
export default window;

In the vite config I added following code (behind the scences it uses rollup.js) to get jquery globally inside my app:

# vite.config.js
  build: {
    rollupOptions: {
      external: ["jquery"],
      output: {
        globals: {
          jquery: "window.jQuery",
          jquery: "$",
        },
      },
    },
  },

The module is then imported before my vue application:

<!-- index.html -->
  <body>
    <div id="app"></div>
    <script type="module" src="/src/jquery.module.js"></script>
    <script type="module" src="/src/main.ts"></script>
  </body>

Everything seems good and dataTable is inside my components and successfully renders the table.

<script setup lang="ts">
# component.vue
import { onMounted } from "vue";
onMounted(() => {
 $("#example").dataTable();
});
console.log($.fn.dataTable);

console log

But if I want to extend dataTables it simply does not work, eg.:

# component.vue
<script setup lang="ts">
import { onMounted } from "vue";
import "datatables.net-select-dt";
console.log($.fn.dataTable.select.version);
# Console log returns undefined for select object
...

Any ideas? I tried to play around with the rollup and other extensions like buttons etc. but with no success. I know we should use require, but don't know how I can get this working inside my vite build.

Cheers
Hannes

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,697Questions: 1Answers: 10,102 Site admin
    Answer ✓

    Hi Hannes,

    I think you'll need to do:

    import Select from 'datatables.net-select-dt';
    
    Select();
    
    console.log($.fn.dataTable.select.version);
    

    When using CommonJS our libraries return a function as the default export. That function can take parameters should you which to load in jQuery or your own window object. If you don't, then you can just call the function as is.

    More information about this available here.

    Allan

  • btreebtree Posts: 99Questions: 14Answers: 11

    Awesome @allan works as expected.

    As I'm currently doing a full rebuild of my app in TypeScript with node.js backend and vue3 frontend its great that DataTables still works for me. Will save me a lot of time!

    Thanks Allan

  • LowProfileLowProfile Posts: 2Questions: 1Answers: 0

    Hi :smiley:

    I've got this initialization in my vue file

    <script setup lang="ts">
    import DataTable from 'datatables.net-vue3';
    import Buttons from 'datatables.net-buttons';
    import ButtonsHtml5 from 'datatables.net-buttons/js/buttons.html5';
    import Select from 'datatables.net-select';
    import JsZip from 'jszip'
    
    
    DataTable.use(Buttons);
    DataTable.use(ButtonsHtml5);
    DataTable.use(Select);
    DataTable.use(JsZip);
    

    Sadly I can't see the Excel export button and in my package.json I've got this dependencies:

      "@vitejs/plugin-vue": "^3.0.3",
      "datatables.net-bs5": "^1.12.1",
      "datatables.net-buttons": "^2.2.3",
      "datatables.net-buttons-dt": "^2.2.3",
      "datatables.net-dt": "^1.12.1",
      "datatables.net-select": "^1.4.0",
      "datatables.net-select-dt": "^1.4.0",
      "datatables.net-vue3": "^1.0.0",
      "jquery": "^3.6.1",
      "jszip": "^3.10.1",
      "typescript": "^4.8.2",
      "vue": "^3.2.38",
      "vue-loader": "^17.0.0",
      "vue-router": "^4.1.5"
    

    At this point the DataTable is working but I need a Excel button and a action button next to:

    const columns = [
              { data: 'id' },
              { data: 'email' },
              { action: button href, etc.}
          ];
    

    Thanks in advance

  • kthorngrenkthorngren Posts: 20,299Questions: 26Answers: 4,769

    This doc shows the options available to display the buttons. { action: button href, etc.} won't work in the columns definition. This doc shows how to create custom buttons with the action option.

    Kevin

Sign In or Register to comment.