2017-03-24 19 views
2

私の要件は次のとおりです。JQueryを使用して動的に作成されたドロップダウンで値を削除して追加するにはどうすればよいですか?

1 - ドロップダウンからオプションを選択すると、現在のドロップダウン以外のドロップダウンには表示されません。

2 - 上記の選択したオプションを他のものに変更すると、以前選択したオプションがすべてのドロップダウンに再び表示されます(&)。新しいものは他のすべてのドロップダウンから削除する必要があります。

HTML

<table class="table table-bordered centeredContent multiSelectFunctionality" id="table"> 
    <tbody> 
    <tr> 
     <td> 
     <button type="button" class="btn btn-plus custom-icon glyphicon glyphicon-plus" onclick="cloneRow();" title="Add Row"></button> 
     </td> 
     <td> 
     <select class="selectedItem form-control" name="selectedItem" id="selectedItem_1"> 
      <option value="entityName">Entity Name</option> 
      <option value="transmitter_mac">Tag Mac</option> 
      <option value="tag_number">Tag Number</option> 
      <option value="sub_category">Sub Category</option> 
      <option value="name">Department Name</option> 
      <option value="in_time">In Time</option> 
      <option value="out_time">Out Time</option> 
     </select> 
     </td> 
     <td> 
     <input class="form-control searchItem" placeholder="Enter Search item" name="searchItem" /> 
     <!-- <input type="hidden" name="counterValue" id="counterValue" value=""> --> 
     </td> 
    </tr> 
    </tbody> 
</table> 

はJavaScript

function cloneRow() { 
    counter++; 
    if (counter >= 7) { 
    return; 
    } else { 
    var a = $("table#table").find("tbody"); 
    var b = a.find("tr:first"); 
    $trLast1 = a.find("tr:last"); 
    $trNew = b.clone(); 
    $trNew.find("button#dltbtn").remove().end(); 
    $trNew.find("td:first").append('<button type="button" class="btn btn-plus custom-icon glyphicon glyphicon-minus" onclick="deleteRow(this);" title="Remove Row"></button>'); 
    $trLast1.after($trNew); 
    } 
} 

function deleteRow(a) { 
    $(a).closest("tr").remove(); 
    counter--; 
} 
+0

を見ることができますHTML内にID「dltbtn」の要素を追加します。 – Twisty

答えて

0
 var rowCount = 1; 
    function cloneRow(self) { 
     if(rowCount==7){ 
      $(".glyphicon-plus").attr("disabled", true); 
      return;    
     } 
     else{ 
     var a = $("table#table").find("tbody"); 
     var b = a.find("tr:first"); 
     $trLast1 = a.find("tr:last"); 
     $trNew = b.clone(); 
     $trNew.find("button#dltbtn").remove().end(); 
     $trNew.find("td:first").append('<button type="button" class="btn btn-plus custom-icon glyphicon glyphicon-minus" onclick="deleteRow(this);" title="Remove Row"></button>'); 
     $trLast1.after($trNew); 
     rowCount = $('table#table tr:last').index() + 1; 
     manage_opns(); 
     } 
    } 

    $(document).on('change', '.selectedItem', function(e, el){ 
     manage_opns(); 
    }); 

    function manage_opns(){ 
     $("select.selectedItem option").attr('disabled',false); 
     $("select.selectedItem").each(function(i, el){ 
      var cur_val = $(el).val(); 
      if(cur_val != ''){ 
       $("select.selectedItem option[value='"+cur_val+"']").attr('disabled',true); 
       $(el).find("option[value='"+cur_val+"']").attr('disabled',false); 
      } 
     }); 
    } 

    function deleteRow(a) { 
     $(a).closest("tr").remove(); 
     rowCount = $('table#table tr:last').index() + 1; 
     if(rowCount <= 7){ 
      $(".glyphicon-plus").attr("disabled", false); 
     } 
     manage_opns(); 
    } 

HTMLコード

<table class="table table-bordered centeredContent" id="table"> 
         <tbody> 
          <tr> 
          <td><button type="button" class="btn btn-plus custom-icon glyphicon glyphicon-plus" onclick="cloneRow(this);" title="Add Row"></button> 
          </td> 
          <td> 
          <select class="selectedItem required-report input-normal input-width-45" name="selectedItem">         
            <option value="entityName">Entity Name</option> 
            <option value="transmitter_mac">Tag Mac</option> 
            <option value="tag_number">Tag Number</option> 
            <option value="sub_category">Sub Category</option> 
            <option value="name">Department Name</option> 
            <option value="in_time">In Time</option> 
            <option value="out_time">Out Time</option> 
          </select> 
           </td> 
          <td> 
           <input class="searchItem input-normal input-width-45 required-report" placeholder="Enter Search item" name="searchItem"/> 

          </td> 
          </tr> 
         </tbody> 
        </table> 

今、あなたは、私はボタンが表示されていない機能

2

それはあなたが何をしようとして決定するために少し難しいです。私はここでそれを刺した。

例:https://jsfiddle.net/Twisty/1L152xyj/

はJavaScript

function cloneRow($obj) { 
    $obj = $obj.length ? $obj : $("#table tbody"); 
    counter++; 
    if (counter >= 7) { 
    $(".btn-plus").button("disable"); 
    return; 
    } else { 
    var b = $obj.find("tr:first"); 
    $trLast1 = $obj.find("tr:last"); 
    $trNew = b.clone(); 
    $trNew.find(".btn-plus").remove(); 
    $trNew.find("td:first").append($("<button>", { 
     type: "button", 
     class: "btn btn-minus custom-icon glyphicon glyphicon-minus", 
     title: "Remove Row" 
    }).button({ 
     icon: "ui-icon-minus" 
    }).click(function() { 
     deleteRow(this); 
    })); 
    $trLast1.after($trNew); 
    } 
} 

function deleteRow(a) { 
    $(a).closest("tr").remove(); 
    $(".btn-plus").button("enable"); 
    counter--; 
} 

var counter = 0; 

$(function() { 
    $(".btn-plus").button({ 
    icon: "ui-icon-plus" 
    }); 
    $(".btn-plus").click(function() { 
    cloneRow($("#table tbody")); 
    }); 
}); 

お役に立てば幸いです。

更新

私は、これはあなたが記述されたものに近いかもしれないと思います。コメントしてお知らせください。

https://jsfiddle.net/Twisty/1L152xyj/6/

HTMLスニペット

<tr id="row-1"> 
     <td> 
     <button type="button" class="btn btn-plus custom-icon glyphicon glyphicon-plus" title="Add Row"></button> 
     </td> 
     <td> 
     <select class="selectedItem form-control" name="selectedItem[]" id="selectedItem_1"> 
      <option value="entityName">Entity Name</option> 
      <option value="transmitter_mac">Tag Mac</option> 
      <option value="tag_number">Tag Number</option> 
      <option value="sub_category">Sub Category</option> 
      <option value="name">Department Name</option> 
      <option value="in_time">In Time</option> 
      <option value="out_time">Out Time</option> 
     </select> 
     </td> 
     <td> 
     <input class="form-control searchItem" placeholder="Enter Search item" name="searchItem[]" /> 
     <!-- <input type="hidden" name="counterValue" id="counterValue" value=""> --> 
     </td> 
    </tr> 
**JavaScript 

はJavaScript

function cloneRow($obj) { 
    $obj = $obj.length ? $obj : $("#table tbody"); 
    counter++; 
    if (counter >= 7) { 
    $(".btn-plus").button("disable"); 
    return; 
    } else { 
    var selectIndex = $obj.find("tr:first option:selected").index(); 
    var b = $obj.find("tr:first"); 
    $trLast1 = $obj.find("tr:last"); 
    $trNew = $("<tr>", { 
     id: "row-" + counter 
    }); 
    b.find("td").each(function() { 
     $(this).clone().appendTo($trNew); 
    }); 
    $trNew.find(".btn-plus").remove(); 
    $trNew.find("td:first").append($("<button>", { 
     type: "button", 
     class: "btn btn-minus custom-icon glyphicon glyphicon-minus", 
     title: "Remove Row" 
    }).button({ 
     icon: "ui-icon-minus" 
    }).click(function() { 
     deleteRow(this); 
    })); 
    $trNew.find("select") 
     .attr("id", "selectedItem_" + counter) 
     .find("option").eq(selectIndex).prop("selected", true); 
    $trLast1.after($trNew); 
    } 
} 

function deleteRow(a) { 
    $(a).closest("tr").remove(); 
    $(".btn-plus").button("enable"); 
    counter--; 
} 

var counter = 1; 

$(function() { 
    $(".btn-plus").button({ 
    icon: "ui-icon-plus" 
    }); 
    $(".btn-plus").click(function() { 
    cloneRow($("#table tbody")); 
    }); 
}); 

これは、すべてのクローンが、それらはユニークであることを確認するためにid属性を更新します。また、オプションの選択したプロパティをクローンします。

アップデート2

あなたはそうのように行うことができ、行が複製されるオプション、無効にしたい場合:

$trNew.find("select") 
    .attr("id", "selectedItem_" + counter) 
    .find("option").eq(selectIndex).prop("disabled", true); 

例:https://jsfiddle.net/Twisty/1L152xyj/7/

アップデート3

このオプションを他のselect要素のppearも、最初の選択が変更されたときはいつでも削除する必要があります。

var options = [{ 
    value: "entityName", 
    label: "Entity Name", 
}, { 
    value: "transmitter_mac", 
    label: "Tag Mac" 
}, { 
    value: "tag_number", 
    label: "Tag Number" 
}, { 
    value: "sub_category", 
    label: "Sub Category" 
}, { 
    value: "name", 
    label: "Department Name" 
}, { 
    value: "in_time", 
    label: "In Time" 
}, { 
    value: "out_time", 
    label: "Out Time" 
}]; 

このオプションでは、いつでもオプションを追加、削除、変更できます。簡単な関数は、これを助けることができる:

function replaceSelect($target, key) { 
    $target.find("select").find("option").remove(); 
    $.each(options, function(k, v) { 
    if (key !== k) { 
     $target.find("select").append($("<option>", { 
     value: v.value 
     }).html(v.label)); 
    } 
    }); 
} 

実施例:https://jsfiddle.net/Twisty/1L152xyj/8/

+0

私の要件は次のとおりです。 1 - ドロップダウンからオプションを選択すると、現在のドロップダウン以外のドロップダウンには表示されません。 2 - 上記の選択したオプションを他のものに変更すると、以前に選択したオプションがすべてのドロップダウンに再び表示され、新しいドロップダウンが他のすべてのドロップダウンから削除されます。 –

+0

できます。それは少しの作業が必要です。私の更新ではヒントがあります。これまでに何を試しましたか? – Twisty

+0

私の要件をお読みください。私はあなたが別の答えを与えたと思う、私はいくつかの別の機能が期待されていた。最初のドロップダウンでentityNameを選択し、エンティティ名を次回作成するドロップダウンで無効にする必要があるとします。これをもう一度見てください。 –

関連する問題