简体   繁体   English

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

[英]DataTables Server Side Processing Adding html content select tag not triggering javascript

I have implemented successfully the datatables server side processing on one of my tables just that the $('.select_users').change(function(event){ doesn't work anymore on my select tab, despite the fact it shows correctly. When I try to select an option from the select tag it should action to the jquery function but it doesn't. 我已经成功地在我的一张表上实现了数据表服务器端处理,只是$('.select_users').change(function(event){在我的选择选项卡上不再起作用,尽管它可以正确显示。我尝试从select标记中选择一个选项,它应该作用于jquery函数,但事实并非如此。

EDIT 28 NOV 2014 编辑2014年11月28日

I have found that there is an issue with the events and server side processing: http://www.datatables.net/examples/advanced_init/events_live.html . 我发现事件和服务器端处理存在问题: http : //www.datatables.net/examples/advanced_init/events_live.html

So how can I convert script BLOCK #1 to match the script from the above link. 因此,如何转换脚本BLOCK #1以匹配上述链接中的脚本。

I added the 我添加了

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

And this was getting triggered. 而这正被触发。

BLOCK #1 => Jquery - on change of select it should trigger this function BLOCK#1 => Jquery-在选择更改时应触发此功能

$('.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 important part 块#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 => Datatables initialized script 块#3 =>数据表初始化脚本

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

BLOCK #4 => EDIT - before implementing server side the function was looking like this and it was working well. 块#4 =>编辑-在实现服务器端之前,该功能看起来像这样,并且运行良好。

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 
}

Follwing on from comments... 关注评论...

Using that updated code as a starting point, change $('#dataTables-ss').on('click', 'tr', function () { to $('#dataTables-ss').on('change', '.select_users', function(event){ 以更新的代码为起点,将$('#dataTables-ss').on('click', 'tr', function () {更改$('#dataTables-ss').on('change', '.select_users', function(event){

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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