Datatables 数据库-隐藏数据直到搜索

Datatables 数据库-隐藏数据直到搜索,datatables,Datatables,我希望我的页面最初只加载一个搜索框,然后当有人开始搜索时,也就是当表格出现时 <script> $(document).ready(function() { $('#example').DataTable( { responsive: true } ); } ); </script> <table id="example" class="display" style="width:100%"&

我希望我的页面最初只加载一个搜索框,然后当有人开始搜索时,也就是当表格出现时

<script>
    $(document).ready(function() {
        $('#example').DataTable( {
            responsive: true
        } );
    } );
</script>

<table id="example" class="display" style="width:100%">
    <thead>
        <tr>
            <th>Name</th>
            <th>Description</th>
            <th class="none">Branch</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Test 1</td>
            <td>Test test test</td>
            <td>ABC</td>
        </tr>
        <tr>
            <td>Test 2</td>
            <td>Test test test</td>
            <td>DEF</td>
        </tr>
        <tr>
            <td>Test 3</td>
            <td>Test test test</td>
            <td>GHI</td>
        </tr>          
    </tbody>    
</table>
我的示例表如下:

<script>
    $(document).ready(function() {
        $('#example').DataTable( {
            responsive: true
        } );
    } );
</script>

<table id="example" class="display" style="width:100%">
    <thead>
        <tr>
            <th>Name</th>
            <th>Description</th>
            <th class="none">Branch</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Test 1</td>
            <td>Test test test</td>
            <td>ABC</td>
        </tr>
        <tr>
            <td>Test 2</td>
            <td>Test test test</td>
            <td>DEF</td>
        </tr>
        <tr>
            <td>Test 3</td>
            <td>Test test test</td>
            <td>GHI</td>
        </tr>          
    </tbody>    
</table>

<script>
    $(document).ready(function() {
        $('#example').DataTable( {
            responsive: true
        } );
    } );
</script>

<table id="example" class="display" style="width:100%">
    <thead>
        <tr>
            <th>Name</th>
            <th>Description</th>
            <th class="none">Branch</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Test 1</td>
            <td>Test test test</td>
            <td>ABC</td>
        </tr>
        <tr>
            <td>Test 2</td>
            <td>Test test test</td>
            <td>DEF</td>
        </tr>
        <tr>
            <td>Test 3</td>
            <td>Test test test</td>
            <td>GHI</td>
        </tr>          
    </tbody>    
</table>

是的,它可以使用ajax调用完成,我想您可以使用php。 例如,您可以在每次按下任何字符、使用结果填充表格或使用按钮搜索输入字段中键入的关键字时生成表格。 ajax和php示例:

<script>
    $(document).ready(function() {
        $('#example').DataTable( {
            responsive: true
        } );
    } );
</script>

<table id="example" class="display" style="width:100%">
    <thead>
        <tr>
            <th>Name</th>
            <th>Description</th>
            <th class="none">Branch</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Test 1</td>
            <td>Test test test</td>
            <td>ABC</td>
        </tr>
        <tr>
            <td>Test 2</td>
            <td>Test test test</td>
            <td>DEF</td>
        </tr>
        <tr>
            <td>Test 3</td>
            <td>Test test test</td>
            <td>GHI</td>
        </tr>          
    </tbody>    
</table>
var search_string = $('#id_of_the_input_box').val();
$.ajax({
    url:"php_file.php",
    method:"POST",
    data:{action:action},
    dataType:"json",
    success:function(data){
    console.log(data);
}
以及使用PDO的php代码

<script>
    $(document).ready(function() {
        $('#example').DataTable( {
            responsive: true
        } );
    } );
</script>

<table id="example" class="display" style="width:100%">
    <thead>
        <tr>
            <th>Name</th>
            <th>Description</th>
            <th class="none">Branch</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Test 1</td>
            <td>Test test test</td>
            <td>ABC</td>
        </tr>
        <tr>
            <td>Test 2</td>
            <td>Test test test</td>
            <td>DEF</td>
        </tr>
        <tr>
            <td>Test 3</td>
            <td>Test test test</td>
            <td>GHI</td>
        </tr>          
    </tbody>    
</table>
$search_string = $_POST["string"];
$result = '';
$query = $connection->prepare(" select * from table where name = '%".search_string."' ";
$query->execute();
$query_results->query->fetchAll;
if($query->rowCount() > 0) {
   $result = '
         <script>
            $(document).ready(function() {
               $('#example').DataTable( {
                  responsive: true
               });
            });
            <table id="example" class="display" style="width:100%">
              YOUR TABLE ROWS HERE
            </table>
   ';

   // foreach loop to populate the table example
   foreach ($query_results as $row) {
        $result.= '
            <tr>
              <td>'.$row["table_column_name_1"].'</td>
              <td>'.$row["table_column_name_1"].'</td>
              <td>'.$row["table_column_name_1"].'</td>
              <td>'.$row["table_column_name_1"].'</td>
        '
   } else {
            echo "No results";
          }
}

// echo the generated table as ajax response
echo $result;

希望您已经掌握了这个示例的想法。

要找到您需要的答案,请看一下如何最小化、完成和验证示例嗨,我对PHP不太熟悉,但实际上我从SharePoint列表中提取了大约60个项目。我的调用看起来像这样-函数loadzipcodestate{var call=$。ajax{url:_spPageContextInfo.webAbsoluteUrl++/_api/Web/list/GetByTitle'All BEMS Applications'/items?+$select=Title,System_x0020_Name,ID,Tags,Description&$top=5000,type:GET,dataType:json,headers:{Accept:application/json;odata=verbose};这是否适用于您的方法?基本上,您是以json格式获取数据,并且您的数据似乎来自选定的项目,我不确定$select=Title,System…是一种查询还是只是一种过滤现有数据。如果您不知道sharepoint如何处理数据,您可以尝试使用mozil的开发人员工具查看结果la或chrome浏览器,了解json结构,并将json传递给我的方法。可能是一个链接,查看它的实况也会对我有所帮助。
<script>
    $(document).ready(function() {
        $('#example').DataTable( {
            responsive: true
        } );
    } );
</script>

<table id="example" class="display" style="width:100%">
    <thead>
        <tr>
            <th>Name</th>
            <th>Description</th>
            <th class="none">Branch</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Test 1</td>
            <td>Test test test</td>
            <td>ABC</td>
        </tr>
        <tr>
            <td>Test 2</td>
            <td>Test test test</td>
            <td>DEF</td>
        </tr>
        <tr>
            <td>Test 3</td>
            <td>Test test test</td>
            <td>GHI</td>
        </tr>          
    </tbody>    
</table>