Forum


Replies: 6   Views: 744
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 phosira  · 16-10-2022 - 10:12

I want insert checkboxes into an each table rows ends.

How can I?

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.

Posted by phosira  · 16-10-2022 - 23:25

Thanks. I solved it. One more question, is it possible to give an id or something each checkbox for identify when transform to html?

What i want is when checkbox checked, get data from checked row and save checked checkbox status on document.

Posted by admin  · 17-10-2022 - 06:04

Hello,

There's no option to set a custom ID to checkboxes. Also note MS Word removes extra informations added to XML contents that are not included in the OOXML specification.

The best approach would be adding bookmarks with custom names:

$checkboxFragmentA = new WordFragment($docx);
$checkboxFragmentA->addBookmark(array('type' => 'start', 'name' => 'bookmark_a'));
$checkboxFragmentA->addFormElement('checkbox', array('fontSize' => 11,    'defaultValue' => true));
$checkboxFragmentA->addBookmark(array('type' => 'end', 'name' => 'bookmark_a'));

$checkboxFragmentB = new WordFragment($docx);
$checkboxFragmentB->addBookmark(array('type' => 'start', 'name' => 'bookmark_b'));
$checkboxFragmentB->addFormElement('checkbox', array('fontSize' => 11,    'defaultValue' => false));
$checkboxFragmentB->addBookmark(array('type' => 'end', 'name' => 'bookmark_b'));

so you can parse the checkbox next to each bookmark in the XML content and also the HTML output. Also note that in addition to the custom bookmark, the addFormElement method adds another bookmark automatically with its own internal id and name (the internal options of this automatic bookmark can't be customized).

Regards.

Posted by phosira  · 17-10-2022 - 07:08

Thanks for the help. 

so is it possible to save the checkbox status changed from html to original document?

Posted by admin  · 17-10-2022 - 07:17

Hello,

You can parse the HTML output and use the parseCheckboxes method to update checkboxes by order. This doesn't require knowing information or adding bookmarks; checkboxes are updated by order.

Or you can use DOCXCustomizer to do more complex updates. For example knowing bookmark names to update specific checkboxes.

If you open a support ticket (https://www.phpdocx.com/support) we can generate a custom sample script.

Regards.

Posted by peteruser1  · 29-10-2022 - 16:38

Deleted by admin · 30-10-2022 - 08:20