Generate next sequence of an "Indice"

Asked

Viewed 77 times

2

I’m having a problem generating a sequence for an "Indice".

As an example, if I took the number 1.2, I wanted to generate the number 1.2.4 as a sequence or if I took 1.2.2 I wanted to generate 1.2.2.1

<select>
  <option>1.1       </option>
  <option>1.1.1     </option>
  <option>1.1.2     </option>
  <option>1.2       </option>
  <option>1.2.1     </option>
  <option>1.2.2     </option>
  <option>1.2.3     </option>
  <option>1.2.3.1   </option>
  <option>1.3       </option>
  <option>1.3.1     </option>
</select>

in case I would have to check how many elements this "Indice" would have and generate a new, but I’m having trouble with his logic.

If anyone can help. Thanks in advance.

  • 1

    What is the default generation of these numbers? There is a maximum value for each sub-version?

  • 1

    It has to be inside a <select>?? If it can be a common list type ul li is very easy to solve...

  • 1

    What do you mean "generate"? You want to create a new option with the sequence?

  • In this case, I just wanted to generate the next sequence, no matter if it is an input, span or other tags, and there is no limit on the generation or a maximum value

1 answer

2


You can do it this way. The explanations are commented on in the code:

$("select").change(function(){
   
   var opts = $("option", this); // pego todos os options
   var opt  = $(":selected", this); // pego o option selecionado
   var idx  = opt.index(); // pego o índice do option selecionado
   var txt  = opt.text().trim(); // pego o texto do option e elimino os espaços nas bordas
   var m; // flag para testar regex

   // aqui verifico se existe uma subsequência com .1
   for(var x=idx+1; x<opts.length; x++){
      var re = new RegExp("^"+txt+".1");
      m = re.test($(opts[x]).text().trim());
      if(m) break;
   }

   if(m){ // se existe, varro o resto do select para definir a próxima sequência
      var y = 2; // começo com 2, já que o 1 já existe
      for(var x=idx+2; x<opts.length; x++){
         var txt2 = $(opts[x]).text().trim();
         var re = new RegExp("^"+txt+"."+y);
         m = re.test(txt2);
         if(!m) break;
         y++;
      }
      var seq = txt+"."+y;
   }else{ // se não existe, crio a subsequência com .1
      var seq = txt+".1";
   }
   
   console.log(seq);
   
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
  <option>1.1       </option>
  <option>1.1.1     </option>
  <option>1.1.2     </option>
  <option>1.2       </option>
  <option>1.2.1     </option>
  <option>1.2.2     </option>
  <option>1.2.3     </option>
  <option>1.2.3.1   </option>
  <option>1.3       </option>
  <option>1.3.1     </option>
  <option>1.3.2     </option>
  <option>1.3.3     </option>
  <option>1.3.4     </option>
  <option>1.3.5     </option>
  <option>1.3.6     </option>
  <option>1.3.7     </option>
  <option>3         </option>
</select>

  • Thanks for the answer, but I found a problem, say I have a 1.3.1 index that goes up to 1.3.7, and then I add an index 3, and ask to generate a sequence of it, it will generate an index of 3.8 and not a 3.1

  • yes, in case you would need the options of 1.3.1 to 1.3.7 to make the mistake, because he is not comparing the whole index, he only took the 3.1 to 3.7 of the index 1, and generated the sequence 3.8 when I asked him to generate the sequence of the index 3

  • did not solve the problem, if I select an index with only one character, it just adds . 1 at the end, if I already had 3.1 it continued generating 3.1 as a sequence. What if

  • And continuing with the error, I had a 1.1.1.2.3 index and tried to generate a 1.1.2 index, having the 1.1.2.1 and 1.1.2.2 index but it generated a 1.1.2.4 index

  • Okay! I’ll go over the code.

  • Ready. Take a look now.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.