Passing primary keys of selected rows to ajax function

Passing primary keys of selected rows to ajax function

jaredgerberjaredgerber Posts: 40Questions: 10Answers: 0

I'm trying to get the primary keys (TransactionID) of the selected rows so I can pass it to my ajax function & redisplay the table.

I've tried many different examples found on the site here and I'm just not having any luck -- I would appreciate any help I can get.

Here is my link:
http://174.120.222.66/~opes/admin/rpt_transactions_process.php

Here is my data (with example of how I want primary keys to be passed (e.g., &items=):
http://174.120.222.66/~opes/admin/php/processListObject.php?list=transaction&statusid=0&action=generatetransactions&items=1985,1986,1987

Here is my code:

   // ABOVE THIS POINT, #transaction editor IS ALREADY DISPLAYED. THE CODE BELOW WILL PASS 
   // SELECTED ID'S TO AJAX FUNCTION & REDISPLAY THE editor

   // PERFORM GENERATE TRANSACTIONS METHOD & REFRESH TABLE
   $('#buttonGenerate').click(function(){

        // GET SELECTED PRIMARY KEYS (TransactionID) TO PERFORM ACTIONS ON 
        // Not working :-(
        // var anSelected = fnGetSelected( editor );
        // alert( anSelected[0] );

        // Not working :-(
        //var sData = $('input', editor.fnGetNodes()).serialize();
        //alert(JSON.stringify($sData));

        // OVERRIDE FOR TESTING
        var sSelectedItems = "1985,1986,1987";  // FOR TESTING ONLY
        
         $('#transaction').dataTable().fnDestroy();
    
        // CONTROLS INLINE EDITOR / DataTables columns and columnDefs objects
        $('#transaction').DataTable( {
            dom: "<'toolbar'>Tfrtip",
            ajax: "php/processListObject.php?list=transaction&action=generatetransactions&statusid=0&items="+sSelectedItems,
            bProcessing: true,
            bServerSide: true,
            "pageLength": 25,
            "lengthMenu": [25, 50, 75, 100 ],
            columns: [
                { data: null, defaultContent: '', orderable: false },
                { data: "TransactionID" },
                { data: "Agent" },
                { data: "Amount" },
                { data: "Email" },
                { data: "Status" }
            ],
            order: [ 1, 'asc' ],
            tableTools: {
                sSwfPath: "../res/DataTables/swf/copy_csv_xls.swf",
                sRowSelect: "os",
                sRowSelector: 'td:first-child',
                aButtons: [
                    { sExtends: "select_all", editor: editor },
                    { sExtends: "select_none", editor: editor },
                    { sExtends: "xls", editor: editor , sButtonText: "Download"}
                ]
            }
        } ); // DATATABLE
    }); // buttonGenerate.click
    
    $('#buttonProcess').click(function(){
        // TRYING TO SHOW SELECTED ROWID'S FOR THIS BUTTON TOO -- TESTING
        var sData = $('input', editor.fnGetNodes()).serialize();
        alert(JSON.stringify($sData));
        //alert(indexes.join("\n"));
    }); 

    function fnGetSelected( oEditor )
    {
        return oEditor.$('tr.row_selected');  // I'M GETTING uncaught TypeError: undefined is not a function
    }
    

This question has an accepted answers - jump to answer

Answers

  • jaredgerberjaredgerber Posts: 40Questions: 10Answers: 0
    edited March 2015

    Ok -- so I'm having a little more luck here -- I'm getting it to return the selected data as an array -- using the code below. The links above are still the same -- you can see it working by selecting a couple items and clicking Remove Selected.

    Is there a way to just request the DT_RowID or TransactionID? I'm assuming that I need to loop through the data returned from fnGetSelectedData() to extract the TransactionID.

    My code:

        // HANDLE REMOVE TRANSACTIONS BUTTON
        $('#buttonRemove').click(function(){
            var oTT = $.fn.dataTable.TableTools.fnGetInstance( 'transaction' ); 
            var aSelectedItems = oTT.fnGetSelectedData();
            alert(JSON.stringify(aSelectedItems));
    
            // OVERRIDE FOR TESTING
            // var sSelectedItems = "1996,1997,1993,1994,1995"; // FOR TESTING ONLY
            // fnProcessTransactions( "removetransactions", sSelectedItems, -1 );       
            
        }); // buttonRemove.click
    

    Thanks,

    Jared

  • allanallan Posts: 61,822Questions: 1Answers: 10,127 Site admin
    Answer ✓

    Hi Jared,

    Looks like you are very close to a final solution there!

    I'm assuming that I need to loop through the data returned from fnGetSelectedData() to extract the TransactionID.

    Exactly that. You could do something like:

    var ids = $.map( oTT.fnGetSelectedData(), function ( o ) {
        return o.DT_RowId;
    } );
    

    Its a bit of a pain this I realise! I'm going to be starting work on a replacement for TableTools soon that will make this kind of thing a one liner... :-)

    Allan

  • jaredgerberjaredgerber Posts: 40Questions: 10Answers: 0

    Allan:

    Thanks for the note and the help.

    That will be cool -- you can call it TableBasic, lol.

This discussion has been closed.