隐藏HTML表单选择打印

时间:2016-06-07 20:32:44

标签: html css datatables

我在HTML表格上使用DataTables,并希望隐藏表格标题中的所有选择和输入。

我使用过这段代码:

@media print{
    th select{
        display: none;
    }
}

但那没用。

一个例子是,在1栏中我将其作为标题:

<th>
    All Businesses
        <select name='business' id='business'>
        <option value=''>All Businesses...</option>
        <option value='Business 1'>Business 1</option>
        <option value='Business 2'>Business 2</option>
        <option value='Business 3'>Business 3</option>
    </select>
</th>

当打印看起来像这样:

Image of Headings

理想情况下,我需要一个解决方案,这意味着选择不在TH中,但与现在处于相同位置(仅在“所有业务”位下)。我这样说是因为DataTables有一个可见性列,最后使用th中的所有内容,这意味着商业名称也会显示在其中。

谢谢!

这是表头的完整代码:

$label_to_name = array();

    $tableRes = $db->query("SELECT * from staff_fields");
    $table = "<table class='table' id='table'><thead><tr><th>All Businesses<select name='business' id='business'><option value=''>All Businesses...</option><option value='Business 1'>Business 1</option><option value='Business 2'>Business 2</option><option value='Business 3'>Business 3</option></select></th>";
    $trowss = $tableRes->fetchAll(PDO::FETCH_ASSOC);

    $js = "";
    $i = 1;

    foreach($trowss as $trows){

        if($trows['type'] == "yesno"){
            $search_option = "<select id='".$trows['name']."' name='".$trows['name']."'><option value=''>Select Filter...<option value='Yes'>Yes</option><option value='No'>No</option></select>";

            $js .= "var ".$trows['name']."index = ".$i.";

    $('#".$trows['name']."').on('change',function(){
        oTable.columns(".$trows['name']."index).search($(this).val()).draw();
    });


    ";


        }else{
            $search_option = "<input type='text' name='".$trows['name']."' id='".$trows['name']."' placeholder='Type to Search...'>";

            $js .=  "var ".$trows['name']."index = ".$i.";
    $('#".$trows['name']."').on('input',function(){
        oTable.columns(".$trows['name']."index).search($(this).val()).draw();
    });

    ";


        }

        $table .= "<div class='table_heading'><th>".$trows['label']."</th>".$search_option."</div>";
        $label_to_name[$trows['label']] = $trows['name'];

        $i++;


    }
    $table .= "</tr></thead><tbody>";

HTML输出:

<table class='table' id='table'>
<thead>
    <tr>
        <th>
            All Businesses
            <select name='business' id='business'>
                <option value=''>All Businesses...</option>
                <option value='Business 1'>Business 1</option>
                <option value='Business 2'>Business 2</option>
                <option value='Business 3'>Business 3</option>
            </select>
        </th>
        <th>First Name<input type='text' name='first_name1459057776924' id='first_name1459057776924' placeholder='Type to Search...'></th>
        <th>Last Name<input type='text' name='last_name1459057788088' id='last_name1459057788088' placeholder='Type to Search...'></th>
        <th>Job Title<input type='text' name='job_title1459057796608' id='job_title1459057796608' placeholder='Type to Search...'></th>
        <th>
            Proof of Age on File
            <select id='proof_of_age_on_file1459057805910' name='proof_of_age_on_file1459057805910'>
                <option value=''>Select Filter...
                <option value='Yes'>Yes</option>
                <option value='No'>No</option>
            </select>
        </th>
        <th>Date of Birth<input type='text' name='date_of_birth1459057814082' id='date_of_birth1459057814082' placeholder='Type to Search...'></th>
        <th>Start Date<input type='text' name='start_date1459057824504' id='start_date1459057824504' placeholder='Type to Search...'></th>
        <th>
            Signed Contract in Place
            <select id='signed_contract_in_place1459057835607' name='signed_contract_in_place1459057835607'>
                <option value=''>Select Filter...
                <option value='Yes'>Yes</option>
                <option value='No'>No</option>
            </select>
        </th>
        <th>
            DBS on File
            <select id='dbs_on_file1459057844504' name='dbs_on_file1459057844504'>
                <option value=''>Select Filter...
                <option value='Yes'>Yes</option>
                <option value='No'>No</option>
            </select>
        </th>
        <th>DBS Date<input type='text' name='dbs_date1459057856098' id='dbs_date1459057856098' placeholder='Type to Search...'></th>
        <th>
            References Received
            <select id='references_received1459057869650' name='references_received1459057869650'>
                <option value=''>Select Filter...
                <option value='Yes'>Yes</option>
                <option value='No'>No</option>
            </select>
        </th>
        <th>
            Payroll Info Given to HO
            <select id='payroll_information_given_to_ho1459057881692' name='payroll_information_given_to_ho1459057881692'>
                <option value=''>Select Filter...
                <option value='Yes'>Yes</option>
                <option value='No'>No</option>
            </select>
        </th>
        <th>Leaving Date<input type='text' name='leaving_date1459057889857' id='leaving_date1459057889857' placeholder='Type to Search...'></th>
        <th>
            Left
            <select id='left1459263499753' name='left1459263499753'>
                <option value=''>Select Filter...
                <option value='Yes'>Yes</option>
                <option value='No'>No</option>
            </select>
        </th>
        <th>Next Of Kin (Name, Relationship and Contact Number)<input type='text' name='next_of_kin1459342113034' id='next_of_kin1459342113034' placeholder='Type to Search...'></th>
        <th>Medical Condition<input type='text' name='medical_condition1459342141909' id='medical_condition1459342141909' placeholder='Type to Search...'></th>
    </tr>
</thead>

1 个答案:

答案 0 :(得分:0)

您遇到的问题不在于您的css,而在于您的HTML。

您应确保<th>标记位于<table> <tr>标记内,否则浏览器将更改您的结构,@media print将不适用。

您始终可以使用w3 validator检查您的html结构是否有效。

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Test</title>
        <style type="text/css">
            @media print {
                th select {
                    display: none;
                }
            }
            
        </style>
    </head>
    <body>
        <table>
            <tr>
                <th>
                    All Businesses
                    <select name='business' id='business'>
                        <option value=''>All Businesses...</option>
                        <option value='Business 1'>Business 1</option>
                        <option value='Business 2'>Business 2</option>
                        <option value='Business 3'>Business 3</option>
                    </select>
                </th>
            </tr>
        </table>
    </body>
</html>