• Hey guys,
    I’m quiet new to WordPress and web development in general.
    For my new project I need to implement a table, in which the cell containing the highest value per row is automatically marked with a colour. The user should also be able to mark a cell manually. Furthermore each marked cell per column should count as numerical value 1, so I can do further calculations with these values.
    Not sure how this could be done. Is there a plugin for such a function?

    Thanks a lot in advance
    Greetings
    Saib0t

Viewing 15 replies - 1 through 15 (of 19 total)
  • This seems very niche and unique to your project. I couldn’t find a plugin to do this and is probably something that would need to be custom developed. My suggestion would be to checkout your local WordPress meetups, maybe attend an upcoming WordCamp and get their thoughts on the project.

    If you’re looking for a starting point to build this yourself, I would leave WordPress out of the equation in the beginning and just build it locally with HTML, CSS, and Javascript until your happy with the results. Once you have it working as you expect / want it to then you can look into porting it over to WordPress, maybe as a shortcode.

    Thread Starter saib0t

    (@saib0t)

    Thanks for your answer.
    Any specific reason, why you opt for Javascript instead of PHP?

    If the user is interacting with the table they’ll want live feed back when they click a cell, “mark a cell manually”. Whether that changes the cell color or does some kind of live calculation the user would want some kind of live feedback instead of having to submit the table with each change they make. Eventually you’ll need to save / process it which is where the PHP would come in to play.

    https://codepen.io/tugbucket/pen/xMRevw

    That’s the “highest value per row is automatically marked with a colour” and the “The user should also be able to mark a cell manually” parts.

    can you elaborate on what you want here: “Furthermore each marked cell per column should count as numerical value 1, so I can do further calculations with these values.”

    Thread Starter saib0t

    (@saib0t)

    Nice. Thank you.
    Sorry for the late reply.
    There’s just one little detail that needs to be changed: The possibility to remove the automated mark, by klicking on the cell.

    can you elaborate on what you want here: “Furthermore each marked cell per column should count as numerical value 1, so I can do further calculations with these values.”

    :

    Each marked cell counts as 1 point, which will be multiplied with a certain factor. This factor differs for each row. In the end all the points multiplied with their factor will be summed up for each column.

    https://codepen.io/tugbucket/pen/xMRevw

    There’s row totals and column totals for testing. The 1 * Row Multiplier is being placed in a span so you can visually see what’s happening.

    Is that what you’re after?

    Thread Starter saib0t

    (@saib0t)

    Yeah that’s pretty much what I was looking for. Thanks

    Thread Starter saib0t

    (@saib0t)

    So to test it, I tried to insert the code into my wordpress site using the plugin “Shortcoder”. But for some reason nothing appears in the preview.

    I merged HTML, CSS and JS into one document, as follows:

    
    <!DOCTYPE html>
    <html>
    <head>
    <style>td { border:1px solid #000; color: #000; cursor: pointer; }
    .max { background: red; color: #fff; }
    .selected { background: purple; color: #fff; }
    small { color: yellow; padding: 0 5px; }</style>
    <script> 
      /* https://stackoverflow.com/questions/45983101/jquery-find-smallest-value-in-table-row-and-style-it */
    
    $('document').ready(function(){
      $('tr').not('.colTotal').each(function(){
          var vals = $('td,th',this).map(function () {
              return parseInt($(this).text(), 10) ? parseInt($(this).text(), 10) :  null;
          }).get();
          // then find their maximum
          var max = Math.max.apply(Math, vals);
    
          // tag any cell matching the max value
          $('td,th', this).filter(function () {
              return parseInt($(this).text(), 10) === max;
          }).addClass('max');
      });
      
      /* user clicks */
      $('td').on('click', function(){
      if($(this).hasClass('max')){
        $(this).removeClass('max');
      } else {
     	  $(this).toggleClass('selected');
      }
      });
    });
    
    /* add up row values */
    $('#calcRows').on('click', function(){
        $('tr').not('.colTotal').each(function () {
            var sum = 0
            /*
            $(this).find('td').not('.total').each(function () {
                var val = $(this).text();
                if (!isNaN(val) && val.length !== 0) {
                    sum += parseFloat(val);
                }
            });
            $('.total', this).html(sum);
            */
            var sum = $('td.max', this).length + $('td.selected', this).length;
            $('.total', this).text(sum);
        });
    });
    $('#calcCols').on('click', function(){
        for (i=0;i<$('tr:eq(0) td').length;i++) {
           var total = 0;
            $('td:eq(' + i + ')', 'tr').each(function(i) {
              $('small', this).remove();
              if (!isNaN(parseInt($(this).text()))){
               total = total + parseInt($(this).text());
              }
            });            
            $('tr.colTotal td').eq(i).text(total);
        }
    });
    $('#calcMul').on('click', function(){
        for (i=0;i<$('tr:eq(0) td').length;i++) {
           var tot = 0;
            $('td:eq(' + i + ')', 'tr').each(function(i) {
              $('small', this).remove();
              if($(this).hasClass('max') || $(this).hasClass('selected')) {
                if (!isNaN(parseInt($(this).text()))){
                  $(this).append('<small />');
                  $('small', this).text(1 * parseInt($(this).parent('tr').data('multiplier')));
                 tot = tot + parseInt($('small', this).text());
                  //tot = 1 * parseInt($(this).parent('tr').data('multiplier'));
                  //alert(parseInt($(this).parent('tr').data('multiplier')));
                }
              }
            });            
          $('tr.mulTotal td').eq(i).text(tot);
        }
    });
    </script> 
    </head>
    <body>
    <table cellpadding="10" cellspacing="0">
      <tr data-multiplier="7">
        <td>29</td>
        <td>921</td>
        <td>2</td>
        <td class="total"></td>
      </tr>
      <tr data-multiplier="2">
        <td>112</td>
        <td>9</td>
        <td>55</td>
        <td class="total"></td>
      </tr>
      <tr data-multiplier="5">
        <td>13</td>
        <td>81</td>
        <td>82</td>
        <td class="total"></td>
      </tr>
      <tr class="colTotal">
        <td></td>
        <td></td>
        <td></td>
      </tr>
      <tr class="mulTotal">
        <td></td>
        <td></td>
        <td></td>
      </tr>
    </table>
    <button id="calcRows">Calculate Rows</button>
    <button id="calcCols">Calculate Columns</button>
    <button id="calcMul">Calculate Multiplier</button>
    </body>
    </html>
    

    Any idea, why the table wouldn’t show up?
    When inserting the code into the text editor of wordpress, the table appears in the visual editor, but not in the page preview.

    • This reply was modified 6 years, 1 month ago by saib0t.

    I installed Shortcoder. Created a short code and pasted in your code above. Then created a page and added the supplied shortcode ([sc name=”table_xyz”]). Saved the page. Opened the page and the table is there. No issue with displaying it. But, that javascript isn’t going to work there. Try it again using the code below. I added an ID to the table to prevent errors and moved the closing JS bracket to the end as Fiddle works differently.

    <html>
    <head>
    <style>td { border:1px solid #000; color: #000; cursor: pointer; }
    .max { background: red; color: #fff; }
    .selected { background: purple; color: #fff; }
    small { color: yellow; padding: 0 5px; }</style>
    <script> 
      /* https://stackoverflow.com/questions/45983101/jquery-find-smallest-value-in-table-row-and-style-it */
    
    jQuery('document').ready(function(){
      jQuery('#table_xyz tr').not('.colTotal').each(function(){
          var vals = jQuery('td,th',this).map(function () {
              return parseInt(jQuery(this).text(), 10) ? parseInt(jQuery(this).text(), 10) :  null;
          }).get();
          // then find their maximum
          var max = Math.max.apply(Math, vals);
    
          // tag any cell matching the max value
          jQuery('td,th', this).filter(function () {
              return parseInt(jQuery(this).text(), 10) === max;
          }).addClass('max');
      });
      
      /* user clicks */
      jQuery('#table_xyz td').on('click', function(){
      if(jQuery(this).hasClass('max')){
        jQuery(this).removeClass('max');
      } else {
     	  jQuery(this).toggleClass('selected');
      }
      });
    
    /* add up row values */
    jQuery('#calcRows').on('click', function(){
        jQuery('#table_xyz tr').not('.colTotal').each(function () {
            var sum = 0
            /*
            jQuery(this).find('td').not('.total').each(function () {
                var val = jQuery(this).text();
                if (!isNaN(val) && val.length !== 0) {
                    sum += parseFloat(val);
                }
            });
            jQuery('.total', this).html(sum);
            */
            var sum = jQuery('td.max', this).length + jQuery('td.selected', this).length;
            jQuery('.total', this).text(sum);
        });
    });
    jQuery('#calcCols').on('click', function(){
        for (i=0;i<jQuery('#table_xyz tr:eq(0) td').length;i++) {
           var total = 0;
            jQuery('td:eq(' + i + ')', '#table_xyz tr').each(function(i) {
              jQuery('small', this).remove();
              if (!isNaN(parseInt(jQuery(this).text()))){
               total = total + parseInt(jQuery(this).text());
              }
            });            
            jQuery('#table_xyz tr.colTotal td').eq(i).text(total);
        }
    });
    jQuery('#calcMul').on('click', function(){
    alert('s');
        for (i=0;i<jQuery('#table_xyz tr:eq(0) td').length;i++) {
           var tot = 0;
            jQuery('td:eq(' + i + ')', '#table_xyz tr').each(function(i) {
              jQuery('small', this).remove();
              if(jQuery(this).hasClass('max') || jQuery(this).hasClass('selected')) {
                if (!isNaN(parseInt(jQuery(this).text()))){
                  jQuery(this).append('<small />');
                  jQuery('small', this).text(1 * parseInt(jQuery(this).parent('tr').data('multiplier')));
                 tot = tot + parseInt(jQuery('small', this).text());
                  //tot = 1 * parseInt(jQuery(this).parent('tr').data('multiplier'));
                  //alert(parseInt(jQuery(this).parent('tr').data('multiplier')));
                }
              }
            });            
          jQuery('#table_xyz tr.mulTotal td').eq(i).text(tot);
        }
    });
    });
    
    </script> 
    </head>
    <body>
    <table cellpadding="10" cellspacing="0" id="table_xyz">
      <tr data-multiplier="7">
        <td>29</td>
        <td>921</td>
        <td>2</td>
        <td class="total"></td>
      </tr>
      <tr data-multiplier="2">
        <td>112</td>
        <td>9</td>
        <td>55</td>
        <td class="total"></td>
      </tr>
      <tr data-multiplier="5">
        <td>13</td>
        <td>81</td>
        <td>82</td>
        <td class="total"></td>
      </tr>
      <tr class="colTotal">
        <td></td>
        <td></td>
        <td></td>
      </tr>
      <tr class="mulTotal">
        <td></td>
        <td></td>
        <td></td>
      </tr>
    </table>
    <button id="calcRows">Calculate Rows</button>
    <button id="calcCols">Calculate Columns</button>
    <button id="calcMul">Calculate Multiplier</button>
    </body>
    </html>

    Another thing that might be messing it up is that you have stuff like:

    <!DOCTYPE html>
    <html>
    <head>
    ...
    </head>
    <body>
    ...
    </body>
    </html>

    My test theme is stripping those out and all good. If your’s isn’t that could be the issue. You don’t need those.

    • This reply was modified 6 years, 1 month ago by tugbucket.
    Thread Starter saib0t

    (@saib0t)

    Ok I think I found the problem.
    At the beginning of the page I inserted another shortcode with Post Snippets. This code is supposed to establish the connection to my database. As soon, as I remove this shortcode everything is fine. Any idea why?
    It would just look like this:

    [db-verbindung-aufbauen]
    
    [sc name="table_xyz"]

    And another question: Why is there a pop-up message every time I press “Calculate Multiplier”?

    • This reply was modified 6 years, 1 month ago by saib0t.
    • This reply was modified 6 years, 1 month ago by saib0t.

    Oops, I left in alert('s'); I used to test with. Thats the popup.

    For the shortcode confluct, not sure. Do you have debugging on? You have a link you can share to a page with both shortcodes on it?

    Thread Starter saib0t

    (@saib0t)

    Well I turend debugging on now. All it shows is the following:
    Warning: Use of undefined constant minor - assumed 'minor' (this will throw an Error in a future version of PHP) in /mnt/web415/e2/89/59712689/htdocs/WordPress_01/wp-config.php on line 94 Warning: Cannot modify header information - headers already sent by (output started at /mnt/web415/e2/89/59712689/htdocs/WordPress_01/wp-config.php:94) in /mnt/web415/e2/89/59712689/htdocs/WordPress_01/wp-admin/includes/misc.php on line 1144 Warning: Cannot modify header information - headers already sent by (output started at /mnt/web415/e2/89/59712689/htdocs/WordPress_01/wp-config.php:94) in /mnt/web415/e2/89/59712689/htdocs/WordPress_01/wp-includes/pluggable.php on line 1223
    That’s the mentioned constant: define( “WP_AUTO_UPDATE_CORE”, minor );
    Yet I’m not sure how to interpret this error, but as it also appears in the header of the working site, I assume, that it’s not the cause for the non visible table.

    hmmm… well that doesn’t look like it would be related. Do you have a link to the page all this is on?

    Thread Starter saib0t

    (@saib0t)

    I do, but there is not much to see:
    https://brightdecide.de/?page_id=15

    hmmm… well in your [db-verbindung-aufbauen] plugin file, is there a die() somewhere? Comparing that page to another page on your site, there’s no footer and the page stops after <div class=”site-content”> so it looks like something in your database plugin is stopping the rest of the output. I don’t know what that plugin is so kind of hard to say. Is the plugin publicly available (link) ? If not, can you send me the zip for it to alan at tugbucket.net and I can have a look?

Viewing 15 replies - 1 through 15 (of 19 total)
  • The topic ‘Implement an interactive table marking the highest value’ is closed to new replies.