如何在Datatable Ajax调用后加载远程模式

时间:2016-02-29 11:27:50

标签: javascript jquery ajax datatables

如何向此代码添加事件处理程序。 我希望每行打开一个远程引导模式, 当前数据表结果不会触发jquery来打开远程模态。

//输出

<table id="users" cellspacing="0" border="0"
   class="table table table-condensed sortable table-striped table-bordered datatables dataTable no-footer"
   role="grid" aria-describedby="users_info" style="width: 1129px;">
<thead>
<tr role="row">
    <th class="col-md-3 sorting_asc" tabindex="0" aria-controls="users" rowspan="1" colspan="1"
        style="width: 243px;" aria-sort="ascending" aria-label="User ID: activate to sort column ascending">User ID
    </th>
    <th class="col-md-3 sorting" tabindex="0" aria-controls="users" rowspan="1" colspan="1" style="width: 250px;"
        aria-label="User Name: activate to sort column ascending">User Name
    </th>
    <th class="col-md-3 sorting" tabindex="0" aria-controls="users" rowspan="1" colspan="1" style="width: 250px;"
        aria-label="Email: activate to sort column ascending">Email
    </th>
    <th class="col-md-3 sorting" tabindex="0" aria-controls="users" rowspan="1" colspan="1" style="width: 246px;"
        aria-label="Created At: activate to sort column ascending">Created At
    </th>
    <th class="col-md-3 sorting" tabindex="0" aria-controls="users" rowspan="1" colspan="1" style="width: 79px;"
        aria-label="table.actions: activate to sort column ascending">table.actions
    </th>
</tr>
</thead>

<tbody>

<tr role="row" class="odd">
    <td class="sorting_1">1</td>
    <td>jess</td>
    <td>jess@example.com</td>
    <td>2016-01-24 19:47:26</td>
    <td><a href="#affiliateInfoModal" role="button" class="btn" data-toggle="modal"
           data-load-remote="/get_affiliate_profile/1" data-remote-target="#affiliateInfoModal .modal-body"><i
                    class="fa fa-info fa-lg"></i></a></td>
</tr>
</tbody>

  <!-- Modal -->
<div class="modal fade" id="affiliateInfoModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
     aria-hidden="true">
    <div class="modal-dialog-affiliate-info">
        <div class="modal-content">

            <div class="modal-body">

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

// Datatables脚本和Modal远程加载器

<script type="text/javascript">
        $(document).ready(function() {
            oTable = $('#users').DataTable({
                "processing": true,
                "serverSide": true,
                "ajax": "/testdata22",
                "columns": [
                    {data: 'id', name: 'id', sorttable: true},
                    {data: 'username', name: 'username' , sorttable: true},
                    {data: 'email', name: 'email' , sorttable: true},
                    {data: 'created_at', name: 'created_at', sorttable: true},
                    {data: 'actions', name: 'actions', searchable: false}
                ]
            });
        });
    </script>




  <script>
        $('[data-toggle="modal-affiliate-info"]').click(function (e) {
            e.preventDefault();
            var url = $(this).attr('href');
            //var modal_id = $(this).attr('data-target');
            $.get(url, function (data) {
                $(data).modal();
            });
        });
    </script>

1 个答案:

答案 0 :(得分:3)

这取决于您使用的DataTables版本。如果您使用的是旧版(&lt; 1.10),请使用fnInitComplete,否则(&gt; 1.10)使用initComplete

例如(版本&lt; 1.10):

$(document).ready(function() {
        oTable = $('#users').DataTable({
            "processing": true,
            "serverSide": true,
            "ajax": "/testdata22",
            "columns": [
                {data: 'id', name: 'id', sorttable: true},
                {data: 'username', name: 'username' , sorttable: true},
                {data: 'email', name: 'email' , sorttable: true},
                {data: 'created_at', name: 'created_at', sorttable: true},
                {data: 'actions', name: 'actions', searchable: false}
            ],
            "fnDrawCallback": function(oSettings){
                clickable();
            }
        });
    });

function clickable(){
    $('[data-toggle="modal-affiliate-info"]').click(function (e) {
        e.preventDefault();
        var url = $(this).attr('href');
        //var modal_id = $(this).attr('data-target');
        $.get(url, function (data) {
            $(data).modal();
        });
    });
}

我使用了一个非常相似的设置,它可以毫无障碍地工作。