Forum


Replies: 2   Views: 2434
Dropdown in a word template, how to set the value
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 dboanno  · 25-07-2017 - 18:34

Hello All,

I have a word template which I'm using. Some Fields have a dropdown value. My question is how to set the values of these dropdown using the library? .

I have purchased a premium version a few days ago. Works awesome for the fields having variables. 

Posted by admin  · 26-07-2017 - 11:51

Hello,

We move the request to the dev team. They are going to generate a sample to illustrate it; they'll update this topic.

Regards.

Posted by admin  · 30-07-2017 - 09:54

Hello,

MS Word dropdowns don't include a specific tag to set the active value. The first item w:listEntry in the w:ddList of the XML is the selected one; for example:

<w:ddList>
    <w:listEntry w:val="Two"/>
    <w:listEntry w:val="One"/>
    <w:listEntry w:val="Three"/>
    <w:listEntry w:val="Four"/>
</w:ddList>

Value 'Two' will be the value of the DropDown.

If you want to change the value of DropDowns, you can use DOCXPath to move the listEntry of the form element to the first position. For example we create a DOCX with a DropDown and value 'Two' is the default value (phpdocx reorders the elements when adding the DropDown to set the selected one in the first position):

<?php

require_once 'classes/CreateDocx.inc';

$docx = new CreateDocx();

$selectOptions = array(
    'selectOptions' => array('One', 'Two', 'Three', 'Four'), 
    'fontSize' => 14,
    'color' => 'C90000', 
    'bold' => true, 
    'underline' => 'double', 
    'font' => 'Algerian', 
    'defaultValue' => 1);
$docx->addFormElement('select', $selectOptions);

$docx->createDocx('output_template');

Using DOCXPath you can move the listEntry. For example, if we want to set the third listEntry of the first DropDown as the default value, you can run this code:

<?php

require_once 'classes/CreateDocx.inc';

$docx = new CreateDocxFromTemplate('output_template.docx');

$referenceNodeFrom = array(
    'customQuery' => '//w:ddList[1]/w:listEntry[3]',
);

$referenceNodeTo = array(
    'customQuery' => '//w:ddList[1]/w:listEntry[1]',
);

$docx->moveWordContent($referenceNodeFrom, $referenceNodeTo, 'before');

$docx->createDocx('output_template_updated');

This code move the third listEntry before the first listEntry. As you can use any XPath query, you can query the element to be moved by position as we do in the previous code or by the text content of the w:val attribute.

Regards.