get data from input element in table after change page

get data from input element in table after change page

omar123omar123 Posts: 12Questions: 1Answers: 0

Hi,
I tri to use my function to export all input elements in datatables (same function of datatables with few changes), but but the problème is that whene i change an element and change page, if clic export the vale of element changed is not correcte. i have the initial data.
Can you help me ?
sorry about my English :)

code :

$.fn.dataTable.Api.register( 'buttons.exportData()', function ( options ) {
if ( this.context.length ) {
var dt = new $.fn.dataTable.Api(this.context[0]);

            var _exportTextarea = $('<textarea/>')[0];
            var config = $.extend( true, {}, {
                rows:           null,
                columns:        '',
                modifier:       {
                    search: 'applied',
                    order:  'applied'
                },
                orthogonal:     'display',
                stripHtml:      true,
                stripNewlines:  true,
                decodeEntities: true,
                trim:           true,
                format:         {
                    header: function ( d ) {
                        return strip( d );
                    },
                    footer: function ( d ) {
                        return strip( d );
                    },
                    body: function ( d ) {
                        return strip( d );
                    }
                }
            }, options );

            var strip = function ( str ) {
                if ( typeof str !== 'string' ) {
                    return str;
                }

                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;
                }
                if ($.isNumeric(str.replace( / /g, '' ))) {
                    //str = str.replace( / /g, '' );
                    str = str.replace(".", ',' );
                }
                return str;
            };


            var header = dt.columns( config.columns ).indexes().map( function (idx) {
                var el = dt.column( idx ).header();
                return config.format.header( el.innerHTML, idx, el );
            } ).toArray();

            var footer = dt.table().footer() ?
                dt.columns( config.columns ).indexes().map( function (idx) {
                    var el = dt.column( idx ).footer();
                    return config.format.footer( el ? el.innerHTML : '', idx, el );
                } ).toArray() :
                null;

            var rowIndexes = dt.rows( config.rows, config.modifier ).indexes().toArray();
            var selectedCells = dt.cells( rowIndexes, config.columns );
            var cells = selectedCells
                .render( config.orthogonal )
                .toArray();
            var cellNodes = selectedCells
                .nodes()
                .toArray();

            var columns = header.length;
            var rows = columns > 0 ? cells.length / columns : 0;
            var body = new Array( rows );
            var cellCounter = 0;

            for ( var i=0, ien=rows ; i<ien ; i++ ) {
                var row = new Array( columns );
                for ( var j=0 ; j<columns ; j++ ) {
                    var val = cellNodes[cellCounter] ? cellNodes[cellCounter].innerHTML : cells[cellCounter];
                    val = "<div>" + val + "</div>";
                                            // priority is for select if not existe, get data from textbox if existe, if not get data from cell
                    var selectChield = $(val).children('select')[0];
                    if (selectChield) {
                        val = $(selectChield).find('option:selected').text();

                    } else {
                        selectChield = $(val).find('input').filter(':visible').not(':input[readonly]')[0];
                        if (selectChield) {
                            val =  $(selectChield).val();
                        }
                    }
                    row[j] = config.format.body( val, i, j, cellNodes[ cellCounter ] );

                    cellCounter++;
                }

                body[i] = row;
            }

            return {
                header: header,
                footer: footer,
                body:   body
            };
        }
    } );

Answers

  • kthorngrenkthorngren Posts: 20,302Questions: 26Answers: 4,769

    Checkout the 2nd FAQ here:
    https://datatables.net/faqs/index#Most-common-FAQs

    Kevin

  • omar123omar123 Posts: 12Questions: 1Answers: 0

    thanks,
    I already checked the forum but, no response for my probleme.
    can you please help me, i am realy ?
    thansk.

  • allanallan Posts: 61,716Questions: 1Answers: 10,108 Site admin

    I'm not sure why you've copied and pasted the export API method from the Buttons code above.

    Can you please link to a page showing the issue so we can help debug it. Otherwise we are just guessing and that's why a link to the page in question is required in the forum rules.

    Allan

  • omar123omar123 Posts: 12Questions: 1Answers: 0

    I've copied the methode because the result of the export is not correcte in my case,
    i have the datat table with text input inside, whene I clic to export, I lose the data of inputs in all pages exept the current.

  • tangerinetangerine Posts: 3,350Questions: 37Answers: 394

    Can you please link to a page showing the issue so we can help debug it. Otherwise we are just guessing and that's why a link to the page in question is required in the forum rules.

  • omar123omar123 Posts: 12Questions: 1Answers: 0
    edited February 2017

    Here is an example of my problème : http://live.datatables.net/sebimipa/24/edit
    please edite the 1st ligne and the 11th, and export, the value of the inputs is not exported.
    You can delete my function and try with the default export function of Datatables, the results are same.
    Thanks.

  • allanallan Posts: 61,716Questions: 1Answers: 10,108 Site admin

    Thank you for the link. I understand the issue now.

    You would need to define a custom body export function (which is described in this example). That function would need to get the values from the DOM input elements, since DataTables will not do that for you.

    Allan

  • omar123omar123 Posts: 12Questions: 1Answers: 0

    Than you for response, but i stille have a probleme.
    even if I use the function 'body: function ( data, row, column, node )' the node is un undefined object in the unvisited pages.

    if I trace the table by iteration in JSP page the result is good, but whene i use the attibute "data" of datatables, i have the probleme.

  • allanallan Posts: 61,716Questions: 1Answers: 10,108 Site admin

    What version of buttons are you using? The node parameter was added in v1.2.2 (noted in the buttons.exportData() documentation).

    Allan

  • omar123omar123 Posts: 12Questions: 1Answers: 0

    I use the version "datatables.net-buttons": "1.2.1", if I want change, must I change version of all components or not ?
    thanks

  • allanallan Posts: 61,716Questions: 1Answers: 10,108 Site admin

    Yes. 1.2.1 is older than 1.2.2, which as I noted above is when the node parameter was added.

    Allan

  • allanallan Posts: 61,716Questions: 1Answers: 10,108 Site admin

    Sorry - I misread your question. No you don't need to update everything. I would suggest that it is a good idea to use the latest versions of all software, but its up to you.

    Allan

  • omar123omar123 Posts: 12Questions: 1Answers: 0

    ok, thank you i'll try and send a feedback

  • omar123omar123 Posts: 12Questions: 1Answers: 0

    I test with the version 1.2.2, but the nodes in the function 'body' are defined juste in the visited page, if the page is not visited the nodes are undefined and the parametre 'data' return the innerHTML of 'TD'.
    the version 1.2.2 give me a partiel solution.
    Thanks

  • omar123omar123 Posts: 12Questions: 1Answers: 0

    The probleme is always her, I thinked, it's resolved but whene I launche the export option, I have in the parameter data the data of incremented <td> but the node give has a as value the node of visited page.
    Ex : if I visite the 1st page and the 17th page, I have the 1st page is exported correctly but the 2nd is replaced with the 17th page.

  • omar123omar123 Posts: 12Questions: 1Answers: 0

    a : data
    b : row
    c : column
    d : node

    the value returned in data is different of the value in the node
    Can you please tell me if there is a solution for this probleme.

  • omar123omar123 Posts: 12Questions: 1Answers: 0

    Can any one helps me ?

  • allanallan Posts: 61,716Questions: 1Answers: 10,108 Site admin

    Can you link to a page showing the issue so it can be debugged please?

    Thanks,
    Allan

  • omar123omar123 Posts: 12Questions: 1Answers: 0
    edited March 2017

    Here is the page http://live.datatables.net/sebimipa/87/edit
    But I cant reproduce my case, in this example I export data correctly, so I don't know what is the problem ..!!

    Please, can you confirm if these dependencies from my bower.json are correct ?

    "jszip": "2.5.0",
    "datatables.net": "1.10.12",
    "datatables.net-bs": "1.10.12",
    "datatables.net-colreorder": "~1.3.0",
    "datatables.net-colreorder-bs": "~1.3.0",
    "datatables.net-buttons": "1.2.2",
    "datatables.net-buttons-bs": "1.2.1",
    "datatables.net-select": "1.1.0",
    "datatables.net-select-bs": "1.1.0",
    

    Thanks,
    Omar

  • allanallan Posts: 61,716Questions: 1Answers: 10,108 Site admin

    It would be worth updating your software to the latest versions. Those versions that you list are all a bit behind (Buttons is 1.2.4 now for example).

    Allan

This discussion has been closed.