Created
December 8, 2015 05:32
-
-
Save mgratch/9701612eb084aa8a5f81 to your computer and use it in GitHub Desktop.
front-end approval when using the datatables extension with additional colums added via `gravityview_datatables_js_options` filter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Custom js script at post edit screen | |
| * | |
| * @package gravityview-edit-datatables-options | |
| * @license GPL2+ | |
| * @author Marc Gratch | |
| * @link http://marcgratch.com | |
| * @copyright Copyright 2015, Marc Gratch | |
| * | |
| * @since 1.0.0 | |
| * | |
| * globals jQuery, gvDTCGlobals, TableAPI | |
| */ | |
| var TableAPI; | |
| (function( $ ) { | |
| //Add the new approval dropdown to the DOM | |
| $('body').append('<ul id="gvdt-dropdown" class="dropdown">' + | |
| '<li><label><span data-status="0" class="dashicons dashicons-marker"></span> Unapproved</label></li>' + | |
| '<li><label><span data-status="1" class="dashicons dashicons-yes"></span> Approved</label></li>' + | |
| '<li><label><span data-status="2" class="dashicons dashicons-no"></span> Rejected</label></li>' + | |
| '</ul>'); | |
| //init | |
| var approval_dd = $("#gvdt-dropdown"), | |
| tables = $('.gv-datatables'), | |
| aTable, | |
| lastChecked = null; | |
| //Prevent child element click from bubbling to parent element | |
| $(approval_dd).on('click',function(evt){ | |
| evt.preventDefault(); | |
| evt.stopPropagation(); | |
| }); | |
| //clicking outside the DD hides/removes it | |
| $(document).on('click', function(evt){ | |
| var elem = $("td.approval_status"); | |
| $(elem).removeClass('active'); | |
| }); | |
| //just in case there are multiple tables on a page find each one and then intialize the rest | |
| $.each(tables, function(index,table){ | |
| //dataTables has completed drawing | |
| $(table).on( 'init.dt', function () { | |
| cb = $("input[type='checkbox']"), | |
| aTable = $(table).dataTable(), | |
| TableAPI = new $.fn.dataTable.Api( aTable); | |
| //using the dataTables API get the clicked cell index | |
| $(table).on('click','td.approval_status', function(evt){ | |
| TableAPI.currPos = [ | |
| TableAPI.cell( this ).index().columnVisible, | |
| TableAPI.cell( this ).index().row | |
| ]; | |
| //add the approval drop down the cell | |
| $(this).append(approval_dd).addClass('active'); | |
| evt.stopPropagation(); | |
| return false; | |
| }); | |
| //select an approval option | |
| $(approval_dd).on('click','li', function(evt){ | |
| var approval_elem = $(approval_dd).prev(), | |
| row_id = $(approval_elem).parents('tr').attr('id'), | |
| lead_id = row_id.replace('lead-',''), | |
| clicked_opt = $(this).find('span').attr("data-status"), | |
| new_value = "<label data-status='"+clicked_opt+"'><span class='value'>"+clicked_opt+"</span></label>", | |
| approved = ''; | |
| if (clicked_opt == 0){ | |
| approved = ''; | |
| } | |
| else if (clicked_opt == 1){ | |
| approved = 'Approved'; | |
| } | |
| else if (clicked_opt == 2){ | |
| approved = 0; | |
| } | |
| //get the data ready for ajax submission | |
| var data = { | |
| action: 'gv_update_approved', | |
| entry_id: lead_id, | |
| form_id: gvDTCGlobals.form_id, | |
| approved: approved, | |
| nonce: gvDTCGlobals.nonce | |
| }; | |
| //fire ajax | |
| $.post( gvDTCGlobals.ajaxurl, data, function ( response ) { | |
| if ( response ) { | |
| aTable.fnUpdate( new_value, TableAPI.currPos[1], TableAPI.currPos[0]); | |
| } | |
| } ); | |
| evt.stopPropagation(); | |
| return false; | |
| }); | |
| //turn on shift click for bulk row selection | |
| var handleChecked = function(e) { | |
| if(lastChecked && e.shiftKey) { | |
| var i = cb.index(lastChecked); | |
| var j = cb.index(e.target); | |
| var checkboxes = []; | |
| if (j > i) { | |
| checkboxes = $('input[type="checkbox"]:gt('+ (i-1) +'):lt('+ (j-i) +')'); | |
| } else { | |
| checkboxes = $('input[type="checkbox"]:gt('+ j +'):lt('+ (i-j) +')'); | |
| } | |
| if (!$(e.target).is(':checked')) { | |
| $(checkboxes).removeAttr('checked'); | |
| } else { | |
| $(checkboxes).prop('checked', 'checked'); | |
| } | |
| } | |
| lastChecked = e.target; | |
| // Other click action code. | |
| }; | |
| //toggle all rows on/ff | |
| var clickAllBoxes = function() { | |
| $(table).find('.headercb').toggle( | |
| function() { | |
| $('td.cb_col input[type=checkbox]').prop('checked', true); | |
| }, | |
| function() { | |
| $('td.cb_col input[type=checkbox]').prop('checked', false); | |
| } | |
| ); | |
| }; | |
| $('td.cb_col input[type=checkbox]').click(handleChecked); | |
| clickAllBoxes(); | |
| }); | |
| }); | |
| } (jQuery) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment