简体   繁体   中英

Filling a dataTable with data from another one using jQuery in CodeIgniter

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM