Issue with select list items appearing in Copy/Excel Export

Issue with select list items appearing in Copy/Excel Export

lion1lion1 Posts: 10Questions: 2Answers: 0

I feel like I'm close to solving this. In each table cell in a row the value is in a <span> and outside a select list or input box is hidden until a user double clicks to edit. When I select rows for export the select list items are included into the export. I've tried to use columns function to look for the span but still not able to export without the select list items. Is there a good way to do this?

extend: 'copyHtml5', exportOptions: {
                                modifier: { selected: true },
                                    columns: function (idx, data, node) {
                                        if (table.column(idx).visible() == true) {
                                            var isVisible = table.column(idx);
                                            //var txt = isVisible.find('span').text();  --thought this would work but doesnt know its in a row?

                                            return isVisible;
                                        }
                                  }
                                    //body: function (data, row, col, node) {
                                    //    if (table.column(col).visible() == true) {
                                    //        return table
                                    //            .cell({ row: row, column: col })
                                    //            .nodes()
                                    //            .to$()
                                    //            .find('span')
                                    //            .text()
                                        
                                        //else { return data; }
                                    //}

                            //}

these are a couple ways I've tried, hence the comments, but columns function seems to work the closest, I just don't know how to access the cell within the row. Please advise. Thank you.

This question has accepted answers - jump to:

Answers

  • colincolin Posts: 14,303Questions: 1Answers: 2,430

    Hi @lion1 ,

    That sounds like a fiddly setup, it's hard to think of a solution without seeing the configuration. We're happy to take a look, but it would help, as per the forum rules, if you could link to a running test case showing the issue so we can offer some help. 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

  • lion1lion1 Posts: 10Questions: 2Answers: 0

    Hi @colin ,

    Created a test case below. If you try the button for copying the row or csv, you can see the <td> where my select options live (even if hidden) are being included into the export. I need just the value that lives inside the <span> in each <td>. I extended and attempted a couple ways to configure the export options to be able to achieve what I want, but to no success so far. The commented code includes other ways I have tried, like using the body/ column functions. Hope you can help me out!

    http://live.datatables.net/yiyihute/5/edit

  • colincolin Posts: 14,303Questions: 1Answers: 2,430

    Hi @lion1,

    Thanks for that example, that helped. Take a look at this example - the column is being rendered differently in an export. Now, instead of just returning those HTML elements, it uses jQuery to pull out the text.

    Hope that all makes sense, and you can use in your code,

    Cheers,

    Colin

  • lion1lion1 Posts: 10Questions: 2Answers: 0

    Hi, @colin ,

    That example cleared things up alot. In my actual code I have 2 or 3 columns with that select list configuration, and tried to apply the colDefs to that another column for the jquery, but it does not work for both columns in the example. How can I configure it for multiple columns? Here is an example,

    http://live.datatables.net/vibavota/2/edit

  • colincolin Posts: 14,303Questions: 1Answers: 2,430
    Answer ✓

    Hi @lion1 ,

    You can use the same render function for them all - see this example here. This does the same in the CSV for both columns.

    Cheers,

    Colin

  • lion1lion1 Posts: 10Questions: 2Answers: 0

    Thanks! @colin

    One last problem with this setup is when I create individual column searching with select inputs, the options include the select list items. Can I do something similar here to only include the value in the select lists?

    http://live.datatables.net/wuwiceva/2/edit

  • colincolin Posts: 14,303Questions: 1Answers: 2,430
    Answer ✓

    Hi @lion1 ,

    Here you go - I made a few changes, your code is commented out. The main change was to get the string out of the span, then to change the search,

    Cheers,

    Colin

  • lion1lion1 Posts: 10Questions: 2Answers: 0

    Hi @colin

    Perfect this solved my problem in all my columns, except the ones where the cell contains a <a> link as the display. I created the test case below, was wondering if I have to specify that a certain col has a link and needs to find the span and return text rather than html. Thanks for all the help so far

    http://live.datatables.net/wuwiceva/5/edit

  • colincolin Posts: 14,303Questions: 1Answers: 2,430

    Hi @lion1 ,

    In that example you sent, I assume you mean "Tiger Nixon" has the <a> link - but for me, when I export it, it still behaves and only exports the "Tiger Nixon" string. I may be being daft (coffee not hit yet) but could you give me steps on how to reproduce your problem, please,

    Cheers,

    Colin

  • lion1lion1 Posts: 10Questions: 2Answers: 0

    Hi @colin ,

    The export is working fine! The issue is with the select list options in the table footer.
    http://live.datatables.net/wuwiceva/5/edit

    Thanks!

  • colincolin Posts: 14,303Questions: 1Answers: 2,430

    Ah, that's just a case of breaking it down again by the 'a', like this,

    Cheers

    Colin

  • lion1lion1 Posts: 10Questions: 2Answers: 0

    Thanks for that @colin, that ended up breaking the other two select list options as they became 'undefined' if you use a column that doesn't have the 'a'. In your example the other two select lists only have 'undefined'

    Thanks!

  • colincolin Posts: 14,303Questions: 1Answers: 2,430

    Not sure if you're being sarcastic there with the thanks, or genuinely thanking me! :) It's an easy test to see if 'undefined', and if it is, do the other extraction.

    C

  • allanallan Posts: 56,187Questions: 1Answers: 8,897 Site admin
    Answer ✓

    For what its worth, its probably easier to do this with a regex rather than inserting it into the DOM (although that is the only 100% certain way of getting the text - just don't nest HTML in your attributes and you'll be fine): http://live.datatables.net/bokamudo/1/edit .

    Allan

This discussion has been closed.