Forum


Replies: 6   Views: 701
How can i insert a checkbox into a table?
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 admin  · 16-10-2022 - 12:55

Hello,

Do you need to add the checkbox in a table created from scratch or using a template?

In both cases you can generate a WordFragment with the checkbox and add it into the table.

For example, a table created from scratch:

// WordFragments
$checkboxFragmentA = new WordFragment($docx);
$checkboxFragmentA->addFormElement('checkbox', array('fontSize' => 11,    'defaultValue' => true));

$checkboxFragmentB = new WordFragment($docx);
$checkboxFragmentB->addFormElement('checkbox', array('fontSize' => 11,    'defaultValue' => false));

// table
$valuesTable = array(
    array(
        'Content A',
        $checkboxFragmentA,
    ),
    array(
        'Content B',
        $checkboxFragmentB,
    )
);
$docx->addTable($valuesTable);

Or filling a table using template methods:

// WordFragments
$checkboxFragmentA = new WordFragment($docx);
$checkboxFragmentA->addFormElement('checkbox', array('fontSize' => 11,    'defaultValue' => true));

$checkboxFragmentB = new WordFragment($docx);
$checkboxFragmentB->addFormElement('checkbox', array('fontSize' => 11,    'defaultValue' => false));

// replace a table in the template
$data = array(
                array(
                    'ITEM' => 'Product A',
                    'REFERENCE' => $checkboxFragmentA,
                ),
                array(
                    'ITEM' => 'Product B',
                    'REFERENCE' => $checkboxFragmentB,
                ),
        );

$docx->replaceTableVariable($data);

If you are using a DOCX template but not template symbols (so you can't use template methods), you can use the insertWordFragment method to set the reference node to insert the WordFragment.

Regards.