Fabrik Text Area line Counting

FatFingers

New Member
Hi,
This is my first post so sorry if im putting this in the wrong area and its probably a simple issue:)

I have a text area Element in a Fabrik form which the user will scan in barcodes.
The barcode adds a carriage return after scanning so I get:

Barcode1
Barcode2
Barcode3


Now what I would like to do is to get it to count the number of barcodes entered and show it in a seperate text area/field

So it would be:

1 Barcode 1
2 Barcode 2
3 Barcode 3

You Have scanned 3 Barcodes


Can this be done ?
Any help would be greatly appreciated.
 
Create a calc element in the same group.
Set Ajax calculation to Yes
Set Ajax observe fields to {your_table_name___your_scanned_textarea_name}
Set Calc on load to Yes

Use this php code as the Calculation (change your_table_name and your_scanned_textarea_name as appropriate)
PHP:
$txt = '{your_table_name___your_scanned_textarea_name}';
$txt_a = explode("\n",$txt);
$linecount = 0;
$html = "<ol>";
foreach($txt_a as $line)
{
  if(!empty($line))
  {
    $linecount++;
    $html .= "<li>".$line."</li>";
  } 
} 
$html .= "</ol><p>You have scanned ".$linecount." Barcodes</p>";
return $html;

You now have a read-only calc element containing a numbered list of the scanned items (as taken from the field/element {your_table_name___your_scanned_textarea_name} ) - followed by your barcode count summary line.:)
 
hi,
Thanks that works great on the search form but just three more questions.
on the from entry page I get:
<ol><ol><p>You have scanned 0 Barcodes</p>
and when I enter the data to the barcode field it doesn't change.
can I just have it to show the total of barcodes scanned rather than re list them? and save the value to the database.

Sorry but i'm a noob on fabric but it really seems a great product.

Thanks again.
 
Is this a form with repeat groups - and you are putting the new calc element in the list for the parent table while the actual textarea that gets the scanned data is in the repeat group? That would cause that issue.

I've used the code I provided in a test form (where both elements are in the same group) and it works fine.

I can work on a javascript solution if you need a way to store the count. Back in a bit.
 
Here's a javascript version
If you put this code as a javascript load event for the textarea element (your_table_name___your_scanned_textarea_name) that holds the scanner info,
this code will take each line, number them, add the counter remark, and put the results in another textarea field.

This code assumes there is already another textarea element (e.g. your_table_name___your_fixed_textarea_name) to hold the altered data results.
Code:
var thisId = this.id;
var txt = this.innerText || this.textContent;
var txt_a = txt.split("\n");
var newtext = "";
var linecount = 0;
for (x = 0; x < txt_a.length; ++x)
{
  if(txt_a[x] !== ""){
    linecount++;
    newtext += linecount + ". " + txt_a[x] + "\n";
  }
}
newtext += "\n"+"You have scanned " + linecount + " barcodes";
 
/*IF the element is in a repeat group use the next 2 lines
var index_pos = thisId.substring(thisId.lastIndexOf("_")+1);
var replace_el = document.getElementById("your_table_name___your_fixed_textarea_name_"+index_pos);
ELSE use this line */
var replace_el = document.getElementById("your_table_name___your_fixed_textarea_name");
 
replace_el.innerHTML = newtext;

If you only want to store the scanned count to a standard fabrik 'field' element -
then rather than creating the textarea field, create a field set as 'Integer' to hold that value.
And remove any lines in the js code that contain 'newtext'
And in the js code change 'your_table_name___your_fixed_textarea_name' to whatever the name of that new field element
And change the last line of the code to...
replace_el.value = linecount;

Oh - and you would also have to add a 2nd javascript event for the element - i.e. duplicate that javascript code (as a "change" event) in your_table_name___your_scanned_textarea_name. Because you would also want any changes to the scanned codes to be reflected and displayed in your new element in real-time.
 
hi,
Thanks for your help on this it really is appreciated.
think I still need to work on it a bit but getting in the right direction :)
The main thing I need to work out is to get either version to be live.
By that I mean. sorry not great at explaining...
User scans in barcode into scanned text area then as it does a carriage return the text saying you have scanned 0 Barcodes changes to you have scanned 1 Barcode
just cant seem to get it to run real time but it does when you go back and view records.
Getting late now :) so will try again tomorrow.

Thanks again for your guidance.
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top