Okay
  Public Ticket #1025022
add row before render
Closed

Comments

  •  2
    Dima started the conversation

    I am trying to add row to wpdatatable object just before it renders. Here is what I got:

    add_filter('wpdatatables_filter_initial_table_construct', 'alter_msds_table', 10 , 1);
    function alter_msds_table($table_data) {
        global $post, $product; $sku = $product->get_sku();    
        $rows[] = array(
            'COALESCE(changes.text, sds.text)' => $sku,
            'ftype' => '',
            'id' => '0',
            'name' => 'Catalog Number',
            'wdt_ID' => '150',
            );
        $rows[] = array(
            'COALESCE(changes.text, sds.text)' => $post->post_title,
            'ftype' => '',
            'id' => '0',
            'name' => 'Product Name',
            'wdt_ID' => '151',
        );
        $table_data->addRows( $rows );
        return $table_data;
    }
    

    And I get "Please provide a proper wpDataTables Row" from function addWDTRow. It checks for instanceof WDTRow, but I cannot find such class.

    How do I add row?

    Thanks

  •  1,846
    Miloš replied

    Hi Dima,
    Thank you for your purchase. 

    Please note that customization questions like this one are not covered in the included support for the plugin. Included support covers help with bugs and general inquiries for the plugin features, but not writing custom code. As you're developing some custom solution if you need our assistance we can offer you our paid customization service.

    Thank you for understanding. 

    Kind Regards, 

    Miloš Jovanović
    [email protected]

    Rate my support

    Try our FREE mapping plugin! MapSVG - easy Google maps, interactive SVG maps and floor plans, choropleth maps and much more - https://wordpress.org/plugins/mapsvg-lite-interactive-vector-maps/

    wpDataTables: FAQ | Facebook | Twitter | InstagramFront-end and back-end demo | Docs

    Amelia: FAQ | Facebook | Twitter | InstagramAmelia demo sites | Docs | Discord Community

    You can try wpDataTables add-ons before purchasing on these sandbox sites:

    Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables

  •  2
    Dima replied

    Generally it is so, but here I am using hook/s  that you provided, which should do the job. There is existing function in your class which I also can use (addRows). But the fact that it checks for instance of "WDTRow" (class I presume), which does not exist anywhere in the code, makes me think that this may be a bug. If this is a bug, I'll expect a fix, and if its not a bug and I am wrong, than your previous message is relevant.

  •  1,846
    Miloš replied

    Hi Dima,

    As you already said the filter returns datatable object but what are you trying to achieve it does not work like that.
    You find some lines that are from previous versions of plugin and they are needed to be changed, and I will give you my suggestion.

    In file ../wp-content/plugins/wpdatatables/source/class.wpdatatable.php around line 822 find this code 

    if( !( $row instanceof WDTRow ) ) {
        throw new WDTException( 'Please provide a proper wpDataTables Row' );
    }

    and comment it.

    Than  find this code around line 825

    if( $row->countCells() != $this->countColumns() ) {
        throw new WDTException( 'Count of the columns in table and in row is not equal. Row: '.$row->countCells().', table: '.$this->countColumns() );
    }

    and change it to be like this:

    if( count($row) != $this->countColumns() ) {
        throw new WDTException( 'Count of the columns in table and in row is not equal. Row: '.$row->countCells().', table: '.$this->countColumns() );
    }


    You will need to write your own method because we are developing functionalities and that methods are not finished yet and it is not easy as it looks to implement it so I hope this will help.

    Kind Regards, 

    Miloš Jovanović
    [email protected]

    Rate my support

    Try our FREE mapping plugin! MapSVG - easy Google maps, interactive SVG maps and floor plans, choropleth maps and much more - https://wordpress.org/plugins/mapsvg-lite-interactive-vector-maps/

    wpDataTables: FAQ | Facebook | Twitter | InstagramFront-end and back-end demo | Docs

    Amelia: FAQ | Facebook | Twitter | InstagramAmelia demo sites | Docs | Discord Community

    You can try wpDataTables add-ons before purchasing on these sandbox sites:

    Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables

  •  2
    Dima replied

    Ok, only now I got around to deal with this. Editing the class was my last option as I wanted to avoid touching your code to be able to update the plugin later automatically. Anyway since there is no other option for this, I have altered 2 functions, and it works fine now. You might want to add those insted of existing ones in future updates.

    public function addWDTRow( $row ) {
            if( count( $this->_wdtIndexedColumns ) == 0 ) {
                throw new WDTException('Please add columns to wpDataTable first.');
            }
            if( count($row) != $this->countColumns() ) {
                throw new WDTException( 'Count of the columns in table and in row is not equal. Row: '.count($row).', table: '.$this->countColumns() );
            }
            apply_filters( 'wdt_add_row', $row );
            $this->_dataRows[] = &$row;
        }
        public function addRows( &$rows ) {
            if( !is_array( $rows ) ) {
                throw new WDTException('Please provide an array of WDTRow objects.');
            }
            apply_filters( 'wdt_add_dataRows', $rows );
            foreach( $rows as &$row ) {
                $this->addWDTRow( $row );
            }
        }
    

    You should however pay attention here:

     if( count($row) != $this->countColumns() ) {

    This test might not be enough, since it does not test if array keys (column names) are right. If it was me, I'd do a function for addRows which only receives values and combines them with the right column names.

    Anyway hope this will help you a bit.

    Thanks

  •  1,846
    Miloš replied

    Hi Dima,


    Thank you for your sharing your ideas and solution with us.
    We really appreciate it.
    I will forward this to our lead developer and maybe we will add this for our future releases.
    Thank you again.

    Kind Regards, 

    Miloš Jovanović
    [email protected]

    Rate my support

    Try our FREE mapping plugin! MapSVG - easy Google maps, interactive SVG maps and floor plans, choropleth maps and much more - https://wordpress.org/plugins/mapsvg-lite-interactive-vector-maps/

    wpDataTables: FAQ | Facebook | Twitter | InstagramFront-end and back-end demo | Docs

    Amelia: FAQ | Facebook | Twitter | InstagramAmelia demo sites | Docs | Discord Community

    You can try wpDataTables add-ons before purchasing on these sandbox sites:

    Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables