将数据从数据表发送回codeigniter

时间:2011-06-06 19:12:05

标签: php javascript jquery codeigniter datatable

我正在使用来自codeigniter控制器的jquery数据表显示数据表。我想知道的是我如何从数据表中将值发送回控制器并使用这些值从数据库中检索新记录,然后再将它们加载到页面中。

我目前的代码是

$(function(){
    $('#tableChart').dataTable( {
        // -------Function for formatting the table data elements-------
        "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {


                $.each(aData, function (i, elem){                       
                    $('td:eq('+i+')', nRow).html( '<b><font color="#40A913">'+aData[i]+'</font></b>' );
                }) 
                return nRow;
        },



            "bAutoWidth": false,
            "bProcessing": true,
            "bLengthChange": false, // Remove the show list drop down button
            "bInfo": false,         // Remove info part under the table
            "bFilter" : false,      // Remove search box
            "bDestroy": true,           // Remove table & recreate table

            "bServerSide": false,

            "sAjaxSource": "<?php echo base_url(); ?>index.php/print_db_table/get_print_db_table/<?php echo $table_name; ?>",

    });         
});

    

<div class="container"> 
    <div class="holderForTableChart">   
        <table width ="100%" cellpadding="5" cellspacing="0" class="display" id="tableChart"> 
            <thead> 
                <tr id="tableHeadder" > 
                    <?php
                        foreach($table_header as $item):
                            $header = $item->name;
                            echo '<th>'.$header.'</th>' ;
                        endforeach;
                    ?>                                              
                </tr> 
                <tr>
                <td></td>
                <td>
                <select id=""></select>
                <select id=""></select>
                </td>
                <td>
                <select id=""></select>
                <select id=""></select>
                </td>
                <td>
                <select id=""></select>
                <select id=""></select>
                </td>
                <td>
                <select id=""></select>
                <select id=""></select>
                </td>
                </tr>
            </thead> 
            <tbody> 
                <tr> 
                    <td colspan="6" class="dataTables_empty">Loading data from server</td> 
                </tr> 
            </tbody> 
        </table>    
    </div>
</div>

现在,当我在任何一个选择框中选择最小最大值时,必须将其发送到控制器,我可以将它们发送到模型并收集它们并在视图中重新加载

2 个答案:

答案 0 :(得分:1)

哼...假设这个选择框的id为#min_max_value 你的javascript代码将类似于下面的代码。 此代码将重新调用ajax并将重绘表。 在codeigniter控制器上,您将能够将此最小值捕获为$ _POST ['min_max_value']

我在fnServerData部分的网页上提到了你的问题 http://www.datatables.net/usage/callbacks

var oTable = '';
$(function(){
    oTable = $('#tableChart').dataTable( {
        // -------Function for formatting the table data elements-------
        'fnRowCallback': function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
                $.each(aData, function (i, elem){                       
                    $('td:eq('+i+')', nRow).html( '<b><font color='#40A913'>'+aData[i]+'</font></b>' );
                }) 
                return nRow;
        },
            'bAutoWidth': false,
            'bProcessing': true,
            'bLengthChange': false, // Remove the show list drop down button
            'bInfo': false,         // Remove info part under the table
            'bFilter' : false,      // Remove search box
            'bDestroy': true,       // Remove table & recreate table
            'bServerSide': false,
            'sAjaxSource': '<?php echo base_url(); ?>index.php/print_db_table/get_print_db_table/<?php echo $table_name; ?>',

            'fnServerData': function (url, data, callback) {
                // Add new data 
                data.push({'name':'min_max_value', 'value':$('#min_max_value').val()});
                $.ajax({
                    'url': url,
                    'data': data,
                    'type': 'POST',
                    'success': callback,
                    'dataType': 'json',
                    'cache': true
                });
            },
    });         

    $('#min_max_value').change(function(){
        oTable.fnDraw();
    });
});

答案 1 :(得分:0)

并且如果有人使用CodeIgniter中控制器功能的Ajax返回类型的'order'参数必须是数组数组(例如:$ {结果中的echo json_encode($result)必须是数组数组),则此代码会很有帮助。

    var manageTable;
    var base_url = "<?php echo base_url(); ?>";
    $('#putselectidhere').on('change', function (e) {

  //  $("#abc").css("display","block");

    manageTable = $('#puttableidhere').dataTable( {

        'order': [], 
        'bAutoWidth': false,
        'bProcessing': true,
        'bLengthChange': false, // Remove the show list drop down button
        'bInfo': false,         // Remove info part under the table
         'bFilter' : false,      // Remove search box
        'bDestroy': true,       // Remove table & recreate table
        'bServerSide': false,
        'sAjaxSource': base_url + 'controller/controllerfunction',

        'fnServerData': function (url, data, callback) {
            // Add new data 
            data.push({'name':'putselectidhere', 'value':$('#putselectidhere').val()});
            $.ajax({
                'url': url,
                'data': data,
                'type': 'POST',
                'success': callback,
                'dataType': 'json',
                'cache': true
            });
        },
});         







    });