Autogenerate incremental topic number without using AUTOINC

  • Tip Category - Developing Wiki Applications
  • Tip Added By - PadraigLennon - 17 Jul 2009 - 12:27
  • Extensions Used - SpreadSheetPlugin
  • Useful To - Beginners
  • Tip Status - Under Review
  • Related Topics -

Problem

Suppose you have a set of topics which have the naming format 0000nnnn-Some-Free-Text

e.g.

  • 00000001-A-Test-Topic
  • 00000002-Another-Test-Topic
For a Wiki application form you may need to get the next sequence number (i.e. in this case 00000003) for a form that you want users to define the name of the topic

Using AUTOINC does not work as it expects a common ending…

i.e. Only this naming conventions would work using AUTOINC:

  • 00000001-A-Test-Topic
  • 00000002-A-Test-Topic
  • ...

Context

This tip could be used in the creation of a Wiki Application where users need to define their own topic name which has a unique prefix number in the topic name.

Solution

You can get around this by performing a search on topics with 0000* which gives the last used number in the sequence (e.g. 00000003-A-Test-Topic )

%SEARCH{ "000" scope="title" web="%WEB%" nonoise="on" reverse="on" limit="1" topic="0000*" format="$percntCALC{$SET(count , $topic)}$percnt"}%

   * Set NEXT_JOB_DOC_ID=%CALC{"$EVAL( $VALUE($GET(count)) + 1 )"}% 

From here you need to use some JavaScript, such as the example found here:

http://sujithcjose.blogspot.com/2007/10/zero-padding-in-java-script-to-add.html

<script language="javascript"> 
<!-- 
function zeroPad(num,count) 
{ 
   var numZeropad = num + ''; 
   while(numZeropad.length < count) { 
      numZeropad = "0" + numZeropad; 
   } 
   return numZeropad; 
} 
//--> 
</script> 

Known Uses

You could use this code in a form such as the following:

<fieldset>
<legend><strong> &nbsp;Add Job Document&nbsp; </strong></legend> 
<table> 
<form action="%SCRIPTURLPATH{"save"}%/%WEB%/" method="post"> 
</div> 
<table border="0" cellpadding="0" cellspacing="6"> 
<input type="hidden" name="jobid" size="40" value="%NEXT_JOB_DOC_ID%"  /> 
<input type="hidden" name="templatetopic" value="TemplateDoc"  /> 
<tr> 
  <td align="right" width="12%"> *Job Doc ID:* </td> 
  <td  width="10%"><input type="text" name="topic" size="40" class="foswikiInputField" value="" onfocus="this.form.topic.value=zeroPad(this.form.jobid.value,8)"/></td><td >The next available seq number will be entered automatically when you click on the field. You can overwrite the number if you wish. The job name should be of the form *0000xxxx-A-Test-Job* </td>
</tr> 
<tr>
  <td align="right" width="12%"> *Job Summary:* </td>
  <td width="18%"><input type="text" name="Job_Summary" size="80" class="foswikiInputField" value="" /></td><td >Enter a one line summary of the job. This is shown in the table below </td>
</tr>
<tr><td align="right"/>&nbsp;</td><td><input type="submit" class="foswikiSubmit" value="Create Job Document"/ > </td></tr>
</form> 
</table> 
</fieldset> 

When the user clicks on the topic text field the next number automatically appears, and the user can update the appending text as appropriate as shown here:

example-form.PNG

Known Limitations

This tip assumes that JavaScript is enabled on the browser

  • PaulHarvey also asks: Is it possible to end up with duplicate sequence numbers, if more than one browser is working on creating a new topic - each browser having searched the topic list, they will all calculate the same "latest" sequence number?

See Also

BestPracticeTipsForm edit

Category Developing Wiki Applications
I Attachment Action Size Date Who Comment
example-form.PNGPNG example-form.PNG manage 8 K 17 Jul 2009 - 12:23 PadraigLennon  
Topic revision: r3 - 09 Dec 2009, PaulHarvey
The copyright of the content on this website is held by the contributing authors, except where stated elsewhere. See Copyright Statement. Creative Commons License    Legal Imprint    Privacy Policy