Jump to content

Manipulating pagetable field markup with Jquery


Juergen
 Share

Recommended Posts

Hello @all,

I use a pagetable field for events as children. For better visibility for active and cancelled events I use Jquery to manipulate the output of the pagetable field. Active events are green and cancelled events are red (see screenshot)

Screenshot_6.jpg

This is the Jquery code snippet:

jQuery(document).ready(function() {
$("#wrap_Inputfield_singleeventtable table tbody tr[data-filter*='aktiv'] td:nth-child(2) ul li").wrapInner('<span class="uk-badge active"></span>');
$("#wrap_Inputfield_singleeventtable table tbody tr[data-filter*='abgesagt'] td:nth-child(2) ul li").wrapInner('<span class="uk-badge cancelled"></span>');
});

This works well until I open a childpage via a link in the modal. After closing the modal the pagetable will be updated via Ajax. This is the point where the ready function has no longer impact on the manipulation.

I am not very familiar with coding Jquery. Has anyone an idea which event could be triggered after Ajax update to manipulate the markup once more or if there is a better solution than Jquery ready function in this case.

Best regards

  • Like 1
Link to comment
Share on other sites

Simplest way is to put your jQuery into a function and call it on DOM ready and also on ajaxComplete:

$(function() {
    myPageTableFunction();
});

$(document).ajaxComplete(function() {
    myPageTableFunction();
});

 

But you could use a hook rather than the jQuery approach:

$this->addHookAfter('Fieldtype::markupValue', function($event) {
    $field = $event->arguments('field');
    $page = $event->arguments('page');
    if($field->name == 'my_text_field' && $page->template->name == 'my_pagetable_template') {
        $text = $event->return;
        if($text == 'aktiv') $event->return = "<span class='uk-badge active'>$text</span>";
        if($text == 'abgesagt') $event->return = "<span class='uk-badge cancelled'>$text</span>";
    }
});

 

  • Like 3
Link to comment
Share on other sites

10 hours ago, Robin S said:

$this->addHookAfter('Fieldtype::markupValue', function($event) { $field = $event->arguments('field'); $page = $event->arguments('page'); if($field->name == 'my_text_field' && $page->template->name == 'my_pagetable_template') { $text = $event->return; if($text == 'aktiv') $event->return = "<span class='uk-badge active'>$text</span>"; if($text == 'abgesagt') $event->return = "<span class='uk-badge cancelled'>$text</span>"; } });

This hook works well on page load, but not after update of the pagetable field:undecided: It is the same problem as the Jquery solution.

I have replaced my Jquery manipulation with this hook

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...