Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
416 views
in Technique[技术] by (71.8m points)

Call view in Laravel Controller with anchor tag

I need to call a view in a Laravel Controller, with parameters and with Anchor Tag. I have this code in my controller:

return view('plans/editPlanView',
    ['plan' => $plan,
    'patient' => $patient,
    'aliments'=>$aliments, 'menu'=>$menu, 'tabName'=>$tabName]);

But i need to add an Anchor tag to land in a specific section of the page. I can't use

return Redirect::to(URL::previous() . "#whatever");

proposed in other posts because i need to pass some parameters.

I think there are some base problem, trying with console this:

 $('html, body').animate({
    scrollTop: $('#whatever').offset().top
 }, 1000);

scrolling to the desired section does not work. it seems the page makes a small snap but always returns to the top.

Update

I have found the cause of the problem. At the bottom of the blade page I have the following code, without it the anchor tag works fine. Adding it the page makes a small scroll to return to the head. I need to use the datepicker, how can I fix the problem and get the anchor tag to work?

@push('scripts')
    <script type="text/javascript">
        $(document).ready(function () {            
            $('.date').datepicker({
                firstDayOfWeek: 1,
                weekDayFormat: 'narrow',
                inputFormat: 'd/M/y',
                outputFormat: 'd/M/y',
                markup: 'bootstrap4',
                theme: 'bootstrap',
                modal: false
            });
        });
    </script>
@endpush
question from:https://stackoverflow.com/questions/65641752/call-view-in-laravel-controller-with-anchor-tag

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can create the method showPage() in your contoller for example TestController

public function showPage(Request $request)
{
    $plan = $request->plan;
    ...

    return view('plans/editPlanView', [
        'plan' => $plan,
        'patient' => $patient,
        'aliments'=>$aliments, 'menu'=>$menu, 'tabName'=>$tabName
    ]);
}

Then create a route for rendering that view

Route::get('/someurl', 'TestController@showPage')->name('show-page');

And then in your methods you can use something like that:

$url = URL::route('show-page', ['#whatever']);
return Redirect::to($url)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...