找回密码
 立即注册

QQ登录

只需一步,快速开始

mfkpie8
银牌会员   /  发表于:2020-12-8 22:59:09
11#
  1. <h1 class="postTitle" style="margin-bottom: 10px; padding-left: 5px; font-size: 15.6px; border: 0px; float: left; width: 723.475px; clear: both; line-height: 1.5; color: rgb(0, 0, 0); font-family: &quot;Helvetica Neue&quot;, Helvetica, Verdana, Arial, sans-serif; background-color: rgb(255, 255, 255);"><a id="cb_post_title_url" class="postTitle2 vertical-middle" href="https://www.cnblogs.com/Web-Architecture/p/6478371.html" style="outline: none; color: rgb(33, 117, 155); text-decoration-line: none; transition: all 0.4s linear 0s;"><span style="vertical-align: middle;">基于添加自定义字段的总结</span></a></h1><div class="clear" style="clear: both; color: rgb(0, 0, 0); font-family: &quot;Helvetica Neue&quot;, Helvetica, Verdana, Arial, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);"></div><div class="postBody" style="color: rgb(0, 0, 0); line-height: 1.7; font-family: &quot;Helvetica Neue&quot;, Helvetica, Verdana, Arial, sans-serif; background-color: rgb(255, 255, 255);"><div id="cnblogs_post_body" class="blogpost-body" style="margin-bottom: 20px; word-break: break-word;"><p style="margin: 10px auto;">以下为添加自定义字段,设置动态添加表格行,删除表格行的实例。</p><p style="margin: 10px auto;">主要功能如下:</p><ul style="margin-left: 30px;"><li style="margin-bottom: 1em; margin-left: 0px; list-style-position: initial; list-style-image: initial;">添加行</li><li style="margin-bottom: 1em; margin-left: 0px; list-style-position: initial; list-style-image: initial;">删除行</li><li style="margin-bottom: 1em; margin-left: 0px; list-style-position: initial; list-style-image: initial;">选择当前字段名称</li><li style="margin-bottom: 1em; margin-left: 0px; list-style-position: initial; list-style-image: initial;">设置当前字段名称可编辑状态</li></ul><p style="margin: 10px auto;">图示为实现的演示图:</p><p style="margin: 10px auto;"><img src="https://images2015.cnblogs.com/blog/799972/201702/799972-20170228122317532-382270099.png" alt="" style="border: 0px; max-width: 700px; height: auto;"></p><p style="margin: 10px auto;"> </p><p style="margin: 10px auto;">html:</p><p style="margin: 10px auto;"> </p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="outline: none; color: rgb(0, 0, 0); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 700px; height: auto;"></a></span></div><pre style="overflow: auto; font-family: &quot;Courier New&quot; !important;">   <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;"><</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">table </span><span style="color: rgb(255, 0, 0); line-height: 1.5 !important;">class</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">="table table-hidden"</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></span>
  2.         <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;"><</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">thead</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></span>
  3.         <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;"><</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">tr</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></span>
  4.             <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;"><</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">th </span><span style="color: rgb(255, 0, 0); line-height: 1.5 !important;">colspan</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">="3"</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">><</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">a </span><span style="color: rgb(255, 0, 0); line-height: 1.5 !important;">id</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">="addRow"</span><span style="color: rgb(255, 0, 0); line-height: 1.5 !important;"> class</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">="btn btn-info"</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></span>添加一行<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;"></</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">a</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">th</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></span>
  5.         <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;"></</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">tr</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></span>
  6.         <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;"><</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">tr</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></span>
  7.             <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;"><</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">th</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></span>字段名称<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;"></</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">th</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></span>
  8.             <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;"><</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">th</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></span>字段值<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;"></</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">th</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></span>
  9.             <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;"><</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">th</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></span>操作<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;"></</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">th</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></span>
  10.         <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;"></</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">tr</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></span>
  11.         <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;"></</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">thead</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></span>
  12.         <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;"><</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">tbody </span><span style="color: rgb(255, 0, 0); line-height: 1.5 !important;">id</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">="optionContainer"</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></span>

  13.         <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;"></</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">tbody</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></span>
  14.     <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;"></</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">table</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></span></pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="outline: none; color: rgb(0, 0, 0); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 700px; height: auto;"></a></span></div></div><p style="margin: 10px auto;">js:</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="outline: none; color: rgb(0, 0, 0); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 700px; height: auto;"></a></span></div><pre style="overflow: auto; font-family: &quot;Courier New&quot; !important;">   <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">function</span><span style="line-height: 1.5 !important;"> File1(e){
  15. </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">        console.log(e)</span>
  16.         <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span> name =<span style="line-height: 1.5 !important;"> e.id;
  17. </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">        alert(name)</span>
  18.         <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span> value=name.substring(6,7<span style="line-height: 1.5 !important;">)
  19. </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">        alert(value)</span>
  20.         <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span> (e.value == '0'<span style="line-height: 1.5 !important;">) {
  21.             $(</span>"#"+name).css('display', 'none'<span style="line-height: 1.5 !important;">);
  22.             $(</span>"#"+name).siblings('input').attr('type', 'text'<span style="line-height: 1.5 !important;">);
  23.             $(</span>"#"+name).siblings('input').attr('name', 'key_text_'+<span style="line-height: 1.5 !important;">value);
  24.         }</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">else</span><span style="line-height: 1.5 !important;"> {
  25.             alert(e.value)
  26.         }
  27.     }
  28.     </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">function</span><span style="line-height: 1.5 !important;"> Field2(e){
  29.         </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span> name =<span style="line-height: 1.5 !important;"> e.id;
  30.         $(</span>"#"+name).siblings('select').css('display', 'block'<span style="line-height: 1.5 !important;">);
  31.         $(</span>"#"+name).attr('type', 'hidden'<span style="line-height: 1.5 !important;">);
  32.     }
  33.     </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span> rowCount = 0<span style="line-height: 1.5 !important;">;
  34.     $(</span>"#addRow").click(<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">function</span><span style="line-height: 1.5 !important;"> () {
  35.         </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">Field1(rowCount);</span>
  36.         rowCount++<span style="line-height: 1.5 !important;">;
  37.         </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span> newRow = '<tr >' +
  38.                 '<td><select id="select' + rowCount + '"  class="form-control FieldSelect">' +
  39.                 '<option value="1">QQ</option>' +
  40.                 '<option value="2">电话</option>' +
  41.                 '<option value="3">地址</option>' +
  42.                 '<option value="0">编辑</option></select>' +
  43.                 '<input id="input' + rowCount + '" type="hidden" name="key'+rowCount+'" class="form-control Fieldtxt" value="QQ" size="10" ondblclick="javascript:return Field2(this)"/> ' +
  44.                 '<td> <input class="form-control"  placeholder="输入字段值" name="content'+rowCount+'" type="text"> </td> ' +
  45.                 '<td> <span class="delRow btn btn-danger" onclick="$(this).parent().parent().remove();">删除</span> </td> ' +
  46.                 '</tr>'<span style="line-height: 1.5 !important;">;
  47.         $(</span>'#optionContainer'<span style="line-height: 1.5 !important;">).append(newRow);
  48.     });</span></pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="outline: none; color: rgb(0, 0, 0); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 700px; height: auto;"></a></span></div></div><p style="margin: 10px auto;">以上实例在独立的文件中尚可实现,在开发过程中难免会遇到问题。</p><p style="margin: 10px auto;">把代码放进了项目中会显示 </p><pre style="overflow: auto;">Field()方法未定义的情况

  49. 就考虑了一种方案,在选择实现调用函数,并不写在动态标签中。

  50. 以下为解决方案:</pre><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="outline: none; color: rgb(0, 0, 0); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 700px; height: auto;"></a></span></div><pre style="overflow: auto; font-family: &quot;Courier New&quot; !important;"><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span> rowCount = 0<span style="line-height: 1.5 !important;">;
  51. $(</span>"#addRow").click(<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">function</span><span style="line-height: 1.5 !important;"> () {
  52.     </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">Field1(rowCount);</span>
  53.     rowCount++<span style="line-height: 1.5 !important;">;
  54.     </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span> newRow = '<tr >' +
  55.         '<td>' +
  56.         '<select id="select' + rowCount + '" name="key_select_'+rowCount+'"  class="form-control FieldSelect">' +
  57.         '<option value="1">QQ</option>' +
  58.         '<option value="2">电话</option>' +
  59.         '<option value="3">地址</option>' +
  60.         '<option value="0">编辑</option>' +
  61.         '</select>' +
  62.         '<input id="input' + rowCount + '" type="hidden" name="key'+rowCount+'" class="form-control Fieldtxt" value="QQ" size="10" disabled="disabled"/> ' +
  63.         '<td> <input class="form-control"  placeholder="输入字段值" name="content'+rowCount+'" type="text"> </td> ' +
  64.         '<td> <span class="delRow btn btn-danger"  onclick="$(this).parent().parent().remove();" >删除</span> </td> ' +
  65.         '</tr>'<span style="line-height: 1.5 !important;">;
  66.     $(</span>'#optionContainer'<span style="line-height: 1.5 !important;">).append(newRow);
  67.     $(</span>'#select' + rowCount).change(<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">function</span><span style="line-height: 1.5 !important;"> () {
  68.         File1(</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">this</span><span style="line-height: 1.5 !important;">)
  69.     });
  70.     $(</span>'#input' + rowCount).dblclick(<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">function</span><span style="line-height: 1.5 !important;"> () {
  71.         Field2(</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">this</span><span style="line-height: 1.5 !important;">)
  72.     })
  73. });
  74. </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span> File1=<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">function</span><span style="line-height: 1.5 !important;">(e){
  75.     </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">console.log(e);</span>
  76.     <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">获取id</span>
  77.     <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span> name =<span style="line-height: 1.5 !important;"> e.id;
  78.     </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">获取id在第几行</span>
  79.     <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span> value=name.substring(6,7<span style="line-height: 1.5 !important;">);
  80.     </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">alert(value);</span>
  81.     <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">判断值是否为编辑</span>
  82.     <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span> (e.value == '0'<span style="line-height: 1.5 !important;">) {
  83.         $(</span>"#"+name).attr("disabled","disabled"<span style="line-height: 1.5 !important;">);
  84.         $(</span>"#"+name).css('display', 'none'<span style="line-height: 1.5 !important;">);
  85.         $(</span>"#"+name).siblings('input').attr('type', 'text'<span style="line-height: 1.5 !important;">);
  86.         </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">input 赋值name字段为key_text_**</span>
  87.         $("#"+name).siblings('input').removeAttr('disabled'<span style="line-height: 1.5 !important;">);
  88.         $(</span>"#"+name).siblings('input').attr('name', 'key_text_'+<span style="line-height: 1.5 !important;">value);
  89.     }</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">else</span><span style="line-height: 1.5 !important;"> {
  90.         </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">select选中的值</span>
  91.         $("#"+name).attr('name','key_select_'+<span style="line-height: 1.5 !important;">value)
  92.     }
  93. };
  94. </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span> Field2=<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">function</span><span style="line-height: 1.5 !important;">(e){
  95.     </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span> name =<span style="line-height: 1.5 !important;"> e.id;
  96.     $(</span>"#"+name).siblings('select').removeAttr('disabled'<span style="line-height: 1.5 !important;">);
  97.     $(</span>"#"+name).siblings('select').css('display', 'block'<span style="line-height: 1.5 !important;">);
  98.     $(</span>"#"+name).attr('disabled', 'disabled'<span style="line-height: 1.5 !important;">);
  99.     $(</span>"#"+name).attr('type', 'hidden'<span style="line-height: 1.5 !important;">);
  100. };</span></pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="outline: none; color: rgb(0, 0, 0); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 700px; height: auto;"></a></span></div></div><p style="margin: 10px auto;"> 以上为实现效果,当设置字段名称为输入框时,可双击返回选择下拉框来选择实现主要功能。</p></div></div>
复制代码
回复 使用道具 举报
Simon.hu讲师达人认证 悬赏达人认证 活字格认证
超级版主   /  发表于:2020-12-9 09:21:26
12#
大佬
这是啥意思?  这么多代码是干啥的?
回复 使用道具 举报
小崽子
注册会员   /  发表于:2022-3-31 15:56:58
13#
有这么一个思路可以试一试:
比如我们平时阿里巴巴上架产品的时候会打开产品编辑详情页,把这个打开的产品编辑详情页看作主表,然后设置一个子表,子表搞两个或两个以上的字段,比如自定义属性标题,自定义属性值,然后主表子表做关联,然后再在最终展示页展示主表内容和子表内容,子表内容以图文列表的方式展示出来,把图文列表放到一行合并居中的行里面,把这一行的行高设置为自适应高度就能达到想要的效果。
回复 使用道具 举报
小崽子
注册会员   /  发表于:2022-3-31 16:00:10
14#
olookok 发表于 2020-12-8 19:00
如果 活字格 中  表格的列  可以动态 展示  就牛逼了 。希望能早日实现。还有锁定 行,锁定 列,锁定 单元 ...

一般锁定行肯定是要满足某些条件就锁定,那再编辑和删除按钮上面可以做if条件判断,满足条件了就不给编辑或者不给删除了。可以这样尝试着去做。
回复 使用道具 举报
Joe.xu讲师达人认证 悬赏达人认证 活字格认证
超级版主   /  发表于:2022-3-31 17:17:36
15#
小崽子 发表于 2022-3-31 16:00
一般锁定行肯定是要满足某些条件就锁定,那再编辑和删除按钮上面可以做if条件判断,满足条件了就不给编辑 ...

感谢大佬的经验分享
回复 使用道具 举报
12
您需要登录后才可以回帖 登录 | 立即注册
返回顶部