Javascript根据列值过滤数据表

时间:2018-07-31 17:42:42

标签: javascript datatables

我正在将数组中的数据呈现为数据表。该表分为三列:ImageName,Location和Brand。基于图像名称,我想动态过滤数据表并仅显示具有图像名称的那些行。图像名称最初是通过click事件提供的。一个最小的工作示例如下:

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>parallelogram</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>    
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.18/css/dataTables.bootstrap.min.css">
    <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.18/js/jquery.dataTables.min.js"></script>
    <script type="text/javascript" src="https://cdn.datatables.net/1.10.18/js/dataTables.bootstrap.min.js"></script>
</head>
<body>

  <div id="annotationDetail">
    <table id="annTable" class="display" cellspacing="0">
        <thead>
            <tr>
                <th>ImageName</th>
                <th>Location</th>
                <th>Brand</th>
            </tr>
        </thead>  
    </table>                
  </div>
</body>
</html>

<script type="text/javascript">
function getAnnotations() {
  var annotations = [];
  var clickedImg = "Image1";
  annotations = [["Image1","Forest1","Theme1"],["Image1","Forest1","Theme2"],["Image2","Forest2","Theme3"]]
  $('#annTable').DataTable({
       "data" : annotations,
       "columns" : [
        {"ImageName" : annotations[0]},
        {"Location" : annotations[1]},
        {"Brand" : annotations[2]}
       ] 
      })
}
window.addEventListener('load', getAnnotations,false);
</script>

我对数据表是完全陌生的,并且以前很少使用它。进一步采取任何帮助都会有很大帮助。

0 个答案:

没有答案
相关问题