[SalesForce] Adding rows to apex pageblocktable with javascript

I'm not sure if this is possible but I'd like to know if you can add and remove rows from apex pageblocktables in visualforce using javascript alone. At the moment I just call the controller to update the list that the pageblocktable is based on and then rerender it, but I've found that the delay in going to the server and back can sometimes take a couple of seconds, which isn't a great user experience.

In an effort to make the UI quicker I'm already using JS to update the cells in existing rows (by passing the id to the JS function using the {!Component.Id} notation). What I'd like to do is add an entire new row, which would then be sent back to the controller along with the rest of the original list when I save everything. At the moment any new rows I add to the table body element appear on screen fine, but don't go back to the controller.

I assume this is something to with the view state, but that's where my knowledge runs out!

Thanks for any help anyone can provide.

Best Answer

For sure there is no official way of adding rows to the controller state. I agree with you that technically it would involve changing the view state, which is encoded in some mysterious way and not documented at all. While I have no clue how difficult would be the trick, I would rather ask a question, if it's good to do any undocumented, unsupported officially acrobatics just to achieve better user experience. Perfect is the enemy of good.

Michael and sfdc_ninja actually pointed the options of implementing user interface in Salesforce. Let me sum up the approaches:

Stateful where every change in view is reflected in controller and vice versa. In Salesforce it's implemented by Visualforce and the default choice - usually most convenient for developers, and absolutely sufficient from users' perspective. It involves however many round-trips from web browser to server resulting in well-known delays.

Stateless where server-side controller doesn't know what happens in a web browser, but provides a set of services that can be called on demand. In Salesforce this approach is implemented by JavaScript Remoting, letting to control when the server should be contacted for resources or persisting data changes. It gives best flexibility and user experience, but in Salesforce requires JavaScript coding for even simplest actions (saving a form to mention).

Mixing the 2 approaches doesn't seem to be a good idea.

Related Topic