title: Populate table in Laravel with ajax?
body: <p>I have a Laravel application where I create a page layout, adding a table to it as a "content" variable (pretty much all from tutorials I found). Here's the controller action:</p>
  
  <pre><code>public function getMain() {
      $js_config = Category::all();
      $resources = Resource::all()-&gt;take(100);
     $this-&gt;layout-&gt;content = View::make('categories.show')-&gt;with('js_config', $js_config)-&gt;with('resources', $resources);
  }
  </code></pre>
  
  <p>This uses the main layout template and inserts this table using the content variable:</p>
  
  <pre><code>&lt;table class="table table-striped table-bordered"&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;td&gt;ID&lt;/td&gt;
              &lt;td&gt;Name&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
      @foreach($resources as $key =&gt; $value)
          &lt;tr&gt;
              &lt;td&gt;{{ $value-&gt;id }}&lt;/td&gt;
              &lt;td&gt;{{ $value-&gt;title }}&lt;/td&gt;
  
          &lt;/tr&gt;
      @endforeach
      &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>But then comes the problem: I have a jstree where the user can select nodes, triggering a jQuery method:</p>
  
  <pre><code>$('#jstree2').on("changed.jstree", function (e, data) {
      console.log(data.selected);
      $.get("filter", { category: data.selected })
          .done(function (resultdata) {
              //Test only, this returns the data wanted in json, which I stringify just to display in test
              alert("Data Loaded: " + JSON.stringify(resultdata));
          });
  });
  </code></pre>
  
  <p>The jQuery calls this action method in the controller:</p>
  
  <pre><code>public function getFilter()
    {
        $input = Input::get('category.0');
  
        $categories = Category::find($input);
  
  
        //category is the name from the model below
        return Response::json(array(
            'error' =&gt; false,
            'category' =&gt; $categories-&gt;toArray()),
            200
        );
    }
  </code></pre>
  
  <p>(The reason there's an array as input is I eventually want to be able to allow picking multiple nodes in the tree)</p>
  
  <p>This action gets the data from the DB correctly and returns it as json. The callback in the jQuery above then alerts this at the moment, just as a test.</p>
  
  <p>But what I really want to do, of course, is to repopulate the table.  Now, as you can see I have used Bootstrap to create a pretty table and all, and I just want to be able to let the user repopulate it at will, without refreshing the page.</p>
  
  <p>But I don't know how to do that, except by painstakingly recreate this table in some sort of string return value, but that doesn't seem like a good idea.</p>
  
  <p>I'm hoping there's some way of passing the return value back to the view and have it reload the values in the table, utilizing the same "sub view" that I loaded in the php variable "content" as described above?</p>
  
  <p>Any help greatly appreciated!</p>
  
  <p>EDIT:</p>
  
  <p>As requested, here's a sample of the json (taken from the browser console output, and it's actually not the categories table, but the same format):</p>
  
  <p>[{"id":"1","title":"Transportation AC 4494","created_by":"4","modified_by":null},{"id":"2","title":"Safety First AC 4294","created_by":"3","modified_by":null},{"id":"3","title":"Warranty AC 34066","created_by":"4","modified_by":null}]</p>
  
  <p>EDIT 2 (Just realized there was some crap from the controller in the previous edit of the json, so I changed it to a cleaner sample now)</p>
  
  <p>EDIT 3:</p>
  
  <p>I have made this work by creating the table rows in jQuery:</p>
  
  <pre><code>var trHTML = '';
                  $.each(resultdata, function (i, item) {
                      trHTML += '&lt;tr&gt;&lt;td&gt;' + item.id + '&lt;/td&gt;&lt;td&gt;' + item.title + '&lt;/tr&gt;';
                  });
                  $('#ajaxtable').html(trHTML);
  </code></pre>
  
  <p>But mainly I'm hoping this might explain my question better: this is not what I wanted to do. What I would have wanted was to just create a partial view and then load that ready-made view with the jquery:</p>
  
  <p>A partial view like this:</p>
  
  <pre><code>&lt;table class="table table-striped table-bordered" id="resultstable"&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;td&gt;ID&lt;/td&gt;
              &lt;td&gt;Name&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody id="ajaxtable"&gt;
      @foreach($resources as $key =&gt; $value)
          &lt;tr&gt;
              &lt;td&gt;{{ $value-&gt;id }}&lt;/td&gt;
              &lt;td&gt;{{ $value-&gt;title }}&lt;/td&gt;
          &lt;/tr&gt;
      @endforeach
      &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>I tested this by creating and calling a new function in the controller from the jquery code:</p>
  
  <pre><code>   public function getTable()
      {
          $resources = Resource::all()-&gt;take(5);
          return View::make('categories.results')-&gt;with('resources', $resources);
      }
  </code></pre>
  
  <p>But it doesn't work. Although it does indeed give me the html of that view, it is unprocessed. I.e, the foreach loop is not resolved, but still there as code in the table. See the image:</p>
  
  <p><img src="https://i.stack.imgur.com/4gSwD.png" alt="enter image description here"></p>
  
  <p>So how can I load this view with the jquery code? It feels to me that even if the jquery creation of table rows works, doing the view in php and then just loading it with jquery should be the more correct way of doing it...?</p>