0

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?

8
  • 2
    What do you mean "a better way"? does it work? what are you asking ? You only need $('.dataTable').wrap('<div class="dataTables_scroll" />'); once as it applies to all of them and you only need to include the data tables plugin once, which you should ideally be doing in wp_enqueue_scripts Commented May 22, 2017 at 15:14
  • 1
    It's not very clear as to what you are asking. Commented May 22, 2017 at 15:19
  • 1
    Most of that datatable code looks redundant, especially the loading of the jQuery plugin twice. Commented May 22, 2017 at 15:21
  • Sorry, I thought the main issue was clarified in the first statement. The datatables work, they both load just fine and operate perfectly, but I don't want two tables on top of each other. I want the drop down to select which table is shown. You can see I use mytable and mytableSurvey as the option values in the dropdown and that corresponds to the 2 table ids. If I have the tables set to display:none;, I want to use the dropdown to select one of the 2 tables and show that one on the page. Commented May 22, 2017 at 15:22
  • 1
    @TomN. It's actually loaded 3 times in the code you provided. Anyway, hopefully my answer will help you out. Commented May 22, 2017 at 15:29

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, this is at least starting to work. After changing the code, the page now loads with just the mytable, which is correct, but when I select mytableSurvey nothing changes. It still shows mytable
You can see the state of it at testing.uwsinc.net/dashboardtest. I'm using the code above except I had to end script and start new script between the first jQuery and second function($). This is the only way it would actually create datatables. It seems to be successfully hiding the second table but not it's wrap/elements. And the dropdown is still not functional, it seems
Great! Glad I could help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.