douju5933 2016-01-06 07:53
浏览 32
已采纳

php输入后如何让js运行?

i tried to add datatables to my web, but i have a problem that is the code will not apply the js code after finishing input which have HTML and PHP. This is my js code:

$(document).ready(function() {
$('#example').DataTable( {
    responsive: {
        details: {
            display: $.fn.dataTable.Responsive.display.modal( {
                header: function ( row ) {
                    var data = row.data();
                    return 'Details for '+data[0]+' '+data[1];
                }
            } ),
            renderer: function ( api, rowIdx, columns ) {
                var data = $.map( columns, function ( col, i ) {
                    return '<tr>'+
                            '<td>'+col.title+':'+'</td> '+
                            '<td>'+col.data+'</td>'+
                        '</tr>';
                } ).join('');

                return $('<table class="table"/>').append( data );
            }
        }
    }
} );
} );

And this is my html code:

<thead>
        <tr>
            <th>STT</th>
            <th>Tên sản phẩm</th>
            <th>Ngày sản xuất</th>
            <th>Mã sản phẩm</th>
            <th>Vị trí lắp đặt</th>
            <th>Chủng loại</th>
            <th>Nhóm</th>
            <th>Dự án</th>
            <th>Nhà sản xuất</th>
            <th>Miêu tả sản phẩm</th>
            <th>Lỗi</th>
            <th>Cấp độ</th>
            <th>Miêu tả lỗi</th>
            <th>Người khắc phục</th>
            <th>Thời gian khắc phục</th>
            <th>Chi tiết lỗi</th>
            <th style="width:1%; text-align:center">Hành động</th>
        </tr>
    </thead> 
    <tbody>

And this is the php code

$stt= 1;
    $sql= "SELECT * from fixed ORDER BY sta ASC, level ASC";
    //thuc hien cau lenh voi bien conn lay tu file connection.php
    $query= mysqli_query($conn, $sql);
    while ($data=mysqli_fetch_array($query)){
?>
    <tr>
        <td scope="row"><?php echo $stt++?></td>
        <td><?php echo $data["ten_sp"]?></td>
        <td><?php echo $data["ngay_sx"]?></td>
        <td><?php echo $data["ma_sp"]?></td>
        <td><?php echo $data["vitrilapdat"] ?></td>
        <td><?php echo $data["chungloai"] ?></td>
        <td><?php echo $data["nhom"] ?></td>
        <td><?php echo $data["d_an"] ?></td>
        <td><?php echo $data["nhasx"] ?></td>
        <td><?php echo $data["mieuta"] ?></td>
        <td><?php echo $data["loi"] ?></td>
        <td>
            <?php
            if($data["level"] == 1){
                echo '<p style="color: red;">Khẩn cấp</p>';
            }else if($data["level"] == 2){
                echo '<p style="color: #00b300;">Quan trọng</p>';
            } else if($data["level"] == 3){
                echo '<p style="color: #0000cc;">Cần lưu ý</p>';
            }
                        ?>  
        </td>
        <td><?php echo $data["mieutaloi"] ?></td>
        <td><?php echo $data["nguoikhacphuc"] ?></td>
        <td><?php echo $data["thoigian"] ?></td>
        <td><?php echo $data["chitiet"] ?></td>
        <td><a href="prod_fixed_editinfo.php?id=<?php echo $data["id"]?>" class="btn btn-info btn-sm" role="button">
            <?php 
            if($data["sta"] == 1){

                    echo "Un-read";
                    }else{
                        echo "Read";
                        } ?></a></td>

        <td><a href="prod_fixed_delete.php?id=<?php echo $data["id"]?>" class="btn btn-danger btn-sm" role="button">Xóa</a></td>
    </tr>
    <?php
    }
    ?>

And this is the result i get: enter image description here

I want the result like this: enter image description here

This is the code of this pic: (The same js and style as the first pic)

<thead>
        <tr>
            <th>First name</th>
            <th>Last name</th>
            <th>Position</th>
            <th>Office</th>
            <th>Age</th>
            <th>Start date</th>
            <th>Salary</th>
            <th>Extn.</th>
            <th>E-mail</th>
            <th>Extn.</th>
            <th>E-mail</th>
            <th>Extn.</th>
            <th>E-mail</th>
            <th>Extn.</th>
            <th>E-mail</th>
            <th>Extn.</th>
            <th>E-mail</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Tiger</td>
            <td>Nixon</td>
            <td>System Architect</td>
            <td>Edinburgh</td>
            <td>61</td>
            <td>2011/04/25</td>
            <td>$320,800</td>
            <td>5421</td>
            <td>t.nixon@datatables.net</td>
            <td>5421</td>
            <td>t.nixon@datatables.net</td>
             <td>5421</td>
            <td>t.nixon@datatables.net</td>
            <td>5421</td>
            <td>t.nixon@datatables.net</td>
             <td>5421</td>
            <td>t.nixon@datatables.net</td>
        </tr>
        <tr>
            <td>Vivian</td>
            <td>Harrell</td>
            <td>Financial Controller</td>
            <td>San Francisco</td>
            <td>62</td>
            <td>2009/02/14</td>
            <td>$452,500</td>
            <td>9422</td>
            <td>v.harrell@datatables.net</td>
            <td>9422</td>
            <td>v.harrell@datatables.net</td>
             <td>5421</td>
            <td>t.nixon@datatables.net</td>
            <td>5421</td>
            <td>t.nixon@datatables.net</td>
             <td>5421</td>
            <td>t.nixon@datatables.net</td>
        </tr>
        <tr>
            <td>Michael</td>
            <td>Bruce</td>
            <td>Javascript Developer</td>
            <td>Singapore</td>
            <td>29</td>
            <td>2011/06/27</td>
            <td>$183,000</td>
            <td>5384</td>
            <td>m.bruce@datatables.net</td>
            <td>9422</td>
            <td>v.harrell@datatables.net</td>
             <td>5421</td>
            <td>t.nixon@datatables.net</td>
            <td>5421</td>
            <td>t.nixon@datatables.net</td>
             <td>5421</td>
            <td>t.nixon@datatables.net</td>
        </tr>
        <tr>
            <td>Donna</td>
            <td>Snider</td>
            <td>Customer Support</td>
            <td>New York</td>
            <td>27</td>
            <td>2011/01/25</td>
            <td>$112,000</td>
            <td>4226</td>
            <td>d.snider@datatables.net</td>
            <td>9422</td>
            <td>v.harrell@datatables.net</td>
             <td>5421</td>
            <td>t.nixon@datatables.net</td>
            <td>5421</td>
            <td>t.nixon@datatables.net</td>
             <td>5421</td>
            <td>t.nixon@datatables.net</td>
        </tr>
    </tbody>

I think the problem here is the js code will not run after input all the info from php code, but i dont know how to fix that, can you help?

  • 写回答

2条回答 默认 最新

  • duanjiang7505 2016-01-06 08:14
    关注

    1/ First, check again the element id #example, do you have it or not, it must be some things like:

    <table id="example">
    
     <thread>
      <tr>
       <th></th>
      </tr>      
     </thread>
    
     <tbody>
      <tr>
       <td></td>
      </tr>      
     </tbody>
    
    </table>
    

    2/ Second, make sure you add the link to DataTable js to the page with the right path, for example:

    <link href="css/dataTables.bootstrap.css" rel="stylesheet">
    
    <link href="css/dataTables.responsive.css" rel="stylesheet">
    
    <script src="js/jquery.dataTables.min.js"></script>
    
    <script src="js/dataTables.bootstrap.min.js"></script>
    
    <script src="js/yourscript.js"></script>
    

    3/ Third, try to remove the php code, create a normal table, to see if the js work or not..?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制