如何在Bootstrap数据表中添加列值

时间:2015-03-04 10:17:58

标签: jquery html datatables jquery-datatables bootstrap-table

我使用Bootstrap DataTable来显示mysql记录。它有搜索按钮,当用户输入值时,表格仅返回相应的值。

演示:http://www.datatables.net/manual/styling/bootstrap-simple.html

现在可以添加特定列的所有数据。在上面的例子中将有工资栏。现在我需要添加所有57个人的薪水并将其显示为总计。而且当表格改变时,值也必须同时改变。

请帮助一些人:)谢谢

                  $result22=mysql_query("select * from cont_details");
                        if(mysql_num_rows($result22)>0)
                        {
                        while($row22=mysql_fetch_array($result22))
                        {?>
    <tr <?php $line_qry = mysql_query("SELECT * FROM cont_sold  WHERE cont_details_id= '".$row22['cont_details_id']."'");
if(mysql_num_rows($line_qry)>0)echo'style="color:green;"'  ?>>

        <td><?php  echo $row22['cont_no']; $cont_details_id5 = $row22['cont_details_id']; ?></td>
        <td>
            <?PHP 
                $qry7 = mysql_query("SELECT * FROM depot_charges WHERE
                `cont_details_id` = '$cont_details_id5' order by (str_to_date(`gate_in`,'%d-%m-%Y')) desc LIMIT 1");
                $row7 = mysql_fetch_array($qry7);
                $depot_details_id = $row7['depot_details_id'];

                $qry8 = mysql_query("SELECT * FROM depot_details WHERE `depot_details_id` = '$depot_details_id'");
                $row8 = mysql_fetch_array($qry8);
                echo $depot_name = $row8['depot_name'];
             ?>
         </td>
        <td>
           <?PHP $row8['depot_charge']; ?>
        </td>
        <td>
            <?PHP $row8['depot_handling']; // ?>
        </td>
        <td>
            <?PHP $row8['depot_repair'];     ?>
        </td>
        <td> 
           <?PHP  $row8['depot_place'];  ?>
        </td>
         <td><?php $row8['cont_price'];  ?></td>
        <td>
          <?php  ?>
        </td>

        <td><?php  echo $row8['cont_other'];  ?></td>

        <td>
            <?PHP $row8['total'];   ?>
        </td>
    </tr>

                        <?php 

                        } ?>

我需要添加Total列。

1 个答案:

答案 0 :(得分:3)

以下是解决方案: - 使用页脚回调函数执行此操作。 每次抽奖都会运行此回调。因此,无论何时更改表的内容,此函数都会运行并更改页面总薪水。

           

<div class="container">

<table cellpadding="0" cellspacing="0" border="0" class="dataTable" id="example">
    <thead>
        <tr>
            <th>Name</th>
            <th>Salary</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
            <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
             <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
             <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
             <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
             <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
            <td>abc</td>
            <td>500</td>
        </tr>
        <tr>
             <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
             <td>abc</td>
            <td>200</td>
        </tr>
        <tr>
             <td>abc</td>
            <td>200</td>
        </tr>
        <tr>
             <td>abc</td>
            <td>200</td>
        </tr>
        <tr>
            <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
             <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
            <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
             <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
            <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
             <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
             <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
             <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
            <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
            <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
             <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
            <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
             <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
            <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
             <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
            <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
            <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
            <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
            <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
            <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
           <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
            <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
             <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
            <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
             <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
            <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
            <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
            <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
             <td>lod</td>
            <td>100</td>
        </tr>
        <tr>
             <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
            <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
            <td>aeda</td>
            <td>100</td>
        </tr>
        <tr>
             <td>xyz</td>
            <td>100</td>
        </tr>
        <tr>
            <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
            <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
            <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
            <td>xyz</td>
            <td>100</td>
        </tr>
        <tr>
            <td>xyz</td>
            <td>100</td>
        </tr>
        <tr>
            <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
            <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
             <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
             <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
            <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
            <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
             <td>abc</td>
            <td>100</td>
        </tr>
        <tr>
            <td>abc</td>
            <td>100</td>
        </tr>
    </tbody>
    <tfoot>
        <tr>
        <td colspan='2'> <span style="float:right;"id ='totalSalary'>dsada </span> </td>
        </tr>

    </tfoot>
</table>

        </div>

<script>
    $(document).ready(function() {
        $('#example').dataTable({   
            "sPaginationType": "full_numbers",
            "footerCallback": function ( row, data, start, end, display ) {
                    var api = this.api(), data;  
                    // Remove the formatting to get integer data for summation
                    var intVal = function ( i ) {
                        return typeof i === 'string' ? i.replace(/[\$,]/g, '')*1 : typeof i === 'number' ?  i : 0;
                    };

                    // total_salary over all pages
                    total_salary = api.column( 1 ).data().reduce( function (a, b) {
                        return intVal(a) + intVal(b);
                    },0 );

                    // total_page_salary over this page
                    total_page_salary = api.column( 1, { page: 'current'} ).data().reduce( function (a, b) {
                        return intVal(a) + intVal(b);
                    }, 0 );

                    total_page_salary = parseFloat(total_page_salary);
                    total_salary = parseFloat(total_salary);
                    // Update footer
                    $('#totalSalary').html(total_page_salary.toFixed(2)+"/"+total_salary.toFixed(2));               
                },      
        });
    });

</script>

工作演示:http://jsfiddle.net/ishandemon/tb058mLq/2/