materialize chips and the addChip() method












0















I am using materialize chips in a typical "Publish Post" form where i use the chips for the tags field. All good.



Now, when i do the same in the almost identical "Edit Post" screen there is a difference, i call from the database the tags the post already has and i insert them in the element where the chips are inserted.



I solved already a couple of that idea's problems but when i want to delete a chip clicking the close icon it doesn't work, either if it's a pre-existing tag or it is a newly generated tag.



This behavior doesn't happen if I don't populate the chip's container with pre-existing tag/chips. So I have 2 options:




  • I keep trying to make the close icon work populating by myself the chip's container or

  • I use the addChip method, so probably if I let the plugin generate those pre-existing tag/chips all is going to be fixed.


My problem with the second option is I have no idea how could I make that method work in my code.



Option 1



<div id="chips1" class="chips chips-placeholder input-field">
@foreach($tags as $tag)
<div class="chip" data-tag="{{$tag}}" tabindex="0">{{$tag}}
<i class="material-icons close">close</i>
</div>
@endforeach
</div>


Option 2 (without the foreach populating the div)



<div id="chips1" class="chips chips-placeholder input-field">

</div>


and the JS



    /* I initialize the chips with a onChipAdd method   
and onDelete method to keep the variable updated */

var val = ;
$('#chips1').chips({
placeholder: 'Etiquetas...',
secondaryPlaceholder: '+Etiqueta',
onChipAdd: function () {
val = this.chipsData;
console.log(val);

},
onChipDelete: function() {
val = this.chipsData;
console.log(val);
}
});


/* ***************************************** */


/* Here populate the hidden input that is going to deliver the
data with the pre-existing chips of Option 1 */

var chipsArr = ;
var chipis = $('#chips1 div.chip');

for (let index = 0; index < chipis.length; index++) {

chipsArr.push($(chipis[index]).data('tag'));
}

$('#chips-values').val(JSON.stringify(chipsArr));

/* ***************************************** */

/* Here i push to the array the newly added tags with the val variable */

$('#publishSubmit').on('click', function (e) {
e.preventDefault();
for (let index = 0; index < val.length; index++) {
chipsArr.push(val[index].tag);
}
$('#chips-values').val(JSON.stringify(chipsArr));
console.log($('#chips-values').val());

$('form#postPublish').submit();
});









share|improve this question



























    0















    I am using materialize chips in a typical "Publish Post" form where i use the chips for the tags field. All good.



    Now, when i do the same in the almost identical "Edit Post" screen there is a difference, i call from the database the tags the post already has and i insert them in the element where the chips are inserted.



    I solved already a couple of that idea's problems but when i want to delete a chip clicking the close icon it doesn't work, either if it's a pre-existing tag or it is a newly generated tag.



    This behavior doesn't happen if I don't populate the chip's container with pre-existing tag/chips. So I have 2 options:




    • I keep trying to make the close icon work populating by myself the chip's container or

    • I use the addChip method, so probably if I let the plugin generate those pre-existing tag/chips all is going to be fixed.


    My problem with the second option is I have no idea how could I make that method work in my code.



    Option 1



    <div id="chips1" class="chips chips-placeholder input-field">
    @foreach($tags as $tag)
    <div class="chip" data-tag="{{$tag}}" tabindex="0">{{$tag}}
    <i class="material-icons close">close</i>
    </div>
    @endforeach
    </div>


    Option 2 (without the foreach populating the div)



    <div id="chips1" class="chips chips-placeholder input-field">

    </div>


    and the JS



        /* I initialize the chips with a onChipAdd method   
    and onDelete method to keep the variable updated */

    var val = ;
    $('#chips1').chips({
    placeholder: 'Etiquetas...',
    secondaryPlaceholder: '+Etiqueta',
    onChipAdd: function () {
    val = this.chipsData;
    console.log(val);

    },
    onChipDelete: function() {
    val = this.chipsData;
    console.log(val);
    }
    });


    /* ***************************************** */


    /* Here populate the hidden input that is going to deliver the
    data with the pre-existing chips of Option 1 */

    var chipsArr = ;
    var chipis = $('#chips1 div.chip');

    for (let index = 0; index < chipis.length; index++) {

    chipsArr.push($(chipis[index]).data('tag'));
    }

    $('#chips-values').val(JSON.stringify(chipsArr));

    /* ***************************************** */

    /* Here i push to the array the newly added tags with the val variable */

    $('#publishSubmit').on('click', function (e) {
    e.preventDefault();
    for (let index = 0; index < val.length; index++) {
    chipsArr.push(val[index].tag);
    }
    $('#chips-values').val(JSON.stringify(chipsArr));
    console.log($('#chips-values').val());

    $('form#postPublish').submit();
    });









    share|improve this question

























      0












      0








      0








      I am using materialize chips in a typical "Publish Post" form where i use the chips for the tags field. All good.



      Now, when i do the same in the almost identical "Edit Post" screen there is a difference, i call from the database the tags the post already has and i insert them in the element where the chips are inserted.



      I solved already a couple of that idea's problems but when i want to delete a chip clicking the close icon it doesn't work, either if it's a pre-existing tag or it is a newly generated tag.



      This behavior doesn't happen if I don't populate the chip's container with pre-existing tag/chips. So I have 2 options:




      • I keep trying to make the close icon work populating by myself the chip's container or

      • I use the addChip method, so probably if I let the plugin generate those pre-existing tag/chips all is going to be fixed.


      My problem with the second option is I have no idea how could I make that method work in my code.



      Option 1



      <div id="chips1" class="chips chips-placeholder input-field">
      @foreach($tags as $tag)
      <div class="chip" data-tag="{{$tag}}" tabindex="0">{{$tag}}
      <i class="material-icons close">close</i>
      </div>
      @endforeach
      </div>


      Option 2 (without the foreach populating the div)



      <div id="chips1" class="chips chips-placeholder input-field">

      </div>


      and the JS



          /* I initialize the chips with a onChipAdd method   
      and onDelete method to keep the variable updated */

      var val = ;
      $('#chips1').chips({
      placeholder: 'Etiquetas...',
      secondaryPlaceholder: '+Etiqueta',
      onChipAdd: function () {
      val = this.chipsData;
      console.log(val);

      },
      onChipDelete: function() {
      val = this.chipsData;
      console.log(val);
      }
      });


      /* ***************************************** */


      /* Here populate the hidden input that is going to deliver the
      data with the pre-existing chips of Option 1 */

      var chipsArr = ;
      var chipis = $('#chips1 div.chip');

      for (let index = 0; index < chipis.length; index++) {

      chipsArr.push($(chipis[index]).data('tag'));
      }

      $('#chips-values').val(JSON.stringify(chipsArr));

      /* ***************************************** */

      /* Here i push to the array the newly added tags with the val variable */

      $('#publishSubmit').on('click', function (e) {
      e.preventDefault();
      for (let index = 0; index < val.length; index++) {
      chipsArr.push(val[index].tag);
      }
      $('#chips-values').val(JSON.stringify(chipsArr));
      console.log($('#chips-values').val());

      $('form#postPublish').submit();
      });









      share|improve this question














      I am using materialize chips in a typical "Publish Post" form where i use the chips for the tags field. All good.



      Now, when i do the same in the almost identical "Edit Post" screen there is a difference, i call from the database the tags the post already has and i insert them in the element where the chips are inserted.



      I solved already a couple of that idea's problems but when i want to delete a chip clicking the close icon it doesn't work, either if it's a pre-existing tag or it is a newly generated tag.



      This behavior doesn't happen if I don't populate the chip's container with pre-existing tag/chips. So I have 2 options:




      • I keep trying to make the close icon work populating by myself the chip's container or

      • I use the addChip method, so probably if I let the plugin generate those pre-existing tag/chips all is going to be fixed.


      My problem with the second option is I have no idea how could I make that method work in my code.



      Option 1



      <div id="chips1" class="chips chips-placeholder input-field">
      @foreach($tags as $tag)
      <div class="chip" data-tag="{{$tag}}" tabindex="0">{{$tag}}
      <i class="material-icons close">close</i>
      </div>
      @endforeach
      </div>


      Option 2 (without the foreach populating the div)



      <div id="chips1" class="chips chips-placeholder input-field">

      </div>


      and the JS



          /* I initialize the chips with a onChipAdd method   
      and onDelete method to keep the variable updated */

      var val = ;
      $('#chips1').chips({
      placeholder: 'Etiquetas...',
      secondaryPlaceholder: '+Etiqueta',
      onChipAdd: function () {
      val = this.chipsData;
      console.log(val);

      },
      onChipDelete: function() {
      val = this.chipsData;
      console.log(val);
      }
      });


      /* ***************************************** */


      /* Here populate the hidden input that is going to deliver the
      data with the pre-existing chips of Option 1 */

      var chipsArr = ;
      var chipis = $('#chips1 div.chip');

      for (let index = 0; index < chipis.length; index++) {

      chipsArr.push($(chipis[index]).data('tag'));
      }

      $('#chips-values').val(JSON.stringify(chipsArr));

      /* ***************************************** */

      /* Here i push to the array the newly added tags with the val variable */

      $('#publishSubmit').on('click', function (e) {
      e.preventDefault();
      for (let index = 0; index < val.length; index++) {
      chipsArr.push(val[index].tag);
      }
      $('#chips-values').val(JSON.stringify(chipsArr));
      console.log($('#chips-values').val());

      $('form#postPublish').submit();
      });






      materialize






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 22 '18 at 0:39









      Marcos Di PaoloMarcos Di Paolo

      339




      339
























          1 Answer
          1






          active

          oldest

          votes


















          1














          I know your question is already a while ago, but maybe it will help you nevertheless. I come across the exact same problem and solved it as follows:



          I created the chip container as follows:



           <div id="chips" class="chips-placeholder"></div>
          <div id="chips_inputcontainer"></div>


          Then I created hidden inputs foreach existing chip in the database inside of the "chips_inputcontainer".



          For example like this:



           <div id="chips_inputcontainer">
          <?php foreach($chips as $chip): ?>
          <input type='hidden' name='chip[previous][<?php echo $chip['id'] ?>]' value='<?php echo $chip['text'] ?>'>
          <?php endforeach; ?>
          </div>


          At the end, I initalized the chip input with the following JavaScript Snipped:



          <script>
          $('#chips').chips({
          placeholder: 'Enter a tag',
          secondaryPlaceholder: '+Tag',
          onChipAdd: function(e, chip){
          $("#chips_inputcontainer").append("<input type='hidden' name='chip[new]' value='" + chip.innerHTML.substr(0, chip.innerHTML.indexOf("<i")) + "'>");
          },
          onChipDelete: function(e, chip){
          $('#chips_inputcontainer input[value="' + chip.innerHTML.substr(0, chip.innerHTML.indexOf("<i")) + '"]').val('**deleted**');
          },
          data: [
          <?php foreach($chips as $chip): ?>
          {tag: '<?php echo $chip['text'] ?>'},
          <?php endforeach; ?>
          ],
          });
          </script>


          This snippet creates every time when a new chip is added a hidden input with the necessary data.
          And every time, when a chip is deleted, the value of the hidden input field is set to **deleted**



          And so I know:




          • which tags are new to the database

          • which ones are existing ones

          • which id do the existing ones have in the database

          • which ones are deleted


          I hope, this will help you.






          share|improve this answer
























          • thanks a lot. I moved to another chips library, but thank a lot. I will try this next time.

            – Marcos Di Paolo
            Nov 24 '18 at 22:55











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51958431%2fmaterialize-chips-and-the-addchip-method%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          I know your question is already a while ago, but maybe it will help you nevertheless. I come across the exact same problem and solved it as follows:



          I created the chip container as follows:



           <div id="chips" class="chips-placeholder"></div>
          <div id="chips_inputcontainer"></div>


          Then I created hidden inputs foreach existing chip in the database inside of the "chips_inputcontainer".



          For example like this:



           <div id="chips_inputcontainer">
          <?php foreach($chips as $chip): ?>
          <input type='hidden' name='chip[previous][<?php echo $chip['id'] ?>]' value='<?php echo $chip['text'] ?>'>
          <?php endforeach; ?>
          </div>


          At the end, I initalized the chip input with the following JavaScript Snipped:



          <script>
          $('#chips').chips({
          placeholder: 'Enter a tag',
          secondaryPlaceholder: '+Tag',
          onChipAdd: function(e, chip){
          $("#chips_inputcontainer").append("<input type='hidden' name='chip[new]' value='" + chip.innerHTML.substr(0, chip.innerHTML.indexOf("<i")) + "'>");
          },
          onChipDelete: function(e, chip){
          $('#chips_inputcontainer input[value="' + chip.innerHTML.substr(0, chip.innerHTML.indexOf("<i")) + '"]').val('**deleted**');
          },
          data: [
          <?php foreach($chips as $chip): ?>
          {tag: '<?php echo $chip['text'] ?>'},
          <?php endforeach; ?>
          ],
          });
          </script>


          This snippet creates every time when a new chip is added a hidden input with the necessary data.
          And every time, when a chip is deleted, the value of the hidden input field is set to **deleted**



          And so I know:




          • which tags are new to the database

          • which ones are existing ones

          • which id do the existing ones have in the database

          • which ones are deleted


          I hope, this will help you.






          share|improve this answer
























          • thanks a lot. I moved to another chips library, but thank a lot. I will try this next time.

            – Marcos Di Paolo
            Nov 24 '18 at 22:55
















          1














          I know your question is already a while ago, but maybe it will help you nevertheless. I come across the exact same problem and solved it as follows:



          I created the chip container as follows:



           <div id="chips" class="chips-placeholder"></div>
          <div id="chips_inputcontainer"></div>


          Then I created hidden inputs foreach existing chip in the database inside of the "chips_inputcontainer".



          For example like this:



           <div id="chips_inputcontainer">
          <?php foreach($chips as $chip): ?>
          <input type='hidden' name='chip[previous][<?php echo $chip['id'] ?>]' value='<?php echo $chip['text'] ?>'>
          <?php endforeach; ?>
          </div>


          At the end, I initalized the chip input with the following JavaScript Snipped:



          <script>
          $('#chips').chips({
          placeholder: 'Enter a tag',
          secondaryPlaceholder: '+Tag',
          onChipAdd: function(e, chip){
          $("#chips_inputcontainer").append("<input type='hidden' name='chip[new]' value='" + chip.innerHTML.substr(0, chip.innerHTML.indexOf("<i")) + "'>");
          },
          onChipDelete: function(e, chip){
          $('#chips_inputcontainer input[value="' + chip.innerHTML.substr(0, chip.innerHTML.indexOf("<i")) + '"]').val('**deleted**');
          },
          data: [
          <?php foreach($chips as $chip): ?>
          {tag: '<?php echo $chip['text'] ?>'},
          <?php endforeach; ?>
          ],
          });
          </script>


          This snippet creates every time when a new chip is added a hidden input with the necessary data.
          And every time, when a chip is deleted, the value of the hidden input field is set to **deleted**



          And so I know:




          • which tags are new to the database

          • which ones are existing ones

          • which id do the existing ones have in the database

          • which ones are deleted


          I hope, this will help you.






          share|improve this answer
























          • thanks a lot. I moved to another chips library, but thank a lot. I will try this next time.

            – Marcos Di Paolo
            Nov 24 '18 at 22:55














          1












          1








          1







          I know your question is already a while ago, but maybe it will help you nevertheless. I come across the exact same problem and solved it as follows:



          I created the chip container as follows:



           <div id="chips" class="chips-placeholder"></div>
          <div id="chips_inputcontainer"></div>


          Then I created hidden inputs foreach existing chip in the database inside of the "chips_inputcontainer".



          For example like this:



           <div id="chips_inputcontainer">
          <?php foreach($chips as $chip): ?>
          <input type='hidden' name='chip[previous][<?php echo $chip['id'] ?>]' value='<?php echo $chip['text'] ?>'>
          <?php endforeach; ?>
          </div>


          At the end, I initalized the chip input with the following JavaScript Snipped:



          <script>
          $('#chips').chips({
          placeholder: 'Enter a tag',
          secondaryPlaceholder: '+Tag',
          onChipAdd: function(e, chip){
          $("#chips_inputcontainer").append("<input type='hidden' name='chip[new]' value='" + chip.innerHTML.substr(0, chip.innerHTML.indexOf("<i")) + "'>");
          },
          onChipDelete: function(e, chip){
          $('#chips_inputcontainer input[value="' + chip.innerHTML.substr(0, chip.innerHTML.indexOf("<i")) + '"]').val('**deleted**');
          },
          data: [
          <?php foreach($chips as $chip): ?>
          {tag: '<?php echo $chip['text'] ?>'},
          <?php endforeach; ?>
          ],
          });
          </script>


          This snippet creates every time when a new chip is added a hidden input with the necessary data.
          And every time, when a chip is deleted, the value of the hidden input field is set to **deleted**



          And so I know:




          • which tags are new to the database

          • which ones are existing ones

          • which id do the existing ones have in the database

          • which ones are deleted


          I hope, this will help you.






          share|improve this answer













          I know your question is already a while ago, but maybe it will help you nevertheless. I come across the exact same problem and solved it as follows:



          I created the chip container as follows:



           <div id="chips" class="chips-placeholder"></div>
          <div id="chips_inputcontainer"></div>


          Then I created hidden inputs foreach existing chip in the database inside of the "chips_inputcontainer".



          For example like this:



           <div id="chips_inputcontainer">
          <?php foreach($chips as $chip): ?>
          <input type='hidden' name='chip[previous][<?php echo $chip['id'] ?>]' value='<?php echo $chip['text'] ?>'>
          <?php endforeach; ?>
          </div>


          At the end, I initalized the chip input with the following JavaScript Snipped:



          <script>
          $('#chips').chips({
          placeholder: 'Enter a tag',
          secondaryPlaceholder: '+Tag',
          onChipAdd: function(e, chip){
          $("#chips_inputcontainer").append("<input type='hidden' name='chip[new]' value='" + chip.innerHTML.substr(0, chip.innerHTML.indexOf("<i")) + "'>");
          },
          onChipDelete: function(e, chip){
          $('#chips_inputcontainer input[value="' + chip.innerHTML.substr(0, chip.innerHTML.indexOf("<i")) + '"]').val('**deleted**');
          },
          data: [
          <?php foreach($chips as $chip): ?>
          {tag: '<?php echo $chip['text'] ?>'},
          <?php endforeach; ?>
          ],
          });
          </script>


          This snippet creates every time when a new chip is added a hidden input with the necessary data.
          And every time, when a chip is deleted, the value of the hidden input field is set to **deleted**



          And so I know:




          • which tags are new to the database

          • which ones are existing ones

          • which id do the existing ones have in the database

          • which ones are deleted


          I hope, this will help you.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 8:01









          M4SX5M4SX5

          405




          405













          • thanks a lot. I moved to another chips library, but thank a lot. I will try this next time.

            – Marcos Di Paolo
            Nov 24 '18 at 22:55



















          • thanks a lot. I moved to another chips library, but thank a lot. I will try this next time.

            – Marcos Di Paolo
            Nov 24 '18 at 22:55

















          thanks a lot. I moved to another chips library, but thank a lot. I will try this next time.

          – Marcos Di Paolo
          Nov 24 '18 at 22:55





          thanks a lot. I moved to another chips library, but thank a lot. I will try this next time.

          – Marcos Di Paolo
          Nov 24 '18 at 22:55


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51958431%2fmaterialize-chips-and-the-addchip-method%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Feedback on college project

          Futebolista

          Albești (Vaslui)