How can I select which data to show and which data to print in excel

How can I select which data to show and which data to print in excel

keremardiclikeremardicli Posts: 7Questions: 2Answers: 1

I am using serverside solution. My tables is working fantastically. Though, I need new requirement. While displaying first 6 columns in html, I need to show all 8 columns in excel. The moment I add a new data to Columns, script stop working. I ha ve the info from server.

I could not find anything in the documentation. How can I achive that?

        $(document).ready(function() {

            let datatable = $('#datatable').DataTable({
                "orderCellsTop": true,
                "dom": "<'top'lBf>rt<'bottom'ip><'clear'>",
                "buttons": [{
                        extend: 'excel',
                        footer: false,
                        exportOptions: {
                            columns: [0, 1, 2, 3, 4, 5, 6, 8]
                        }
                    },
                    {
                        extend: 'pdf',
                        footer: false,
                        exportOptions: {
                            columns: [0, 1, 2, 3, 4, 5, 6, 8]
                        }
                    },
                    'copy'
                ],
                "language": {
                    url: './turkish.json'
                },
                "pageLength": 20,
                "lengthMenu": [
                    [20, 50, 100, -1],
                    ["20 Adet", "50 Adet", "100 Adet", "Tümü"]
                ],
                "oLanguage": {
                    "sSearch": "Arama: "
                },
                "bSortClasses": false,
                "bStateSave": true,
                "order": [
                    [0, 'asc']
                ],
                "processing": true,
                "serverSide": true,
                "ajax": {
                    url: './api',
                    type: 'post',
                    data: {
                        "kongre": "<?= $dbname ?>"
                    }
                },
                "columnDefs": [{
                        target: ['nosort'],
                        orderable: false
                    },
                    {
                        render: function(data, type, row) {

                            className: "text-danger";

                            return data;
                        },
                        targets: 5,
                    },
                    {
                        render: function(data, type, row) {
                            // console.log(data)
                            let html = `<button class='btn btn-primary btn-sm' data-toggle='modal' data-target='#exampleModal2' 
                            onclick='UpdateMe("${data}")'>Düzenle ya da Giriş yap</button>
                            <!-- <button class='btn btn-danger btn-sm border border-dark' onclick='epostaGonder(this)'>E-Posta Gönder</button> -->`;
                            return html
                        },
                        targets: 7 // actions a mukabil geliyor
                    }

                ],
                "columns": [{
                        data: 'id'
                    },
                    {
                        data: 'Name'
                    },
                    {
                        data: 'Surname'
                    },
                    {
                        data: 'Email'
                    },
                    {
                        data: 'gsm'
                    },
                    {
                        data: 'gorev'
                    },
                    {
                        data: 'girdi'
                    },
                    {
                        data: 'actions'
                    }
                ]
            });

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 21,299Questions: 26Answers: 4,945

    YOu have 8 columns defined using columns.data. The column indexes start at 0 so you have the renge of 0 through 7 available. You have columns: [0, 1, 2, 3, 4, 5, 6, 8]. The 8 is trying to access the 9th column which doesn't exist. Change it to 7.

    Kevin

  • keremardiclikeremardicli Posts: 7Questions: 2Answers: 1

    I did that too of course but when i click on excel button it just does not respond. İf i return data in columndefs, table does not render at all

  • kthorngrenkthorngren Posts: 21,299Questions: 26Answers: 4,945

    Sounds like you might be getting a Javascript error. Check the browser's console for errors.

    Can you post a link to your page or a test case replicating the issue so we can help debug?
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • keremardiclikeremardicli Posts: 7Questions: 2Answers: 1

    There you go.

    You can add {data: 'otel'}, and print it to excel but not to the HTML.
    https://live.datatables.net/qunabesi/2/edit?html,js,output
    I have skipped some buttons and styling...

    Note: I have disabled CORS but will enable it later on.

  • allanallan Posts: 63,451Questions: 1Answers: 10,465 Site admin

    I've reordered your Javascript script tags, added in JSZip, pdfmake and Buttons. And it appears to work as expected: https://live.datatables.net/qunabesi/2/edit .

    Allan

  • keremardiclikeremardicli Posts: 7Questions: 2Answers: 1
    edited February 2023

    Now I have added {data: 'otel'} and it is not working.
    Though it is sent by server. :)

  • kthorngrenkthorngren Posts: 21,299Questions: 26Answers: 4,945
    Answer ✓

    I see. The question is that you are not showing all the data in the JSON response but want to export some of that data. Add those columns using option columns.data and use columns.visible to hide them in HTML, for example:

    {
      data: 'otel',
      visible: false
    }
    

    See the Column selectors example to help export the columns you wish.

    Kevin

  • keremardiclikeremardicli Posts: 7Questions: 2Answers: 1

    YES!!! Thanks alot man. I knew it was easy but could not find it anywhere. Now I need to find how I give a column name in the excel for visible data, and all is set :)

  • kthorngrenkthorngren Posts: 21,299Questions: 26Answers: 4,945
    Answer ✓

    Try columns.title.

    Kevin

Sign In or Register to comment.