Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails render() with a fragment parameter

Is there a way to use render() with a fragment param so on page load it automatically scrolls to a specific part of the page? Similarly to how we can call

redirect(controller: "book", action: "show", fragment: "profile")
like image 657
Slavko Avatar asked Nov 25 '25 23:11

Slavko


1 Answers

You cannot pass it to render(), because by the time you're actually invoking render(), the URL has already been determined and mapped to your action; all render is doing is controlling what gets written back to the response.

The fragment must already be in the URL before the rendering controller action gets called. Here's an example:

grails-app/controllers/MyController.groovy

class MyController {
    def foo = {
        render(view: 'foo')
    }

    def quux = {
        redirect(action: 'foo', fragment: 'baz')
    }
}

grails-app/views/my/foo.gsp

<html>
  <head>
    <title>Foo</title>
  </head>
  <body>
    <a id="bar">Bar</a>
    <g:each in="${0..100}"><br/></g:each>
    <a id="baz">Baz</a>
  </body>
</html>

With various URLs:

http://example.com/myapp/my/foo     - doesn't scroll to an anchor
http://example.com/myapp/my/foo#baz - scrolls to the 'baz' anchor
http://example.com/myapp/my/quux    - scrolls to the 'baz' anchor'
like image 68
Rob Hruska Avatar answered Nov 27 '25 23:11

Rob Hruska



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!