phplaravel-5checkboxlist

how to Select and Clear all check-boxes using Laravel


I have written a code that sends email to selected people using check-box, now I have to add select all and clear all button so that all the check-boxes should get selected/dis-selected.

Here is my code:

My controller:

 public function display(){
        $data = Input::get('agree');
        $count = count ($data);
        $this->contact( $data );
        return view('clientinfo.display', compact('data','count'));
    }

My View:

@extends('app')
@section('content')

    <h1>Welcome! Send e-mail to selected people.</h1>
    <hr>
    {!! Form::open(array('action' => 'ClientsController@display','method' => 'GET'))!!}
        @foreach($clients as $client)
            {!! Form::checkbox("agree[]", $client->email, null,['id' => $client->email]) !!}
                <article>
                    {{ $client->user_name }}
                    &nbsp;
                    {{ $client->email }}

                </article>
        @endforeach
        {!! Form::submit('Send Mail',['class' => 'btn btn-primary form-control']) !!}

    {!! Form::close() !!}
    <br/>
    @include('clientinfo.newmember')
@stop

if i add a button in my view naming select-all and clear-all how do i relate them, to select all the members within list?


Solution

  • I have made changes to my code and it worked using JavaScript.

    Here is my code:

    @extends('app')
    @section('content')
    
        <h1>Welcome! Send e-mail to selected people.</h1>
        <hr>
        {!! Form::open(array('action' => 'ClientsController@display','method' => 'GET','name'=>'f1' , 'id'=>'form_id'))!!}
        <br/>
            @foreach($clients as $client)
                <div class="form-group">
                {!! Form::checkbox("agree[]", $client->email, null,['id' => $client->email], ['class' => 'questionCheckBox']), $client->email !!}
                <br/>
                </div>
            @endforeach
        <div class="form-group">
        {!! Form::submit('Send Mail',['class' => 'btn btn-primary form-control']) !!}
        </div>
    
        <div class="form-group">
        {!! Form::button('Select All',['class' => 'btn btn-info form-control','onClick'=>'select_all("agree", "1")']) !!}
        </div>
    
        <div class="form-group">
        {!! Form::button('Clear All',['class' => 'btn btn-danger form-control','onClick'=>'select_all("agree", "0")']) !!}
        </div>
    
        {!! Form::close() !!}
    @stop
    @endsection
    

    And in my master file,I have added a JavaScript:

        <script type="text/javascript">
    
        var formblock;
        var forminputs;
    
        function prepare() {
            formblock= document.getElementById('form_id');
            forminputs = formblock.getElementsByTagName('input');
        }
    
        function select_all(name, value) {
            for (i = 0; i < forminputs.length; i++) {
                // regex here to check name attribute
                var regex = new RegExp(name, "i");
                if (regex.test(forminputs[i].getAttribute('name'))) {
                    if (value == '1') {
                        forminputs[i].checked = true;
                    } else {
                        forminputs[i].checked = false;
                    }
                }
            }
        }
    
        if (window.addEventListener) {
            window.addEventListener("load", prepare, false);
        } else if (window.attachEvent) {
            window.attachEvent("onload", prepare)
        } else if (document.getElementById) {
            window.onload = prepare;
        }
    </script>
    

    Earlier i was doing this using Jquery, that was not working, then i tried using JavaScript and it worked. I have tried various ways to add jquery in my code which are given in answers of stackoverflow.com, but nothing worked in laravel. If anyone know how to use jquery in laravel, please leave a comment.