Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bottelet's avatar

Vue grid component

Hello im trying to make the grid as VueJS has in their documentation(vuejs.org/examples/grid-component.html), but can't really get it to work, i made my array into a Jsonobj in my Controller

    public function index()
    {
        $clients = Client::all()->toJson();
        //dd($clients);
        return view('clients.index')->withClients($clients);

    }

And this is the code from the docs im using in the view


<!-- component template -->
<script type="text/x-template" id="grid-template">
  <table>
    <thead>
      <tr>
        <th v-for="key in columns"
          @click="sortBy(key)"
          :class="{active: sortKey == key}">
          @{{key | capitalize}}
          <span class="arrow"
            :class="sortOrders[key] > 0 ? 'asc' : 'dsc'">
          </span>
        </th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="
        entry in data
        | filterBy filterKey
        | orderBy sortKey sortOrders[sortKey]">
        <td v-for="key in columns">
          @{{entry[key]}}
        </td>
      </tr>
    </tbody>
  </table>
</script>

<!-- demo root element -->
<div id="demo">
  <form id="search">
    Search <input name="query" v-model="searchQuery">
  </form>
  <demo-grid
    :data="gridData"
    :columns="gridColumns"
    :filter-key="searchQuery">
  </demo-grid>
</div>

<script>  // register the grid component
Vue.component('demo-grid', {
  template: '#grid-template',
  props: {
    data: Array,
    columns: Array,
    filterKey: String
  },
  data: function () {
    var sortOrders = {}
    this.columns.forEach(function (key) {
      sortOrders[key] = 1
    })
    return {
      sortKey: '',
      sortOrders: sortOrders
    }
  },
  methods: {
    sortBy: function (key) {
      this.sortKey = key
      this.sortOrders[key] = this.sortOrders[key] * -1
    }
  }
})

// bootstrap the demo
var demo = new Vue({
  el: '#demo',
  data: {
    searchQuery: '',
    gridColumns: ['name', 'power', 'test'],
    gridData: [
      { name: 'Chuck Norris', power: Infinity },
    ]
  }
})</script>

im wonderig if there someway to make the gridData get the users from the controller like so:

    gridData: [
      {{$clients }},
    ]
  }

I know this is not the way but hopefull the example is understandable, and also does it have to be json? or can i keep it like this in the Controller

$clients = Client::all()
0 likes
0 replies

Please or to participate in this conversation.