【问题标题】:Sorting a Table with Vue使用 Vue 对表格进行排序
【发布时间】:2016-01-06 17:52:05
【问题描述】:

如果我想使用 Vue.js 对表格进行排序,我是否应该始终在 ajax 请求中接收要排序的数据?为 v-repeat 工作?或者哪种方法可以使用来自控制器的数据?

我想知道这是否是使用 Vue.js 时的唯一方法 我试图以传统方式使用已经获取的表,然后对其标题字段进行排序,例如。 (标题,created_at)

 @foreach($sliders as $slider)
              <tbody>
                <tr>
                  <td>{{$slider->nombre}}</td>
                  <td>{{$slider->mensaje}}</td>
                  <td>{!!Html::image($slider->thumb_path)!!}</td>
                  <td>{{$slider->publicado}}</td>
                  <td>{{$slider->created_at}}</td>
                   <td>{!!link_to('admin/slider/'.$slider->id.'/edit', 'editar','class="btn btn-primary"')!!}
                      {!!link_to('admin/slider/'.$slider->id, 'borrar','class="btn btn-danger"')!!}
                  </td>
 @endforeach

我希望我的桌子像 http://javascriptbook.com/code/c12/sort-table.html 但是使用 Vue.Js 和从控制器接收的数据

【问题讨论】:

    标签: laravel vue.js


    【解决方案1】:

    您可以使用orderBy Vue 过滤器,例如:

    <table>
      <thead>
        <tr>
          <th v-repeat="column: columns">
            <a href="#" v-on="click: sortBy(column)">{{ column }}</a>
          </th>
        </tr>
      </thead>
    
      <tbody>
        <tr v-repeat="users | orderBy sortKey reverse">
          <td>{{ name }}</td>
          <td>{{ age }}</td>
        </tr>
      </tbody>
    </table>
    

    data: {
      sortKey: 'name',
    
      reverse: false,
    
      columns: ['name', 'age'],
    
      users: [
        { name: 'John', age: 50 },
        { name: 'Jane', age: 22 }
        // ...
      ]
    },
    
    methods: {
      sortBy: function(sortKey) {
        this.reverse = (this.sortKey == sortKey) ? ! this.reverse : false;
    
        this.sortKey = sortKey;
      }
    }
    

    [demo]

    【讨论】:

    • 你好@pespantelis,您提出的答案涉及 v-repeat 指令,但如果我必须使用这种方法,我将不得不使用 Ajax 请求获取我的数据,对吗?然后使用 v-repeat 填充视图。但是,如果我的数据已经被调用到视图中并且我只想使用 Vue.js 对给定的表进行排序,请查看这个线程。 laracasts.com/discuss/channels/laravel/sorting-a-table-with-vue
    • 顺便说一句,我会接受你的回答,如果没有办法像非异步方式那样对表进行排序,我想使用但我是 Vue.js 的新手 v-el。据我所知,Vue.js 像 Jquery 一样是 View 和 Javascript 之间的翻译器,它让您在编写 js 代码时更轻松?
    • 我会给你的答案是有效的,因为我找不到我需要的答案,所以我看到 Vue.js 就像你在 laracasts 教程中解释的那样工作。谢谢。
    • @Pantelis Peslis - 如何使用 Vue 2.*
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-21
    • 2016-04-10
    • 1970-01-01
    • 2021-11-27
    • 2012-12-25
    相关资源
    最近更新 更多