Cart Licenses Contact Support Online Creator Log in/Register
Try
Buy
v 1.9.50

Dropdown with Custom Item Template


                        
        
            Survey.StylesManager.applyTheme("defaultV2");
        



                
                    var json = {
  "elements": [
    {
      "type": "dropdown",
      "name": "car",
      "title": "What car are you driving?",
      "itemComponent": "new-item",
      "isRequired": true,
      "choices": [ "Ford", "Vauxhall", "Volkswagen", "Nissan", "Audi", "Mercedes-Benz", "BMW", "Peugeot", "Toyota", "Citroen" ]
    }
  ]
};

                    window.survey = new Survey.Model(json);
                
        
            survey.onComplete.add(function(sender) {
            document.querySelector('#surveyResult').textContent =
            "Result JSON:\n" + JSON.stringify(sender.data, null, 3);
            });
        
class ItemTemplateComponent extends React.Component  {
    getImageUrl(brand) {
        return ("https://www.carlogos.org/car-logos/" + brand.toLowerCase() + "-logo.png");
    }
    render() {
        const item = this.props.item;
        const logoUrl = this.getImageUrl(item.title);

        return (
            <div className="my-list-item"
                    style={{ display: "flex" }}>
                <img className="list-item_image" src={logoUrl} alt="Logo" />
                <span className="list-item_text">{item.title}</span>
            </div>
        );
    }
}
SurveyReact.ReactElementFactory.Instance.registerElement(
  "new-item",
  (props) => {
    return React.createElement(ItemTemplateComponent, props);
  }
);        
            ReactDOM.render(
            <SurveyReact.Survey model={survey}  />, document.getElementById("surveyElement"));
        

                    
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Dropdown with Custom Item Template, Reactjs Survey Library Example</title>
    
<meta name="viewport" content="width=device-width" />
    <script src="https://unpkg.com/react@17.0.1/umd/react.production.min.js"></script>
    <script src="https://unpkg.com/react-dom@17.0.1/umd/react-dom.production.min.js"></script>
    <script src="https://unpkg.com/@babel/standalone@7.2.5/babel.min.js"></script>
    <script src="/DevBuilds/survey-core/survey.core.min.js"></script>
    <script src="/DevBuilds/survey-core/survey.i18n.min.js"></script>
    <script src="/DevBuilds/survey-react-ui/survey-react-ui.min.js"></script>
    <link href="/DevBuilds/survey-core/defaultV2.min.css" type="text/css" rel="stylesheet" />
    <link rel="stylesheet" href="./index.css">

</head>
<body style="margin: 0">
    
            <div id="surveyElement" style="display:inline-block;width:100%;">
            </div>
    <div id="surveyResult"></div>

<script type="text/babel" src="./index.js"></script>

</body>
</html>

.list-item_text {
    padding: 10px;
}

.list-item_image {
    width: 60px;
    height: 45px;
    vertical-align: middle;
}

If you want to customize a drop-down menu item, follow the steps below:

  1. Implement a component that renders item markup.
    The item is an IAction object. You can use component props to access it.

  2. Register the component so that it can be accessed by name.
    In React, register the component in ReactElementFactory as shown in the JavaScript tab.

    In Vue and Knockout, use techniques native to these libraries:

  3. Assign the component name to the Dropdown question's itemComponent property in the survey JSON definition.