2

I use CodeIgniter and I have two dataTables (in two separate views. same js, model and controller files)

I can fill the first table with a SELECT query and it works normally.

My problem is that I need data (for the query) from the first table (using a click to select which row) to fill the second one. I couldn't make it work yet. I already executed the required SELECT query in phpMyAdmin and it works.

controller file

function refreshT2(){
    $id = $_POST['id'];
    $id = stripslashes($id);
    $id = mysql_real_escape_string($id);
    $this->load->model('model');
    $data=$this->model->getAllById($id);
    echo json_encode($data);
}

js file

$(document).ready( function () {
    $("#table1").addClass("started");
    $("#table2").addClass("started");

    var t1Source = "controller/refreshT1";
    var t2Source = "controller/refreshT2";

    var oTablet1=$('#table1').dataTable({
        "sScrollX": "100%",
        "sPaginationType":"full_numbers",
        "bJQueryUI":true,
        "sDom": 'R<"H"lfr>t<"F"ip>',
        "bDeferRender": true,
        "bProcessing": true,
        "bServerSide": true,
        "aaSorting": [[ 0, "desc" ]],
        "sAjaxSource": ft1Source
    });

    $("#table1 tbody").click(function(event) {
        var iPos=oTablet1.fnGetPosition(event.target.parentNode);
        var aData=oTablet1.fnGetData(iPos);

        var id=aData[2];

        $('input[name=id]').val(id);
    /* When I click on the first dataTable the field 'id' is filled properly */

        var oTableft2 = $('#table2').dataTable({
            "sScrollX": "100%",
            "sPaginationType":"full_numbers",
            "bJQueryUI":true,
            "sDom": 'R<"H"lfr>t<"F"ip>',
            "bDeferRender": true,
            "bProcessing": true,
            "bRetrieve": true,
            "bDestroy": true,
            "bServerSide": true,
            "aaSorting": [[ 0, "desc" ]],
            "sAjaxSource": t2Source
        });
    } );
}

If I need to provide more information/code please tell me.

Thank you for your time.

EDIT: when I switch $id = $_POST['id']; with a real value it works. How can I retrieve this data?

I don't want to use a "button+action" solution. If it works inside the js I won't need any send/receive method. what I need is how to pass a parameter to the second table.

4
  • 1
    your problem is pass the value from server side right? Commented Aug 30, 2013 at 11:15
  • 1
    lets debug it for you, is your click on table1's row working properly, and is the data / id that you are sending to server for other view to update being received properly. if yes, then what's coming our from db, and what format, then are you being able to parse and bind it with table2? debug it Commented Aug 30, 2013 at 12:45
  • I just found out that the problem lies in the {$id = $_POST['id'];} part. I switched it to a real value and it worked Commented Aug 30, 2013 at 12:55
  • It seems that I need to pass my id using another method. Commented Aug 30, 2013 at 13:23

1 Answer 1

1

It turns out that I needed to add the id to the link in sAjaxSource like this:

"sAjaxSource": t2Source+"?id="+id

and use $_GET and not $_POST in my controller.

Sign up to request clarification or add additional context in comments.

1 Comment

here is a sample cms that uses datatables with server side processing for your reference . github.com/gencms/sample-gencms

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.