dongmi19720408 2017-05-22 15:05
浏览 82
已采纳

使用下拉列表显示2个不同的数据表

I've been working with an issue on my WordPress site for a bit now but I'm stuck. I have two working datatables (both showing on top of one another on the page currently) and a dropdown selection box. I need the dropdown box, which houses 2 options (one for each table) to select one table and show only that one.

Ideally I'd like the page to load with a default table (id="mytable") and then the dropdown can control everything from there.

Here is the code, other than the tables themselves:

<select name='tables' id='select-tables'>
  <option value="mytable">Survey Test Table</option>
  <option value="mytableSurvey">Survey Only</option>
</select>

//This is the code for the dropdown 

<script type="text/javascript"   src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js">
<script>
(function($) {
$('#select-tables').on('change', function(){
   var table = $(this).find('option:selected');
   $('#' + table).show();
   $('table').not('#' + table).hide();
});
}(jQuery));

Both tables have their own datatable script:

//datatable 1, table id is mytable

<script type="text/javascript" src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js">
</script>
<script type="text/javascript">
(function($) {
$(document).ready(function(){
$('#mytable').DataTable();
   $('.dataTable').wrap('<div class="dataTables_scroll" />');
});
}(jQuery));
</script>

// table 2, table id is mytableSurvey
<script type="text/javascript" src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js">
</script>
<script type="text/javascript">
(function($) {
$(document).ready(function(){
$('#mytableSurvey').DataTable();
   $('.dataTable').wrap('<div class="dataTables_scroll" />');
});
}(jQuery));
</script>

Since this is in wordpress, I had to modify the JS for the dropdown code to match the datatable code so that it will work in WP. Is there a better way to code the dropdown with JS for existing datatables?

  • 写回答

1条回答 默认 最新

  • dongni8124 2017-05-22 15:29
    关注

    You have a lot of redundant code in their, so I'll also reduce a lot of the repetition for you.

    <select name='tables' id='select-tables'>
      <option value="mytable">Survey Test Table</option>
      <option value="mytableSurvey">Survey Only</option>
    </select>
    
    //This is the code for the dropdown 
    
    <script type="text/javascript"   src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js">
    <script type="text/javascript">
    (function($) {
    $('#select-tables').on('change', function(){
       var table = $(this).find('option:selected');
       $('#' + table).show();
       $('table').not('#' + table).hide();
    });
    }(jQuery));
    
    (function($) {
    $(document).ready(function(){
       $('#mytable').DataTable();
       $('#mytableSurvey').DataTable();
       $('.dataTable').wrap('<div class="dataTables_scroll" />');
    
       //open the #mytable table on page load and close 'mytableSurvey'
       $('#mytable').show();
       $('#mytableSurvey').hide();
    });
    }(jQuery));
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办