Getting Selected row/rows Data

Getting Selected row/rows Data

kfirrkfirr Posts: 10Questions: 2Answers: 0
edited January 2015 in Free community support

Hi everyone,
I'm trying to pass the row data to php function that insert the rows into DB but i'm getting all off the table data
and not row that i select.
My code:

$(document).ready( function () {
                                    var table = $('#example').dataTable( {
                                        "sDom": 'T<"clear">lfrtip',
                                        "language": {
                                           "info": "<strong>Showing _START_ to _END_ of _TOTAL_ Alerts.</strong>"
                                        },
                                        "processing": true,
                                        "sAjaxSource": "<?php echo base_url() .'index.php/welcome/main_data';?>",
                                        "oTableTools": {
                                            "sRowSelect": "single",
                                            "sSwfPath": "media/swf/copy_csv_xls_pdf.swf",
                                            "aButtons": [
                                                {
                                                    "sExtends":    "ajax",
                                                    "bSelectedOnly": "true",
                                                    "sAjaxUrl" : "<?php echo base_url();?>index.php/welcome/insert",
                                                    "sButtonText": "Add To Alexandria",
                                                    "fnClick": function( nButton, oConfig ) {
                                                        var sData = this.fnGetTableData(oConfig);
                                                        $.ajax( {
                                                            "url": oConfig.sAjaxUrl,
                                                            "data": [
                                                                { "name": "tableData", "value": sData }
                                                            ],
                                                            "success": oConfig.fnAjaxComplete,
                                                            "dataType": "json",
                                                            "type": "POST",
                                                            "cache": false,
                                                            "error": function () {
                                                                alert( "Error detected when sending table data to server" );
                                                            }
                                                        } );
                                                    }
                                                },
                                                {
                                                        "sExtends":    "copy",
                                                        "sButtonText": "Copy",
                                                        "oSelectorOpts": { filter: 'applied',page: 'current'},
                                                },
                                                {
                                                    "sExtends":    "collection",
                                                    "sButtonText": "Save",
                                                    "aButtons":    [ "csv", "xls", "pdf" ]
                                                }
                                            ]
                                        }
                                    } );
                                        $('#example tbody').on( 'click', 'tr', function () {
                                               if ( $(this).hasClass('selected') ) {
                                                    $(this).removeClass('selected');
                                                }
                                                else {
                                                    table.$('tr.selected').removeClass('selected');
                                                    $(this).addClass('selected');
                                                }
                                    } );
                                } );

someone could help me please?
best regards!

Answers

  • kfirrkfirr Posts: 10Questions: 2Answers: 0

    by the way, how i can post the code so it can be readable for you?

  • mRendermRender Posts: 151Questions: 26Answers: 13
    edited January 2015

    put 3 ``` before and after your code dump

  • kfirrkfirr Posts: 10Questions: 2Answers: 0

    Thanks Modgility, i have edited my question,
    any ideas about the question's solution?

  • mRendermRender Posts: 151Questions: 26Answers: 13

    You're welcome :)

    Unfortunately, I've never tried to do what you're doing so I feel like I'm not really qualified to give you an answer. But I'll take a guess?

     "data": [
                                                                    { "name": "tableData", "value": sData }
                                                                ],
    

    It seems like you're value is sData, and the value of sData is set to be this.fnGetTableData(oConfig). Is this variable set to get all of your table's data at once? Maybe there is a way to set that data as only a specific piece of a row that you click on? Again, I'm not really sure but this is only my guess.

    Good luck!

  • kfirrkfirr Posts: 10Questions: 2Answers: 0

    Yeah you right but i also add
    "bSelectedOnly": "true",
    so i guess it should get only the selected rows, There must be someone that
    had the same issue. anyway thanks for the quick comment!

  • kfirrkfirr Posts: 10Questions: 2Answers: 0

    so i succeeded getting only the selected row/rows but the ajax
    still sending error message about the sending data.
    my currently code:

    $(document).ready( function () {
                                        var table = $('#example').dataTable( {
                                            "sDom": 'T<"clear">lfrtip',
                                            "language": {
                                               "info": "<strong>Showing _START_ to _END_ of _TOTAL_ Alerts.</strong>"
                                            },
                                            "processing": true,
                                            "sAjaxSource": "<?php echo base_url() .'index.php/welcome/main_data';?>",
                                            "oTableTools": {
                                                "sRowSelect": "single",
                                                "sSwfPath": "media/swf/copy_csv_xls_pdf.swf",
                                                "aButtons": [
                                                    {
                                                        "sExtends":    "ajax",
                                                        "bSelectedOnly": "true",
                                                        "sAjaxUrl" : "<?php echo base_url();?>index.php/welcome/insert",
                                                        "sButtonText": "Add To Alexandria",
                                                        "fnClick": function( nButton, oConfig ) {
                                                            var aData = this.fnGetTableData(oConfig); //Gets all data for selected rows
                                                            alert(aData);
                                                            $.ajax( {
                                                                "url": oConfig.sAjaxUrl,
                                                                "data": [
                                                                    { "name": "tableData", "value": aData }
                                                                ],
                                                                "success": oConfig.fnAjaxComplete,
                                                                "dataType": "json",
                                                                "type": "POST",
                                                                "cache": false,
                                                                "error": function () {
                                                                    alert( "Error detected when sending table data to server" );
                                                                }
                                                            } );
                                                        }
                                                    },
                                                    {
                                                            "sExtends":    "copy",
                                                            "sButtonText": "Copy",
                                                            "oSelectorOpts": { filter: 'applied',page: 'current'},
                                                    },
                                                    {
                                                        "sExtends":    "collection",
                                                        "sButtonText": "Save",
                                                        "aButtons":    [ "csv", "xls", "pdf" ]
                                                    }
                                                ]
                                            }
                                        } );
    
                                    } );
    
    

    my php function on the server side is only printing content of data to file:

    public function insert()
            {
                    $row_data = $_POST['tableData'];
                    file_put_contents("/tmp/text","$row_data");
            }
    

    Please help me

  • kfirrkfirr Posts: 10Questions: 2Answers: 0

    hi everyone ,
    so i figured out how to do it and now it works, im getting the selected rows
    data into my server side php function.
    i thought to share the code with you guys

    $(document).ready( function () {
                                        var table = $('#example').dataTable( {
                                            "sDom": 'T<"clear">lfrtip',
                                            "language": {
                                               "info": "<strong>Showing _START_ to _END_ of _TOTAL_ Historical Alerts.</strong>"
                                            },
                                            "processing": true,
                                            "sServerMethod": "POST",
                                            "sAjaxSource": "<?php echo base_url() .'index.php/welcome/main_data';?>",
                                            <?php
                                                    if (isset($_POST['range'])){
                                            ?>
                                            "fnServerParams": function ( aoData ) {
                                                                aoData.push( { "name": "range", "value": "<?php echo $_POST['range'];?>" } );
                                            },
                                            <?php
                                                    }
                                            ?>
                                            "oTableTools": {
                                                "sRowSelect": "multi",
                                                "sSwfPath": "media/swf/copy_csv_xls_pdf.swf",
                                                "aButtons": [
                                                    {
                                                        "sExtends":    "ajax",
                                                        "bSelectedOnly": "true",
                                                        "sAjaxUrl" : "<?php echo base_url();?>index.php/welcome/insert",
                                                        "sButtonText": "Add To Alexandria",
                                                        "fnAjaxComplete": function () {
                                                                    alert( "Alerts were added to Alexandria!" );
                                                        },
                                                        "fnClick": function( nButton, oConfig ) {
                                                            var tt = TableTools.fnGetInstance( 'example' );
                                                            var indexes = tt.fnGetSelectedIndexes();
                                                            if (indexes.length == 0){
                                                                    alert("You didn't select any rows, Try again");
                                                                    return;
                                                            }
                                                            else{
                                                                    var sData = this.fnGetTableData(oConfig); //Gets all data
                                                            }
                                                            $.ajax( {
                                                                "url": oConfig.sAjaxUrl,
                                                                "data": [
                                                                    { "name": "tableData", "value": sData }
                                                                ],
                                                                "success": oConfig.fnAjaxComplete,
                                                                "dataType": "json",
                                                                "type": "POST",
                                                                "cache": false,
                                                                "error": function () {
                                                                    alert( "Problem Passing data!" );
                                                                }
                                                            } );
                                                        }
                                                      },
                                                    {
                                                            "sExtends":    "copy",
                                                            "sButtonText": "Copy",
                                                            "oSelectorOpts": { filter: 'applied',page: 'current'},
                                                    },
                                                    {
                                                        "sExtends":    "collection",
                                                        "sButtonText": "Save",
                                                        "aButtons":    [ "csv", "xls", "pdf" ]
                                                    }
                                                ]
                                            }
                                        } );
    
    

    the only problem is that even if i'm getting the data to the server side,
    the error alert is keep showing up - there is a way to fix it?

This discussion has been closed.