How to pass a fieldset to javascript?

When my user clicks a button, I want to access the fieldset by javascript. Can i do that?

  <apex:commandbutton value="Send"  onClick="check('{$ObjectType.Customization__c.FieldSets.Fields_for_fieldChooser}')" />

javascript

<script>
    function check(apiNameList) {

        for (var i = 0; i < apiNameList.length; i++) {
            var apiName = apiNameList[i];
            console.log(apiNameList[i]);

            var updateVal = apiName + "_update";
            var deleteVal = apiName + "_delete";
            var dontaddVal = apiName + "_do_not_add";
            //    if ( document.getElementById(updateVal).checked === false && document.getElementById(deleteVal).checked === false && document.getElementById(dontaddVal).checked === false){
            alert("Please choose an option for each field");
            break;

            //     }

        }
    }
</script>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

To get quotes round the field names so you have a JavaScript array of strings, you need to turn the Apex array into a JavaScript array by for example using an apex:repeat:

<apex:page>
<script>
var fields = [
<apex:repeat var="f" value="{!$ObjectType.Customization__c.FieldSets.Fields_for_fieldChooser}">
    '{!f}',
</apex:repeat>
];
</script>
</apex:page>

The above Visualforce produces this:

<script>
var fields = [
    'Field1__c',
    'Field2__c',
   ' Field3__c',
];
</script>

Then adapt you other JavaScript to reference this array. (Note that the spurious trailing comma is allowed in JavaScript).

Or if you need the JavaScript arrays to be inline, this will work:

... onClick="check([<apex:repeat var="f" value="{!$ObjectType.TestRelationship__c.FieldSets.TestFieldSet}">'{!f}', </apex:repeat>])" ...


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x