Forum


Replies: 2   Views: 148
Removewordcontent with type table-row
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by avandoorine  · 20-08-2024 - 12:10

Hello.

I may have found a small issue ith the removeWordContent.

My need is to remove a table row base on some condition, so as a trial I did this code : 
 

$referenceNode = array(
    'type' => 'table-row',
    'contains' => 'Bravo',
);

$docx->removeWordContent($referenceNode);

The goal is to remove table row if one of the cell contains Bravo. In my test file I have three rows with Alpha, Bravo and Charlie.

The issue is that it does not work, I can delete the complete table with the type set to table but the type table-row does not remove anythin.

I have found a workaround using the customQuery with this code 

$referenceNode = array(
    'customQuery' => '//w:tbl/w:tr[contains(.,"Bravo")]',
);

$docx->removeWordContent($referenceNode);

But it seems to me that the first code should be working as well.

Posted by admin  · 20-08-2024 - 12:20

Hello,

By default, the parent element of DOCXPath queries is the main document body (w:body) (unless a customQuery is used). If you want to query by any parent you need to set "/" as parent:

Main document body as default, allows to set any parent or a specific one. w:body (default), '/' (any parent) or any other specific parent (/w:tbl/, /w:tc/, /w:r/...).

From the sample DocxPath/removeWordContent/sample_9.php included in the package:

// get the reference node to be removed
$referenceNode = array(
    'type' => 'table-row',
    'parent' => '/',
    'occurrence' => 2,
);

// a custom query can also be used for more complex tasks
/*
$referenceNode = array(
    'customQuery' => '//w:tbl/w:tr[2]',
);
*/

$docx->removeWordContent($referenceNode);

Please try the following code with your DOCX:

$referenceNode = array(
    'type' => 'table-row',
    'parent' => '/',
    'contains' => 'Bravo',
);

$docx->removeWordContent($referenceNode);

Regards.

Posted by avandoorine  · 21-08-2024 - 10:49

Thanks a lot it is working I had not really understod that option.