• Resolved schiff7

    (@schiff7)


    Hi,
    Is there any way to add some custom commands once for all tables, rather than input them in every table’s options?
    Thank you very much.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author TobiasBg

    (@tobiasbg)

    Hi,

    thanks for your post, and sorry for the trouble.

    This is possible, via a plugin filter hook. What’s the “Custom Command” that you would like to give to all tables automatically?

    Regards,
    Tobias

    Thread Starter schiff7

    (@schiff7)

    Hi,

    Thanks for your reply.
    I have read the document of the DataTables JavaScript library, and would like to filter the table data on the fly via javascript, which need me to access the DataTable javascript object.
    The scripts that run on the page are surrounded by jQuery(document).ready(...), so I only can add an initComplete option to the Custom Command to access the DataTable javascript object and write my own scripts.

    Regards,
    Schiff7

    Plugin Author TobiasBg

    (@tobiasbg)

    Hi,

    thanks for the explanation!

    I think the best way for this would be to add this “Custom Command” via custom PHP code that uses plugin filter hook. For example, you could add this to your theme’s “functions.php” file:

    add_filter( 'tablepress_table_render_options', 'schiff7_tablepress_custom_commands', 10, 2 );
    funtion schiff7_tablepress_custom_commands( $render_options, $table ) {
      $custom_commands = '"initComplete": ...';
      if ( ! empty( $render_options['custom_commands'] ) ) {
        $render_options['custom_commands'] .= ','; 
      }
      $render_options['custom_commands'] .= $custom_commands;
      return $render_options;
    }

    Here, you would put your code into that

    $custom_commands = '"initComplete": ...';
    

    line.

    Regards,
    Tobias

    Thread Starter schiff7

    (@schiff7)

    Hi Tobias,

    Thanks for your reply and sorry for the delayed response.

    I appreciate that you taking the time to write me the code, but it seems that things still can’t function well yet.

    I checked the source code and found the following code in ‘controller-frontend.php’, around line 569:

    
    $render_options = apply_filters( 'tablepress_table_render_options', $render_options, $table );
    
    // Eventually add this table to list of tables which have a JS library enabled and thus are to be included in the script's call in the footer.
    if ( $render_options['use_datatables'] && $render_options['table_head'] && count( $table['data'] ) > 1 ) {
        // Get options for the DataTables JavaScript library from the table's render options.
        $js_options = array();
        foreach ( array(
            'alternating_row_colors',
            'datatables_sort',
            'datatables_paginate',
            'datatables_paginate',
            'datatables_paginate_entries',
            'datatables_lengthchange',
            'datatables_filter',
            'datatables_info',
            'datatables_scrollx',
            'datatables_scrolly',
            'datatables_locale',
            'datatables_custom_commands',
        ) as $option ) {
            $js_options[ $option ] = $render_options[ $option ];
        }
        (etc...)
    }
    

    Then I updated the $render_options['custom_commands'] to $render_options['datatables_custom_commands'] and saw my code rendered on the page.

    Should the $render_options['custom_commands'] be $render_options['datatables_custom_commands']?

    Thanks again for your help.

    Regards,
    Schiff7

    • This reply was modified 3 years, 11 months ago by schiff7.
    Plugin Author TobiasBg

    (@tobiasbg)

    Hi,

    ah, yes, of course! Sorry for that typo…
    Nice catch!

    Regards,
    Tobias

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Custom command for all tables’ is closed to new replies.