Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Table of Checkboxes

New Here ,
Feb 11, 2025 Feb 11, 2025

Copy link to clipboard

Copied

I am very new to writing scripts in PDF and have so far been able to learn a lot just from reading discussions here in this community so thank you to all those that contribute with questions and answers.  

 

As seen in the attachment, I have a table of checkboxes in my form.  It is already set so that only 1 box per column can be checked but I am wondering how to also make it so that the user can only check boxes in the same row.  For example, if user selects the first "AddAlt1" CB that has an export value of "Cont1", then in "AddAlt2" CB and "AddAlt3" CB, only the boxes with an export value of "Cont1" can be selected. 

TOPICS
PDF forms

Views

192
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
2 ACCEPTED SOLUTIONS
Community Expert ,
Feb 11, 2025 Feb 11, 2025

Copy link to clipboard

Copied

Yes this is possible.

But to be clear. If no checkboxes are selected, then any checkbox can be selected? any row, any column?

Then only checkboxes in the same row as the first selected checkbox can be selected?

To do this you will need to track the value of the first check box selected. All other selections are then rejected if they don't match the saved value. The saved value can only change if all boxes are first unchecked.   

I would also suggest renaming the fields to include a block prefix, like this "block.AddAlt1", "block.AddAlt2". 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

View solution in original post

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 11, 2025 Feb 11, 2025

Copy link to clipboard

Copied

Tracking the selected value is done by saving it and watching for all changes to the checkboxes. Probably the best approach is to use a text field. The calculation event is called anytime any value on a form is changed. So it works for both saving the value and tracking changes.  For the example code I'll assume the renaming I suggested. 

 

Here's the script for a custom calculation on a hidden text field.

if(event.source && (event.source.name.split(".").shift() == "Block"))
{// only process field changes from the checkbox block
	if(event.source.value == "Off")
	{// Checkbox is turning off
		if(this.getField("Block").getArray().every(a=>a.value == "Off"))
		{// all checks are off so clear saved value
			event.value = "";
		}
	}
	else if(event.target.value == "")
	{// No previously selected value, save current selection
		event.value = event.source.value;
	}
	else
	{// Current value, reject selected if it doesn't match
        if(event.target.value != event.source.value)
		{// Doesn't match so turn off
		//	Can't directly change field that is triggering this event, so delay
			app.setTimeOut('this.getField("' + event.source.name + '").value = "Off";',10);
		}
	}
}

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

View solution in original post

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 11, 2025 Feb 11, 2025

Copy link to clipboard

Copied

Yes this is possible.

But to be clear. If no checkboxes are selected, then any checkbox can be selected? any row, any column?

Then only checkboxes in the same row as the first selected checkbox can be selected?

To do this you will need to track the value of the first check box selected. All other selections are then rejected if they don't match the saved value. The saved value can only change if all boxes are first unchecked.   

I would also suggest renaming the fields to include a block prefix, like this "block.AddAlt1", "block.AddAlt2". 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 11, 2025 Feb 11, 2025

Copy link to clipboard

Copied

Correct, If no checkboxes are selected, then any checkbox can be selected, any row, any column. Then only checkboxes in the same row as the first selected checkbox can be selected.  That is correct.  

 

Could you point me in the right direction as to how to track the value and then make the others rejected if they don't match the saved value.  Renaming the fields would not be a problem.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 11, 2025 Feb 11, 2025

Copy link to clipboard

Copied

Tracking the selected value is done by saving it and watching for all changes to the checkboxes. Probably the best approach is to use a text field. The calculation event is called anytime any value on a form is changed. So it works for both saving the value and tracking changes.  For the example code I'll assume the renaming I suggested. 

 

Here's the script for a custom calculation on a hidden text field.

if(event.source && (event.source.name.split(".").shift() == "Block"))
{// only process field changes from the checkbox block
	if(event.source.value == "Off")
	{// Checkbox is turning off
		if(this.getField("Block").getArray().every(a=>a.value == "Off"))
		{// all checks are off so clear saved value
			event.value = "";
		}
	}
	else if(event.target.value == "")
	{// No previously selected value, save current selection
		event.value = event.source.value;
	}
	else
	{// Current value, reject selected if it doesn't match
        if(event.target.value != event.source.value)
		{// Doesn't match so turn off
		//	Can't directly change field that is triggering this event, so delay
			app.setTimeOut('this.getField("' + event.source.name + '").value = "Off";',10);
		}
	}
}

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 11, 2025 Feb 11, 2025

Copy link to clipboard

Copied

LATEST

Thank you so much! works perfectly. 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines