Custom query on button

Custom query on button

m75sam75sa Posts: 132Questions: 30Answers: 0

Hi, I would like to add a custom button to export .txt file that execute a customized mysql query.
What i can see is that i can customized columns shown on the table but in my case i don't have columns shown so the quey must show only result into the exported .txt file.
Any ideas?
Thanks for the support

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974

    I don't understand the part about not having columns shown in the table. If you are using the Buttons extension you can create a custom button. See this example. The Action function can be any code you create to execute the MySql query and save to a text file.

    Kevin

  • m75sam75sa Posts: 132Questions: 30Answers: 0

    @kthorngren i try to explain.
    I have my table that comes from mysql query such as:
    name + surname + age

    and buttons that export pdf, csv and print.
    Now if i click on the button the export shows only the result of name + surname + age so what i see on screen.
    What i want is an additional button (called i.e. txt) that exports in txt format the following columns:
    name + surname + age + phonenr

    so phone nr is in the same table of name + surname + age...

    so i think that i need to query the database with the phonenr too...

    how to do?

  • m75sam75sa Posts: 132Questions: 30Answers: 0

    any help?

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    It might be easier to return all the data to the table - but hide those columns are view - using columns.visible. You can then export them by doing something like this:

                {
                    extend: 'pdfHtml5',
                    exportOptions: {
                        columns: function ( idx, data, node ) {
                          return true;
                        }
                    }
                },
    

    See example here: http://live.datatables.net/qitayeva/1/edit

    Colin

  • m75sam75sa Posts: 132Questions: 30Answers: 0
    edited February 2022

    thanks colin, this could work but if i have to export a .txt file with items separated by commas?

  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974

    The CSVHtml5 options docs show all the options available. See if the fieldSeparator will change the format to remove the commas.

    Kevin

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Yep, the export code I posted could be used for both PDF, CSV, and any other export type.

    Colin

  • m75sam75sa Posts: 132Questions: 30Answers: 0
    edited February 2022

    thanks colin. Is there a way to skip the first row on the csv exported file? I don't want the columns header title...

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    This thread should help, it's asking the same thing.

    Cheers,

    Colin

  • m75sam75sa Posts: 132Questions: 30Answers: 0

    thanks a lot. :)
    Back to my previous question, i was able to hide columns during the export (this is good) but i didn't found a solution how to hide columns when table is shown so not when i export...

  • m75sam75sa Posts: 132Questions: 30Answers: 0

    any help on this? Sorry to press you but i'm in panic...

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓

    i didn't found a solution how to hide columns when table is shown so not when i export

    I'm not clear what that means, I'm afraid. Do you mean something like the "colvis" button - see example here.

    If not, please provide a test case with steps on how to reproduce the issue. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

Sign In or Register to comment.