7

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.

  • 2
    I suspect that if you add/remove rows to a PageBlockTable with javascript, you'll lose out on the two way binding and will need to use Javascript remoting to persist your data. – Michael Welburn Dec 21 '13 at 17:43
  • 1
    Agreed that will likely lose the data bind. The advantage of the remoting is the speed as their is no view state to pass, but you lose the convenience of the rerender as you have to append the rows to the table manually in the call back function. Perhaps you could post your code so we can take a look and see what would work best – Chris Duncombe Dec 21 '13 at 18:00
  • I think everyone has pretty much confirmed what I was thinking - I think I will tackle this with js remoting and see what I can come up with. No code to post at the moment, it's all in my head :-) Thanks for your help though! – user4976 Dec 22 '13 at 15:44
1

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.

  • Thanks - that has pretty what I thought, but it's good to get confirmation! I suspect javascript remoting is the way to go in my situation. – user4976 Dec 22 '13 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.