Cheers02's avatar

Send ajax from view to controlelr, controller send back json and pagination suitable from json

this ini my problem.. i have a lot of data, example 1000 data, and i create pagination 15 data each page in table.. and i have 3 link to filter my data, this link send ajax string to my controller, and then my controller will send back json to my view.. but i have problem i cant replace my table to the json from my controller

this is my javascript
function filter($color)
  {
    var colors = {color:$color};
     $.ajax({
        url:'/filter',
        type: 'POST',
        headers:{"content-type" : "application/json"},
        data: JSON.stringify(colors),
        success: function(response) {
          console.log(response.filter);
        }
       
    });

  }
this is my controller
  public function filter(Request $request)
  {
    $color = $request->color;
   $arrFilter = array();
    .
    . 
        .
    return response()->json(['filter' => $arrFilter]);
  }

when i try to console.log in javascript its success..

0 likes
3 replies
RamjithAp's avatar
function filter($color)
  {
    var colors = {color:$color};
     $.ajax({
        url:'/filter',
        type: 'POST',
        headers:{"content-type" : "application/json"},
        data: JSON.stringify(colors),
        success: function(response) {
        $('#tableId').after("<tr><td>response.filter.data1</td><td>response.filter.data1</td></tr>");
        $('#tableId').remove();
        }
       
    });

  }
Cheers02's avatar

@RamjithAp the result from ur code like thisresponse.filter.id response.filter.name and btw if u like that i just can get 1 data, my data more than 1

RamjithAp's avatar
Level 10
function filter($color)
  {
    var colors = {color:$color};
     $.ajax({
        url:'/filter',
        type: 'POST',
        headers:{"content-type" : "application/json"},
        data: JSON.stringify(colors),
        success: function(response) {
         $('#tableId').remove();
         var data= '';
         for (var i = 0; i < response.filter.length; i++) {
          data = data+"<tr><td>response.filter[i]['id']</td><td>response.filter[i]['name']</td>
           </tr>";
          }
        }
       $('#tableId').after(data);
    });

  }

Please or to participate in this conversation.