Bootstrap数据表

时间:2016-01-17 22:41:02

标签: javascript php jquery twitter-bootstrap

这是我的数据表

<table id="example" class="table display" cellspacing="0">  
    <thead>
        <tr>
            <th>Name</th>
            <th>Mobile</th>
            <th>Interest</th>
            <th>Gender</th>
            <th>Visit</th>
            <th>Sales Person</th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <th>Name</th>
            <th>Mobile</th>
            <th>Interest</th>
            <th>Gender</th>
            <th>Visit</th>
            <th>Sales Person</th>
        </tr>
    </tfoot>
    <tbody>
    <?php

        $sql = "SELECT * from enquiry where cmpId='$idCompany'";
        $result = mysqli_query($conn, $sql);
        while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
        $time = $row['time'];
        $date = explode(" ", $time);
        $dateonly = $date[0];    
    ?>

        <tr>
            <td><a href="<?php echo $row['id']" data-target="#commentModal" data-toggle="modal" class="commentView"><?php echo $row['fname'] ?></a></td>
            <td><?php echo $row['mobile'] ?></td>
            <td><?php echo $row['interest'] ?></td>
            <td><?php echo $row['sex'] ?></td>
            <td><?php echo $dateonly ?></td>
            <td><?php echo $row['sales'] ?></td>
        </tr>
    <?php 
    }
    ?>    
    </tbody>
</table>

正如你所看到的那样,第一个td有一个标签,所以我可以打开一个模态。

Javascript for modal opening

 <script>
     $(".commentView").click(function(e){                 
            e.preventDefault();

            var view_id = $(this).attr("href");
            console.log(view_id);
            datastring = 'memberId='+view_id;
        $.ajax({
            url: "targetComment.php",
            data: datastring,
            type:"POST",
            text:"json"
         }).done(function(info) {    
        //         console.log(info);
               $('#commentModal').html(info);
         });
         });
         $('#commentModal').on('hidden.bs.modal', function () {
        //$('#memberInfo').modal('hide')
       location.reload();
    });  
</script>

我的模态完全正常且工作正常......

数据表的javascript

        <script type="text/javascript">
        $(document).ready(function() {
        $('#example').DataTable( {
        initComplete: function () {
        this.api().columns().every( function () {
            var column = this;
            var select = $('<select><option value=""></option></select>')
                .appendTo( $(column.footer()).empty() )
                .on( 'change', function () {
                    var val = $.fn.dataTable.util.escapeRegex(
                        $(this).val()
                    );

                    column
                        .search( val ? '^'+val+'$' : '', true, false )
                        .draw();
                } );

            column.data().unique().sort().each( function ( d, j ) {
                select.append( '<option value="'+d+'">'+d+'</option>' )
            } );
        } );
    }
} );

});

AS u can see in the image the bottom scroll search is completely ruined because of the a tag.

Basically the javascript function is converting td into select option and the value of the option returning everything thats inside the td tag.

你可以在图像中看到底部滚动搜索完全被破坏,因为&#34; a&#34;标签。 基本上javascript函数将td转换为select选项,并且选项的值返回td标记内的所有内容。 注意:我已链接/添加了数据表的所有js和css文件 请帮助

1 个答案:

答案 0 :(得分:0)

尝试从存储的html中提取文本。

从外部循环获取列索引并使用内部循环中的列索引来识别哪个列需要来自html的文本

var cols = this.api().columns();

cols.every(function(colIdx) {

    var column = this;

    var select = $('<select><option value=""></option></select>')
        .appendTo($(column.footer()).empty())
        .on('change', function() {
            var val = $.fn.dataTable.util.escapeRegex(
                $(this).val()
            );

            column
                .search(val ? '^' + val + '$' : '', true, false)
                .draw();
        });
    var data = column.data().unique().sort()
    data.each(function(d, j) {
        if (colIdx == 0) {
            d = $(d).text()
        }
        select.append('<option value="' + d + '">' + d + '</option>')
    });

});

DEMO