0

I have a CPT list in the admin area.. and have two columns with checkboxes thats toggle the post status between publish/pending by ajax.. Is there any code or action fired by ajax to refresh the content of the CPT list without to refresh the entire page after a user clicks on one checkbox to change the status?

Thanks in advance for any kind of help!

enter image description here

2
  • Hi... Can you upload a screenshot somewhere to see better at what you dealing with?
    – Brada
    Feb 23 '19 at 19:08
  • Added, see the "enable" checkbox column.. each one affects the status of the post in that row.. the post status change by ajax query.. and the post status change.. but the cpt list don´t refresh automatically.. i must press f5 key to reflect in the list the new post status.. or.. maybe i could only change the post title reflecting the new status "-pending" or no when the checkbox is clicked.. but not sure how to do that. Feb 23 '19 at 20:45
0

This is a simple JavaScript problem to fix... I will tell you in pseudocode what you need to do, since I don't know what your HTML/JS/PHP code look like... If you have written the AJAX JS code then it would be easy to modify this.

I suppose you use JQuery AJAX Object abstraction... so the success function should look like:

success: function( result ) {
    // Previous Code

    // 1. Get the HTML Node title you want to modify
    // Ex: let ctpRow = $( `#post-${post_id}`  );

    // 2. Get the HTML Node that contains the title
    // Ex: let ctpRowTitle = ctpRow.find( '.row-title' );


    if ( post has been enabled ) {
        // Make sure to not have any other state.
        // Ex: ctpRowTitle.find( '.post-state' ).remove();

        // Append the HTML Node.
        // Ex: ctpRowTitle.append( '<span class="post-state">&nbsp;&mdash; Enabled</span>' );
    } else {
        // Make sure to not have any other state.
        // Ex: ctpRowTitle.find( '.post-state' ).remove();

        // Append the HTML Node.
        // Ex: ctpRowTitle.append( '<span class="post-state">&nbsp;&mdash; Disabled</span>' );
    }

}

There are a lot more to say... I didn't understand exactly what you want to do... the classes to target the rows are from default WordPress 'Posts', so you should modify the code to match yours... this is merely an example...

2
  • Perfect, I was in the right path! Thanks for your answer Feb 24 '19 at 14:34
  • Glad I could help!
    – Brada
    Feb 24 '19 at 16:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.