DataTables服务器端处理添加html内容选择标记不触发javascript

时间:2014-11-26 12:49:04

标签: javascript php jquery jquery-datatables

我已经在我的一个表上成功实现了数据表服务器端处理,只是$('.select_users').change(function(event){在我的选择选项卡上不再工作,尽管它显示正确。当我尝试从select标签中选择一个选项时,它应该对jquery函数起作用,但它没有。

2014年11月28日编辑

我发现事件和服务器端处理存在问题: http://www.datatables.net/examples/advanced_init/events_live.html

那么如何转换脚本BLOCK #1以匹配上述链接中的脚本。

我添加了

$('#dataTables-ss').on('click', 'tr', function () {
    var name = $('td', this).eq(0).text();
    alert( 'You clicked on '+name+'\'s row' );
} );

这被触发了。

BLOCK#1 => Jquery - 在更改select时它应该触发此函数

$('.select_users').change(function(event){
    var selected_value = this.value;
    var id = $(this).find('option:selected').attr('class');
    var id = id.split('-');
    var select_id = id[1];
    $.ajax({
        type: 'post',
        url: 'includes/ajax.html',
        data: {
                action: 'confirm_notconfirm_subscribers',
                selected_value: selected_value,
                select_id: select_id
              },
        success: function(data) {
            $('#message').html(data).fadeIn(1000).fadeOut(1000);
        },
        error: function() {
            $("#result").html('There was an error');
        }               
    });
});

BLOCK#2 => PHP的重要部分

public function read_subscriber_table() {
    $table = 'subscribers';
    $primaryKey = 'ID';
    $columns = array(
        array( 'db' => 'name', 'dt' => 1 ),
        array( 'db' => 'email', 'dt' => 2 ),
        array( // issue is here in the third array where I create the select string.
            'db' => 'confirmed', 
            'dt' => 3,
            'formatter' => function( $d, $row ) {
                if ($d == '1') {
                    $confirmed = 'Confirmed';
                } else {
                    $confirmed = 'Not Confirmed';
                }

                if ($d !== '1') { $selected = 'selected'; } else { $selected = ''; }
                $string = '<select class="select_users form-control" name="status-users">
                    <option class="opt-'.$row["ID"].'" value="1"  '.$selected.'>Confirmed</option>
                    <option class="opt-'.$row["ID"].'" value="0" '.$selected.'>Not Confirmed</option>
                </select>';
                return $string;
            }
        ),
        array( 
            'db' => 'date', 
            'dt' => 4,
            'formatter' => function( $d, $row ) {
                return date( 'd-m-Y', strtotime($d));
            }
        ),
        array( 
            'db' => 'ID', 
            'dt' => 5,
            'formatter' => function( $d, $row ) {
                return '<input type="checkbox" class="checkbox_subscribers" value="'.$d.'" >';
            }
        )
    );
    $sql_details = $this->dtss_processing();
    require( 'ssp.class.php' );
    $result = SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns );
    $start=$_REQUEST['start'] + 1;
    foreach($result['data'] as &$res){
        $res[0]=(string)$start;
        $start++;
    }
    echo json_encode($result);
}

BLOCK#3 =&gt;数据表初始化脚本

$(document).ready(function() {
    $('#dataTables-ss').dataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": "includes/data_tables.html?action=read_subscriber_table",
        "aaSorting": [[4, 'desc']]
    } );
} );

BLOCK#4 =&gt;编辑 - 在实现服务器端之前,该功能看起来像这样,并且运行良好。

public function read_subscriber_table() {
    $col_names = array('ID', 'name', 'email', 'confirmed', 'date');
    $this->read_table('subscribers', $col_names);
    $i = 0;
    foreach ($this->results as $item) {
        $i++;
        if ($item->confirmed == '1') {
            $confirmed = 'Confirmed';
        } else {
            $confirmed = 'Not Confirmed';
        }
        if ($item->confirmed !== '1') { $selected = 'selected'; } else { $selected = ''; }
    ?>
        <tr id="tablerow-<?php echo $item->ID; ?>">
            <td><?php echo $i; ?></td>
            <td><?php echo $item->name; ?></td>
            <td><?php echo $item->email; ?></td>
            <td>                    
                <select class="select_users form-control" name="status-users">
                    <option class="opt-<?php echo $item->ID; ?>" value="1" <?php echo $selected ?>>Confirmed</option>
                    <option class="opt-<?php echo $item->ID; ?>" value="0" <?php echo $selected ?>>Not Confirmed</option>
                </select>
            </td>
            <td><?php echo $item->date; ?></td>
            <td><input type="checkbox" class="checkbox_subscribers" value="<?php echo $item->ID; ?>" ></td>
        </tr>
    <?php
    }
    ?>
<?php 
}

1 个答案:

答案 0 :(得分:2)

继续发表评论... 使用更新后的代码作为起点,将$('#dataTables-ss').on('click', 'tr', function () {更改为$('#dataTables-ss').on('change', '.select_users', function(event){