1 回答
TA贡献2012条经验 获得超12个赞
由于今天的工作已被取消,我有 1/2 小时的空闲时间并使用 vanilla javascript 编写了一个小演示,我认为,语法上有效的 HTML 标记提供了比原始标记更大的灵活性,因为您可以简单地添加新template标签(带有内容)并且 javascript 应该相应地容纳它们并生成所需的输出。整个评论应该有助于澄清正在发生的事情......
document.addEventListener('DOMContentLoaded',()=>{
// references to HTML elements
const oSelect=document.querySelector('select[name="picker"]');
const oBttn=document.querySelector('input[type="button"][name="template_bttn"]');
const oForm=document.querySelector('form[name="template"]');
const oTable=document.querySelector('table[id="output"]');
const colTemplates=Array.from( document.querySelectorAll('template.source') );
const oSubBttn=document.querySelector('input[type="button"][name="subdata"]');
// event handler for when the `show selected` button is clicked
const clickhandler=function(e){
// only proceed if the SELECT has been changed from the default
if( oSelect.value != oSelect.childNodes[1].value ){
// find the template from HTML
let template=document.querySelector( 'template[ id="'+oSelect.value+'" ]' );
// add the template HTML to the empty form
oForm.innerHTML=template.innerHTML;
//assign event handler to button within form
let bttn=oForm.querySelector('input[type="button"]');
bttn.addEventListener('click', addcontenthandler );
}
};
// clear the form when select menu changes
const changehandler=function(e){
oForm.innerHTML='';
};
// event handler that does the final processing of data when all rows have been added...
const submithandler=function(e){
console.info( oTable.outerHTML );
};
const addcontenthandler=function(e){
// form elements that are NOT hidden or buttons...
let col=Array.from( oForm.querySelectorAll('input:not( [type="button"] ):not( [type="hidden"] )') );
// the type determines which tbody to write content to
let type=oForm.querySelector('input[ type="hidden" ]').value;
// the tbody that will be populated according to selection made in the SELECT menu
let tbody=oTable.querySelector('[ data-name="'+type+'" ]')
// If this particular tbody does NOT have column headers, add them based upon form element names in this form
if( tbody && !tbody.querySelector('tr[ scope="col" ]') ){
let tr=document.createElement('tr');
tr.setAttribute('scope','col');
tbody.appendChild( tr );
col.forEach( input=>{
let td=document.createElement('td');
td.textContent=input.name.replace(/\_/gi,' ');
tr.appendChild( td );
});
}
// for each input element add it's content to the table...
tr=document.createElement('tr');
tbody.appendChild( tr );
col.forEach( input=>{
td=document.createElement('td');
td.textContent=input.value;
input.value='';
tr.appendChild( td );
});
};
// add new tbody for each Template found in HTML source & add entry to the select menu
colTemplates.forEach( template=>{
let tbody=document.createElement('tbody');
tbody.dataset.name=template.id;
oTable.appendChild( tbody );
oSelect.appendChild( new Option( template.id, template.id ) );
});
// assign event listeners to various DOM elements
oBttn.addEventListener('click', clickhandler );
oSelect.addEventListener('change', changehandler );
oSubBttn.addEventListener('click', submithandler );
});
body, body *{font-family:monospace;box-sizing:border-box}
table{border:1px solid gray;border-collapse:none;width:50%;margin:5rem 0 0 0;padding:1rem;}
td{border:1px dotted gray;padding:1rem;margin:0.1rem;}
tr[scope='col']{background:gray;color:white}
[name='subdata']{padding:1rem;width:50%;}
.pl-276{}
.collapse.toggle{}
<form method='post'>
<select name='picker'>
<option selected hidden disabled>Please Select Template
<!-- other options are populated dynamically -->
</select>
<input type='button' name='template_bttn' value='Show Selected' />
</form>
<form method='post' name='template'><!-- content will be added here dynamically depending upon chosen template --></form>
<table id='output'></table>
<input type='button' name='subdata' value='Submit generated data' />
<!-- you can add as many `TEMPLATES` as you like with as many `INPUT ELEMENTS` as you like -->
<template id='identity' class='source'>
<input type='hidden' name='DB_Table' value='identity' />
<div class='collapse.toggle pl-276'>
<input type='text' placeholder='Full Name' name='Full_Name' />
<input type='text' placeholder='Last Name' name='Last_Name' />
</div>
<input type='button' value='Add Content' />
</template>
<template id='payment' class='source'>
<input type='hidden' name='DB_Table' value='payment' />
<div class='collapse.toggle pl-276'>
<input type='text' placeholder='Full Name' name='Full_Name' />
<input type='text' placeholder='Credit Card' name='Credit_Card' />
</div>
<input type='button' value='Add Content' />
</template>
<template id='banana' class='source'>
<input type='hidden' name='DB_Table' value='banana' />
<div class='collapse.toggle pl-276'>
<input type='text' placeholder='Banana Colour' name='Banana_Colour' />
<input type='text' placeholder='Banana Shape' name='Banana_Shape' />
<input type='text' placeholder='Banana Weight' name='Banana_Weight' />
</div>
<input type='button' value='Add Content' />
</template>
- 1 回答
- 0 关注
- 107 浏览
添加回答
举报