簡體   English   中英

動態添加時如何使 materializecss 下拉列表工作

[英]How to make the materializecss dropdown work when added dynamically

以下是我在Google Apps Script中制作的示例網絡表單示例,我試圖在單擊add按鈕時動態添加三個 select 下拉菜單和一個輸入元素。 元素應按以下順序呈現 - dropdown dropdown input dropdown

我正在為此使用物化框架

經過大量嘗試和閱讀materializecss文檔后,我能夠按預期呈現文本輸入字段 但是,下拉菜單仍然不會呈現。 顯然,我犯了一些錯誤,無法弄清楚是什么和在哪里。

我包括代碼文件-

  1. Code.gs
function doGet(e) {
  Logger.log(e);
  return HtmlService.createTemplateFromFile('form_materialize').evaluate();
}

function include(fileName){
  return HtmlService.createHtmlOutputFromFile(fileName).getContent();
}
  1. form_materialize.html
<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <!-- google font pack link -->
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    <!-- Mini materialize.css cdn link -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
    <?!= include('css_scripts'); ?>
  </head>
  <body>
    <div class="container">
      
      <div class = "row">
        <h1>A Sample Form</h1>
      </div>
      
      <div id="productsection">
        <!-- product details like "Product Type"(dropdown), "Products"(dropdown), "Product Qty"(text input field), "Unit"(dropdown) to be added here dynamically -->

      </div>

      <div class = "row">
        <a class="btn-floating btn-large waves-effect waves-light red" id="addproduct"><i class="material-icons">add</i></a>
      </div> 

    </div>
    <!-- Mini materialize.js cdn link -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
    <?!= include('js_scripts_materialize'); ?>
  </body>
</html>
  1. js_scripts_materialize.html
<script>
  document.addEventListener('DOMContentLoaded', function() {
    var elems = document.querySelectorAll('select');
    var instances = M.FormSelect.init(elems, options);
  });

  let counter = 0;

  const orderTypeList = ["PH", "ECOM"];
  const optionList = ["Test Product 1", "Test Product 2", "Test Product 3", "Test Product 4", "Test Product 5"];                                   
  const unitOptionList = ["KGS", "PCS", "BAGS"];
  
  document.getElementById("addproduct").addEventListener("click", addInputField);

  function addInputField(){
    counter++;
    
    // everytime when "add product" button is clicked, the following elements must be added to the "<div id="produc></div>" tag.

    // <div class="row">
    //  <div class="input-field col s4" id="divone">
      //   <select id="productX">
      //     <option>option-i</option>
      //     <option>option-1</option>
      //     <option>option-2</option>
      //     ...
      //     ...
      //     <option>option-n</option>
    //     <select>
    //  </select>
    //  <div class="input-field col s4" id="divtwo"> 
    //     <input id="productqtyX" type="text">
    //     <label for="productqtyX">Quantity</label>
    //  </div>
    //  <div class="input-field col s4" id="divthree"> 
    //     <select id="productUnitX">
    //       <option>option-1</option>
    //       <option>option-2</option>
    //       ...
    //       ...
    //       <option>option-n</option>
    //     </select>
    //  </div>
    // </div>
    
    // creates a new div of class row
    const newDivElem = createElementTemplate('div', null, ['row']);

    // creates a new select tag for order type dropdown
    const newOrderTypeSelectElem = createElementTemplate('select', "ordertype" + counter.toString());

    // generates the content of the dropdown for products and is inserted to the above "productX" select tag
    createOptionsElem(newOrderTypeSelectElem, orderTypeList);

    // creates a new select tag for product dropdown
    const newProductSelectElem = createElementTemplate('select', "product" + counter.toString());

    // generates the content of the dropdown for products and is inserted to the above "productX" select tag
    createOptionsElem(newProductSelectElem, optionList);

    // creates a input element for quantity input
    const newQtyInputElem = createElementTemplate('input', 'productqty' + counter.toString(), ['validate']);
    newQtyInputElem.type = 'text';

    // creates a label for the quantity input element
    const newQtyLabelElem = createElementTemplate('label');
    newQtyLabelElem.textContent = "Quantity";

    //Creates a new select element for product quantity unit(dropdown)
    const newUnitSelectElem = createElementTemplate('select', 'productqtyunit' + counter.toString());

    // generates the content of the dropdown for units and is inserted to the above "productqtyunitX" select tag
    createOptionsElem(newUnitSelectElem, unitOptionList);

    //create inner "div" tags with class "input-field col s4" as described in materializecss documentation
    const innerDivElems = [];

    for(let i = 0; i < 4; i++){
      innerDivElems.push(createElementTemplate('div', `div${(Number(i) + 1)}`, ['input-field', 'col', 's3']));
    }
    
    innerDivElems[0].appendChild(newOrderTypeSelectElem);

    innerDivElems[1].appendChild(newProductSelectElem);

    innerDivElems[2].appendChild(newQtyInputElem);
    innerDivElems[2].appendChild(newQtyLabelElem);

    innerDivElems[3].appendChild(newUnitSelectElem);

    //Inserts select, quantityInput, quanityLabel, newUnitSelectTag tags in div child
    for(let i in innerDivElems){
      newDivElem.appendChild(innerDivElems[i]);
    }

    // Finally, appends the newly created div tag to the productSection tag.
    document.getElementById('productsection').appendChild(newDivElem);
  }

function createOptionsElem(selectElem, optionsArr){
  const newDefaultOptionElem = document.createElement('option');
  newDefaultOptionElem.disabled = true;
  newDefaultOptionElem.setAttribute('selected', true);
  newDefaultOptionElem.textContent="Choose your option";

  selectElem.appendChild(newDefaultOptionElem);

  for(let i in optionsArr){
    const newOptionElem = document.createElement('option');
    newOptionElem.textContent = optionsArr[i];
    newOptionElem.value = optionsArr[i];

    // Inserts the option tag in select tag
    selectElem.appendChild(newOptionElem);
  }
}

// function to create a new element
function createElementTemplate(tagType, idVal, classNameList){
  const newElement = document.createElement(tagType);
  
  if(idVal !== undefined)
    newElement.id = idVal;
  
  if(classNameList !== undefined){
    for(let i in classNameList){
      newElement.classList.add(classNameList[i]);
    }
  }

  return newElement;
}
</script>

雖然我不確定我是否能正確理解你的預期結果,但下面的修改呢?

本次修改,修改了你的js_scripts_materialize.html

修改腳本:

我認為在這種情況下,這部分可能不需要使用。

document.addEventListener('DOMContentLoaded', function() {
  var elems = document.querySelectorAll('select');
  var instances = M.FormSelect.init(elems, options);
});

另外,請按如下方式修改addInputField()

從:

document.getElementById('productsection').appendChild(newDivElem);

到:

document.getElementById('productsection').appendChild(newDivElem);
var elems = document.querySelectorAll('select'); // Added
M.FormSelect.init(elems); // Added
  • 通過這個修改,我認為當你點擊一個紅色按鈕時,你可以看到下拉列表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM