theonlychase
7/19/2017 - 9:14 PM

Table Functionality Code for Masterminds

Table Functionality Code for Masterminds

<script>
// http://www.mindsharemastermind.com/mastermind-evite/

jQuery(window).on("load", function() { 

    if (jQuery('div').not('#confirm-invite')) {

         /*Adds two attributes to the red button*/
         jQuery('a.button-style-1-red').attr('href', 'javascript:;').attr('id', 'cancel');

         /*Adds ID to modal confirm button*/
         jQuery('div.confirm-button a').attr('id', 'save-submit');

         /*Adds fancybox close functionality to the modal cancel button*/
         jQuery('#cancel').click(function() {
              jQuery.fancybox.close();
         });

        /*Appends "Select" column to the table header*/
        jQuery('thead tr.i4wmemberdirhead').append('<th>Select</th>');

        /*Adds "Select" to the table footer*/
        jQuery('tfoot tr.i4wmemberdirhead').append('<th rowspan="1" colspan="1">Select</th>');

        /*Adds "table1" class to the first table element*/
        jQuery('table').first().addClass("table1");

        /*Adds an id to each <tr>*/
        jQuery('table.table1 tbody tr').append(function(index) {
            return '<td id="' + index + '"></td>';
        });

        /*Gets the Id from each <tr>, slices it, then appends an input to the third column*/
        jQuery("table.table1 tbody tr").each(function(index, value) {
            var id = jQuery(this).attr('id').slice(4);
            jQuery(this.children[2]).append('<input type="checkbox" value="' + id + '"/>');
        });
    }

    jQuery(function() { 
        jQuery('.op-popup-button a.button-style-1-green').click(function() {
            
            /*Array of objects that contain the selected values*/
            var val = [];
            var ids = [];
            
            /*Adds the selected values to the "val" array*/
            jQuery(':checkbox:checked').each(function(i) {

                /*Object of all the values*/
                var values = {
                    id: "",
                    first: "",
                    last: ""
                }

                /*Adds the first, last and id to the values object*/
                values.first = jQuery(this).parent().siblings().eq(0).text();
                values.last= jQuery(this).parent().siblings().eq(1).text();
                values.id = jQuery(this).val();

                /*On each iteration, adds the values object to the val array*/
                val[i] = values;
            });

           /*Loops through the val array*/
           for (var i = 0; i < val.length; i++) {
                 
                 /*Adds a different class depending on whether the row is even or odd*/
                 if (i % 2 === 0) {
                       jQuery('table.custom-table tbody').append('<tr class="even"><td>' + 
                       val[i].first + '</td><td class="sorting_1">' + val[i].last + '</td></tr>');
                 } else {
                       jQuery('table.custom-table tbody').append('<tr class="odd"><td>' + val[i].first 
                       + '</td><td class="sorting_1">' + val[i].last + '</td></tr>');
                 }
          
                /*These two below empty the <tr> from the dom if the user decides to make a change. This was necessary because otherwise any changes you make won't take effect in the modal.*/
                jQuery('#cancel').click(function() {
                     jQuery.fancybox.close();
                     jQuery('table.custom-table tbody tr').empty();
                });
                jQuery('.fancybox-close').click(function() {
                     jQuery.fancybox.close();
                     jQuery('table.custom-table tbody tr').empty();
                });

                /*Pushes the id property from the val object*/
                ids[i] = val[i].id;
        }
            var x = "/mastermind-evite-step-2/?ids=";
            var y = ids;
            var result = x + y;
            jQuery('#save-submit').attr('href', result);
        });
    });
});

</script>