exportOptions format and call default function

exportOptions format and call default function

Mike205Mike205 Posts: 2Questions: 0Answers: 0

HI everybody,

I want the exportOptions format body to export the value of an input when there is an input and call the default behavior (strip HTML tag) when there is no input.

var buttonCommon = {
        exportOptions: {
            format: {
                body: function ( data, row, column, node ) {
                    var val = $(node).find('input:not([type=hidden])').val();

                    // when val is undefined i want to call default exportOptions.format.body() ????
                    return (val === undefined)? data : val; 
                }
            }
        }
    };

I don't want to copy the default function strip in my own code because it can change in a next version of datatable.
Is it possible to do that or i have to copy the default function strip in my own code?

var strip = function ( str ) {
        if ( typeof str !== 'string' ) {
            return str;
        }
        // Always remove script tags
        str = str.replace( /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '' );

        // Always remove comments
        str = str.replace( /<!\-\-.*?\-\->/g, '' );

        if ( config.stripHtml ) {
            str = str.replace( /<[^>]*>/g, '' );
        }

        if ( config.trim ) {
            str = str.replace( /^\s+|\s+$/g, '' );
        }

        if ( config.stripNewlines ) {
            str = str.replace( /\n/g, ' ' );
        }

        if ( config.decodeEntities ) {
            _exportTextarea.innerHTML = str;
            str = _exportTextarea.value;
        }

        return str;
    };

Any help is much appreciated

Replies

  • allanallan Posts: 63,230Questions: 1Answers: 10,416 Site admin

    i have to copy the default function strip in my own code?

    That is correct. If you have a look at this line you will see that by using format.body you are replacing the default stripping function. So if you want similar abilities you'd need to add it there as well.

    Allan

  • Mike205Mike205 Posts: 2Questions: 0Answers: 0

    thanks Allan

  • ffeffe Posts: 28Questions: 4Answers: 0

    I want the exportOptions format body to export the value of an input when there is an input and call the default behavior (strip HTML tag) when there is no input.

    I have the exact same issue, and I think it is quite common for most people that they want to use a custom function on some columns, and the default strip function for the other ones.

    i have to copy the default function strip in my own code?

    That is correct.

    Well, not my favorite solution!

    @allan Is there any possibility to expose the default (strip) function, in order to call it in our own format function as some sort of fallback?

  • allanallan Posts: 63,230Questions: 1Answers: 10,416 Site admin

    Sounds like a good option - I've committed in that change and the nightly will rebuild with it shortly.

    Regards,
    Allan

  • ffeffe Posts: 28Questions: 4Answers: 0

    Wow, that was quick! Thank you, Allan

  • ffeffe Posts: 28Questions: 4Answers: 0

    @allan, I have a follow up question about the exposed data strip function. You changed the function signature again in this change.

    Is there a possibility to use the function with the given standard config? Otherwise we have a little bit the same problem as before (copy config instead of keeping things DRY)

  • allanallan Posts: 63,230Questions: 1Answers: 10,416 Site admin

    I don't quite understand what you mean I'm afraid. Can you show me how you are using that function at the moment?

    Thanks,
    Allan

  • ffeffe Posts: 28Questions: 4Answers: 0

    I want to call $.fn.dataTables.Buttons.stripData(str, config), but how do I call it with the "default" config object? The config object is not exposed, is it?

  • allanallan Posts: 63,230Questions: 1Answers: 10,416 Site admin

    I see what you mean - good point! I've made a little change to allow the function to operate with default true values if there is no config object passed in.

    Allan

This discussion has been closed.