Find your content:

Search form

You are here

How to Using JSON with visualforce

 
Share

I'm new on visualforce and JSON, and I have a page that have many inputFields want to insert them by clicking on save, and get them back on click on edit button for example, I searched on google for examples but i found them not clear enough to understand, for me. I have an object name Book__c with Id,BookNum__c,BookTitle__c,Section__c (reference to sections object).

the input fields sorted in the VF page with repeat like this

<apex:pageBlock id="books">
     <apex:repeat value="{!counter}" var="count" rows="16">
         <apex:outputLabel value="{!count}" />
     </apex:repeat>
     <apex:repeat value="{!counter}" var="count" rows="16">
         <apex:inputField value="{!Book__c.BookTitle__c}" id="input"/>
     </apex:repeat>
     <apex:commandButton value="save" action="{!save}" />
     <apex:commandButton value="Get to Edit" action="{!getBooksToEdit}" reRender="books" />
</apex:pageBlock>

Controller

public integer[] counter{set;get{
        return counter;
    }}
    public BooksController() {
            counter = new list<Integer>();
            for (integer i=101; i<=105; i++)
                counter.add(i);
    }

Image1


Attribution to: user3003810

Possible Suggestion/Solution #1

Your posted code is missing the idea that to have many books displayed your controller will have to provide an array of books.

If you have a Section__c custom object that is a child of your Book__c custom object then a controller something like this:

public with sharing class BooksController {
    public Book__c[] books {
        get {
            if (books == null) {
                books = [
                        select Id, BookNum__c, BookTitle__c, (select Name from Sections__r)
                        from Book__c
                        order by BookNum__c
                        limit 10000
                        ];
            }
            return books;
        }
        private set;
    }
}

and a page something like this:

<apex:pageBlock id="books">
    <apex:pageBlockTable value="{!books}" var="b">
        <apex:column value="{!b.BookNum__c}" />
        <apex:column headerValue="Book Title">
             <apex:inputField value="{!b.BookTitle__c}" />
        </apex:column>
        <apex:column headerValue="Sections">
            <apex:pageBlockTable value="{!b.Sections__r}" var="s">
                <apex:column value="{!s.Name}" />
            </apex:pageBlockTable>
        </apex:column>
    </apex:pageBlockTable>
</apex:pageBlock>

should give your roughly the output you want.

If instead of having a Section__c object you want to have say a Sections__c text field on Book__c that holds JSON, then you will have to do more work yourself. I would suggest adding jQuery to your page and when the page loads transforming the JSON into HTML inserted next to the other book information. But if you want editing too of the sections then you will have to add JavaScript code for new/update/delete of sections and that is more work still.


Attribution to: Keith C
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/32622

My Block Status

My Block Content