simonexmachina / jquery-bonsai

Super lightweight jQuery tree plugin
http://simonwade.me/jquery-bonsai
MIT License
148 stars 42 forks source link

How can i assign id with my seld like data-name & data-value #22

Closed ghost closed 9 years ago

ghost commented 9 years ago

hello i have simple Question

can i assign id manually ? because right now it make id dynamically :sa: regards

simonexmachina commented 9 years ago

If you create the checkboxes yourself then it won't need to generate them for you, and should use whatever ids are in the checkboxes.

ghost commented 9 years ago

but how i can't figure out :S

simonexmachina commented 9 years ago
<ol id='checkboxes'>
  <li class='expanded'><input type='checkbox' value='root' id='foo' /> All
    <ol>
      <li><input type='checkbox' value='1' id='bar' /> One</li>
      <li><input type='checkbox' value='2' id='baz' /> Two</li>
    </ol
  </li>
</ol>
nmondal commented 9 years ago

First, thanks for putting this up! Tremendous stuff, appreciated!

Hi, It looks like a fix would be :

inputIdFor: function(listItem) {
      var actItem = listItem[0];
      var id = actItem.id ;
      if ( id == null ){
         id = listItem.data('id');
      }
      if ( id != null ){
          id = this.checkboxPrefix +id ;
          // is the thing unique as of now?
         if ( $('#' + id).length == 0 ){
             // yes, good enough, so we are good...
             return id ;
         }
      }
      // the id is now needs to be auto-generated, so...
      do {
        id = this.checkboxPrefix + Bonsai.uniqueId++;
      }
      while ($('#' + id).length > 0);
      return id;
    }

This is my guess, but please let me know if something else is required. May I also suggest that we can also use the unused attribute "idAttribute" instead of using 'id' in the data() function, or rather anywhere.

Please let us know what needs to be done.

simonexmachina commented 9 years ago

Yep, that's reasonable. Although the code can be quite a bit simpler ;)

    inputIdFor: function(listItem) {
      var id = $(listItem).data('id');
      while (!id || ($('#' + id).length > 0)) {
        id = this.checkboxPrefix + Bonsai.uniqueId++;
      }
      return id;
    },
simonexmachina commented 9 years ago

Okay I've pushed v2.1.0 which adds support for this. Be aware that it uses the data-id value as-is, it only adds the prefix if it's not specified or if the id already exists.

nmondal commented 9 years ago

Thanks a ton for the quick fix, much appreciated!

However, a quick question :

With the code here, here is the possible flaw ( please let me know if I am correct )

Example here :

<ol id='auto-checkboxes' data-name='foo'>
  <li id="__all__" class='expanded' data-value='0'>All
    <ol>
      <li data-value='1'>One</li>
      <li data-value='2'>
        Two
        <ol>
          <li data-name='baz' data-value='3'>
            Three
            <ol>
              <li data-name='baz' data-value='4' data-checked='1'>Four</li>
            </ol>
          </li>
          <li data-value='5'>Five</li>
        </ol>
      </li>
    </ol>
  </li>
</ol> 

Now in here the all id would get replaced, because the list item exists with the same id, thus an id duplication is happening. This is precisely what we do not want.

Please let me know - if my understanding is correct.

simonexmachina commented 9 years ago

Use data-id to specify the id for the checkbox.

nmondal commented 9 years ago

Thanks again! We will let you posted in case we miss anything!