Suyesh Bhatta
Suyesh Bhatta

Reputation: 151

how to filter data on button click and display on the same tab without reloading the page

i have three tabs. in one i want to load data from database and have a filter by email. when i click the button i want the data of only the user with the email to be displayed in the table in the same tab.

this is my script in view

<script>
    jQuery('.savedata').click(function (e) {
        e.preventDefault();

        jQuery.post('/gettabdata', {
            _token: window.csrf_token,
            email: jQuery('input[name="email"]').val()
        } 
        function (data) {
            var $tableBody = jQuery('#filtered-data tbody');
            $tableBody.html('');

            jQuery.each(data, function (i) {
                $tableBody.append(
                    '<tr>' +
                        '<td>' + data[i].User_id + '</td>' +
                        '<td>' + data[i].email + '</td>' +
                        '<td>' + data[i].status + '</td>' +
                        '<td>' + data[i].date + '</td>' +
                        '<td>' + data[i].time + '</td>' +

                    '</tr>'
                ); 
            });
        }

        'json');
    });
</script> 

this is my controller for the tab in which i want to see the result else {

        $user = \Auth::guard('api')->user();
        $post = $request->all();
        $email = $request->input('email');
        $cond = ' 1=1 ';
        if(!empty($post['email'])){
        $cond .= " and email like '".$post['email']."'";
        }
        $qry = 'SELECT User_id, email, status, date, time FROM profile WHERE  '.$cond.' ';
        $data = DB::select($qry);


        $response = $this->analysis($post);
        //$getdata=$this->userdata($post);

        $data = [
        'data'=>$data,
        'response'=>$response,
        //'getdata'=>$getdata

        ];

            return response()->json($data);


    }

Upvotes: 0

Views: 236

Answers (1)

Julius Fasema
Julius Fasema

Reputation: 902

try this

Route
Route::get('/get-record-by-email','Controller@getRecord');


//controller
    public function getRecord(Request $request)
    {
    $emailid= Input::get('email_id');

    $jsondata= DB::table(your-table)->where('email',$emailid)->get();
    return response()->json($jsondata);

    }


    //Ajax call
    $("#btnClick").change(function(e){

            //console.log(e);
            var email_id = e.target.value;

            //alert(email_id);
            //$token = $("input[name='_token']").val();
            //ajax
            $.get('/get-record-by-email?email_id='+email_id, function(data){
            $('#filterdata').empty();
            //console.log(data);
            $.each(data, function(index, obj){
            $('#filterdata').append( '<tr>'

                                          '<td>' + obj.User_id + '</td>' +
                                          '<td>' + obj.email + '</td>' +
                                          '<td>' + obj.status + '</td>' +
                                          '<td>' + obj.date + '</td>' +
                                          '<td>' + obj.time + '</td>' +

                                      '</tr>' );
              });
            })
        });

Upvotes: 1

Related Questions