button value

button value

yskapellyskapell Posts: 47Questions: 14Answers: 3

Hello all,

I have this code on a button

                        {
                            text: "Copy",
                            action: function ( e, dt, node, config ) {
                                var tblData = this.rows('.selected').data();
                                //console.log(tblData);
                                var tmpData;
                                $.each(tblData, function(i, val) {
                                    tmpData = tblData[i];
                                    count = count + 1;
                                    $.post('copy.php', {
                                        data: tmpData
                                    }, function(response) {
                                        //$('#content').html(response);
                                        console.log(response);
                                        
                                    });
                                });
                                return totalCount = count; 
                            }
                        },

Can I pass the totalCount in another button ?

for examples to have something like this

{
     text: "Preiview ("+totalCount+")",
      ...
}

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406

    Yep, set a global variable and update the other buttons text on click of the first button.

  • yskapellyskapell Posts: 47Questions: 14Answers: 3

    I want to update with the row I select to copy..

  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736

    Can I pass the totalCount in another button ?

    Sounds like you want to change the button text. To do this use button().text().

    I want to update with the row I select to copy..

    I'm not sure what this means. Please elaborate.

    Kevin

  • yskapellyskapell Posts: 47Questions: 14Answers: 3

    If I copy 5 rows the Preview button to change to Preview (5), if I copy 1 to change Preview (1) etc

  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736
    Answer ✓

    Maybe something like this:

    {
        text: "Copy",
        action: function ( e, dt, node, config ) {
            // Get selected rows
            var rows = this.rows('.selected');
    
            // Set the Preview button text to the count of rows
            dt.button( x ).text( "Preview (" + rows.count() + ")" );
    
            // Get the selected rows data
            var tblData = rows.data();
            //console.log(tblData);
            var tmpData;
            $.each(tblData, function(i, val) {
                tmpData = tblData[i];
                count = count + 1;
                $.post('copy.php', {
                    data: tmpData
                }, function(response) {
                    //$('#content').html(response);
                    console.log(response);
                     
                });
            });
            return totalCount = count;
        }
    },
    

    I didn't test this. First it gets the selected rows. Uses count() to get the row count to set the other button via button().text(). Replace x with the appropriate button-selector.

    Does your copy.php accept multiple rows? If so you can send them in one request. Change var tblData = rows.data(); to var tblData = rows.data().toArray(); to get a Javascript array that can be sent.

    Kevin

Sign In or Register to comment.