Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Action Buttons #6

Open
abdullah-abunada opened this issue Oct 3, 2022 · 8 comments
Open

Action Buttons #6

abdullah-abunada opened this issue Oct 3, 2022 · 8 comments

Comments

@abdullah-abunada
Copy link

abdullah-abunada commented Oct 3, 2022

Hello,

Any idea about how we can add custom vue component as action in datable?
for example why this component did not support slot for row to allow us to add custom actions like dropdown list.

Best,

@AllanJard
Copy link
Contributor

You can't currently add a Vue component inside a DataTable - sorry.

@monster010
Copy link

The question is will it come in and if so, when?

@AllanJard
Copy link
Contributor

Due to the way it is currently implemented, no, I'm afraid that this ability will not be coming any time soon.

@conwayatmundotca
Copy link

I created the following component for myself..

import {createVNode, render} from "vue";
import DataTablesLib from 'datatables.net';

/**
 *
 * @name renderVueComponent
 * @memberOf DataTablesLib
 * @param {Object} component - vue component
 * @param {Object} meta - meta infomation from column
 * @param {Object|false} [props] - object of props, if false (default) then row data is used
 * @param {boolean} includeTableAndMeta
 * @returns {String}
 */
function renderVueComponent(component, meta, props = false, includeTableAndMeta = false) {
    props = props ? props : this.cell(meta.row, meta.col).row().data();
    if (includeTableAndMeta) {
        props._table = this;
        props._meta = meta;
    }
    let nodes = this.cell(meta.row, meta.col).nodes();
    if (nodes.length > 0) {
        this.on('draw', () => render(createVNode(component, props), nodes[0]));
    }
    return '';
}

DataTablesLib.Api.register('renderVueComponent', renderVueComponent);

Here are some snipets that i have in the page component where I have datatables....


import TableOptions from "@/Components/RegistrationRequests/TableOptions.vue";

<DataTable
      :ajax="ajax"
      :options="options"
      :columns="columns"
      ref="datatable"
  >

onMounted(() => {
  table = datatable.value.dt;
})
static columns = [
//...other columns
{
    mData: 'id',
    mRender: (data, type, row, meta) => table.renderVueComponent(TableOptions, meta, {id: data})
  }
]

TableOptions is a component with 3 buttons.

The only instance where it doesn't work is where I change the ajax source dynamically. It seems to be related to this.cell not referencing the correct node.

I'm sure there are opportunities to make this a bit better.

@conwayatmundotca
Copy link

Writing this post gave me some renewed energy to look at the ajax issue....

const ajax = {
  url: registrations_list_endpoint,
  data:function(d){
    d.format = 'dataTables',
    d.filterQuery = props.type
  }
};

onUpdated(()=>{
  table.ajax.reload();
});

@coyy
Copy link

coyy commented Feb 27, 2024

@AllanJard version with template columns not working after pagination.
@conwayatmundotca Your code only works when the column index = 0, any column after that doesn't work

My options config:

import TableAction from "@/Components/TableAction.vue";

options: {
  columns: [
  //...other columns
   { 
      data: '',
      title: 'Action'
    }
  ],
  fnDrawCallback: function (dt) {
      let rows = dt.api.rows();
      let headers = dt.api.columns().header();
      for (let i = 0; i < headers.length; i++) {
        if (headers[i].innerText == 'Action') {
          rows[0].forEach((item) => {
            let props = dt.api.row(item).data()
            let node = dt.api.cell(item, i).node()
            if (node) {
              dt.api.on('draw', () => render(createVNode(TableAction, props), node));
            }
          });
        }
      }
    }
}

@AllanJard
Copy link
Contributor

Are you using the unreleased and undocumented version? If so, can you link to a test case showing the issue so I can look into it.

@coyy
Copy link

coyy commented Feb 27, 2024

Are you using the unreleased and undocumented version? If so, can you link to a test case showing the issue so I can look into it.

I don't have the test version online at the moment. But I see you use ColumnDefs. I tested it locally and it doesn't work after switching pages

<DataTable :options="configTableFile">
    <template #column-5="props">
      TEST
    </template>
</DataTable>

Alt Text

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants