groxwitch00's avatar

Livewire 3 and change the other component

Hello, I've installed a livewire 3 and in my dashboard there's jquery steps, when the first time that load dashboard it is working, but when i try to wire:navigate and return to dashboard page, it said that $(...).steps is not a function

@push('scripts') $(document).ready(function () { console.log('ready'); });

<script src="{{ asset('assets/vendor_components/jquery-steps-master/build/jquery.steps.js')}}"></script>
<script src="{{ asset('assets/vendor_components/jquery-validation-1.17.0/dist/jquery.validate.min.js')}}"></script>

<script>
    var form = $(".validation-wizard").show();
    $(".validation-wizard").steps({
        headerTag: "h6"
        , bodyTag: "section"
        , transitionEffect: "none"
        , titleTemplate: '<span class="step">#index#</span> #title#'
        , labels: {
            finish: "Submit"

        }
        , onStepChanging: function (event, currentIndex, newIndex) {
            return currentIndex > newIndex || !(3 === newIndex && Number($("#age-2").val()) < 18) && (currentIndex < newIndex && (form.find(".body:eq(" + newIndex + ") label.error").remove(), form.find(".body:eq(" + newIndex + ") .error").removeClass("error")), form.validate().settings.ignore = ":disabled,:hidden", form.valid())
        }
        , onFinishing: function (event, currentIndex) {
            return form.validate().settings.ignore = ":disabled", form.valid()
        }
        , onFinished: function (event, currentIndex) {
            console.log('finished');
            Livewire.dispatch('submitted-form');


        }
    });
    $(".validation-wizard").validate({
        ignore: "input[type=hidden]"
        , errorClass: "text-danger"
        , successClass: "text-success"
        , highlight: function (element, errorClass) {
            $(element).removeClass(errorClass)
        }
        , unhighlight: function (element, errorClass) {
            $(element).removeClass(errorClass)
        }
        , errorPlacement: function (error, element) {
            error.insertAfter(element)
        }
        , rules: {
            email: {
                email: !0
            }
        }
    });

    $('.repeater').on('change', function (e) {
        @this.groupa = $('.repeater').repeaterVal();
    });






    $('.repeater').repeater({
        show: function () {
            $(this).slideDown();
        },

        hide: function (deleteElement) {
            if(confirm('Are you sure you want to delete this record?')) {
                $(this).slideUp(deleteElement);
            }
        },

    });



</script>

@endpush

0 likes
14 replies
LaryAI's avatar
Level 58

The error message $(...).steps is not a function indicates that the steps function from the jQuery Steps library is not being recognized. This could be due to a few reasons:

  1. The jQuery Steps library is not being loaded properly.
  2. The Livewire component is being re-rendered without re-initializing the jQuery Steps plugin.

To resolve this issue, you can try the following steps:

  1. Make sure that the jQuery Steps library and the jQuery Validation library are being loaded correctly. Check that the file paths in the <script> tags are correct and that the files exist in the specified locations.

  2. Wrap the jQuery Steps initialization code inside a Livewire wire:ignore directive. This will prevent Livewire from re-rendering the component and breaking the jQuery Steps plugin.

Here's an example of how you can modify the code:

@push('scripts')
    <script src="{{ asset('assets/vendor_components/jquery-steps-master/build/jquery.steps.js')}}"></script>
    <script src="{{ asset('assets/vendor_components/jquery-validation-1.17.0/dist/jquery.validate.min.js')}}"></script>

    <script>
        $(document).ready(function () {
            console.log('ready');

            // Wrap the initialization code inside a Livewire wire:ignore directive
            Livewire.on('submitted-form', function () {
                $('.validation-wizard').steps({
                    headerTag: "h6",
                    bodyTag: "section",
                    transitionEffect: "none",
                    titleTemplate: '<span class="step">#index#</span> #title#',
                    labels: {
                        finish: "Submit"
                    },
                    onStepChanging: function (event, currentIndex, newIndex) {
                        return currentIndex > newIndex || !(3 === newIndex && Number($("#age-2").val()) < 18) && (currentIndex < newIndex && (form.find(".body:eq(" + newIndex + ") label.error").remove(), form.find(".body:eq(" + newIndex + ") .error").removeClass("error")), form.validate().settings.ignore = ":disabled,:hidden", form.valid());
                    },
                    onFinishing: function (event, currentIndex) {
                        return form.validate().settings.ignore = ":disabled", form.valid();
                    },
                    onFinished: function (event, currentIndex) {
                        console.log('finished');
                        Livewire.dispatch('submitted-form');
                    }
                });

                $('.validation-wizard').validate({
                    ignore: "input[type=hidden]",
                    errorClass: "text-danger",
                    successClass: "text-success",
                    highlight: function (element, errorClass) {
                        $(element).removeClass(errorClass);
                    },
                    unhighlight: function (element, errorClass) {
                        $(element).removeClass(errorClass);
                    },
                    errorPlacement: function (error, element) {
                        error.insertAfter(element);
                    },
                    rules: {
                        email: {
                            email: true
                        }
                    }
                });

                $('.repeater').on('change', function (e) {
                    @this.groupa = $('.repeater').repeaterVal();
                });

                $('.repeater').repeater({
                    show: function () {
                        $(this).slideDown();
                    },
                    hide: function (deleteElement) {
                        if (confirm('Are you sure you want to delete this record?')) {
                            $(this).slideUp(deleteElement);
                        }
                    }
                });
            });
        });
    </script>
@endpush

By wrapping the initialization code inside the Livewire wire:ignore directive, the Livewire component will not re-render the code and the jQuery Steps plugin will be initialized properly.

Remember to include the @push and @endpush directives in your Blade template to include the JavaScript code.

I hope this helps! Let me know if you have any further questions.

groxwitch00's avatar

@vincent15000 I've tried but it stil gets error "$(...).steps is not a function" and i wonder how to reinitialize all script in @push('scripts') just as like DOMContentLoaded, I've tried wire navigaed to event listener,

1 like
groxwitch00's avatar
document.addEventListener('DOMContentLoaded', function() {
  $('#reservation').daterangepicker();

})

It works when it first load, but when i try to navigate to the other page, and i try to add a code 'livewire:load' or init, initlized, it doesnt work, do you have any suggestion to solve this?

document.addEventListener('livewire:load', () => {
// get script from asset daterangepicker.js and run a function
$.getScript('{{ asset('assets/vendor_components/bootstrap-daterangepicker/daterangepicker.js')}}', function() {
  $('#reservation').daterangepicker();
});      
})
1 like
groxwitch00's avatar

@vincent15000 I've already done it, could you please give me an example to reintialize such select2 when you try to the other component?

1 like
vincent15000's avatar

@groxwitch00 No sorry I can't. I just try to help you because I also discover the new Livewire version, but I don't have tested this yet.

groxwitch00's avatar
groxwitch00
OP
Best Answer
Level 1

Well, I've solution for this : In the layout.app

add

@livewireScriptConfig

and add a 'data-navigate-once' in script tag and type="text/javascript"

It looks like :

<script  type="text/javascript"  data-navigate-once>

    document.addEventListener('DOMContentLoaded', function () {
        // Your JS CODE here...
    });

    document.addEventListener('livewire:navigated', () => {
        // Your JS CODE here...
    })

</script>

I've added when it loads from dom or navigated

1 like
groxwitch00's avatar

Well I hope, there's better solution for this, because i have to loaded it from 2 event listener, whether it DOMLoaded or navigated, I try to livewire:init, it doesnt work

1 like
groxwitch00's avatar

@vincent15000 thanks, but I just noticed that the script wasn't reinitiated when I try to wire:navigate the other components:

{{-- it works when DOMContentLoaded but not wire:navigated --}}
<script src="{{ asset('assets/vendor_components/jquery-steps-master/build/jquery.steps.js')}}"></script>


<script>

    $(".validation-wizard").steps(); // it works when wire:navigated but not when DOMContentLoaded
    console.log('test-this-page'); // it works when wire:navigated and DOMContentLoaded

</script>

Please or to participate in this conversation.