2016-08-28 9 views
0

vuejsテーブルコンポーネントで並べ替えを有効にしようとしていますが、行を並べ替えてソートしたところで、ソートの仕組みはクリックで初めてですが、テーブル内の項目はソートに失敗します。2回目の並べ替えを繰り返すことができません

var columns = 
 
[{ 
 
\t name: 'Type', 
 
\t key: 'type', 
 
    sortOrder: 1, 
 
    callback: function(item) { 
 
     if(item.type = 'issue') 
 
     return '<i class="fa fa-bars"></i> I-'+item.id; 
 
     else 
 
     return '<i class="fa fa-check-square-o"></i> R-'+item.id; 
 
    } 
 
}, 
 
{ 
 
\t name: 'Name', 
 
\t key: 'name', 
 
    sortOrder: 1 
 
}, 
 
{ 
 
\t name: 'Created On', 
 
\t key: 'created_at', 
 
    sortOrder: 1 
 
}, 
 
{ 
 
\t name: 'Due Date', 
 
\t key: 'due_date', 
 
    sortOrder: 1 
 
}, 
 
{ 
 
\t name: 'Priority', 
 
\t key: 'priority', 
 
    sortOrder: 1 
 
}, 
 
{ 
 
\t name: 'Assigned To', 
 
\t key: 'email', 
 
    sortOrder: 1 
 
}, 
 
{ 
 
\t name: 'Severity', 
 
\t key: 'severity', 
 
    sortOrder: 1 
 
}, 
 
{ 
 
\t name: 'Workflow', 
 
\t key: 'workflow', 
 
    sortOrder: 1 
 
}]; 
 

 
var data = [{ 
 
    id: 81, 
 
    name: "a", 
 
    workflow: "backlog", 
 
    created_at: "1", 
 
    user_id: 1, 
 
    due_date: "04:09:19 \t 2016-08-06", 
 
    severity: "se", 
 
    priority: "1", 
 
    type: "1", 
 
    email: "[email protected]" 
 
}, { 
 
    id: 83, 
 
    name: "Add files or images", 
 
    workflow: "deployed", 
 
    created_at: "2016-08-01 03:09:19", 
 
    user_id: 5, 
 
    due_date: "2016-08-06", 
 
    severity: "Major", 
 
    priority: "1", 
 
    type: "issue", 
 
    email: "[email protected]" 
 
}]; 
 

 

 

 
Vue.config.debug= true; 
 

 
Vue.component('testnetic-table', { 
 
    template: '#testnetic-table', 
 
\t props: ['data', 'columns'], 
 
    data: function() { 
 
     return { 
 
      searchKey: '', 
 
      sortKey: '' 
 
     }; 
 
    }, 
 
\t methods: { 
 
     sortBy: function(index) {   
 
     this.columns[index].sortOrder = this.columns[index].sortOrder * -1; 
 
     this.sortKey = this.columns[index].key; 
 
     }, 
 
     display: function(item,key,index) { 
 
     if(this.columns[index].callback){ 
 
      return this.columns[index].callback(item); 
 
     }   
 
     else 
 
      return item[key]; 
 
     } 
 
    } 
 
}); 
 

 

 

 
new Vue({ 
 
    el: '#test', 
 
    data: function() { 
 
    return { 
 
     columns: columns, 
 
     data: data 
 
    } 
 
    } 
 
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.17/vue.js"></script> 
 
<script src="https://cdn.jsdelivr.net/lodash/4/lodash.min.js"></script> 
 
<!DOCTYPE html> 
 
<html> 
 

 
    <head> 
 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.css" /> 
 
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" 
 
    </head> 
 

 
    <body> 
 
    
 
<script type="text/x-template" id="testnetic-table"> 
 
\t <input type="text" v-model="searchKey"> 
 
\t <table class="table table-hover table-light table-responsive"> 
 
\t \t <thead> 
 
\t \t \t <tr> 
 
\t \t \t \t <th v-for="item in columns" 
 
\t \t \t \t \t \t @click="sortBy($index)"> 
 
\t \t \t \t \t \t {{ item.name | capitalize}} 
 
\t \t \t \t \t <i class="fa" :class="item.sortOrder > 0 ? 'fa-sort-desc' : 'fa-sort-asc'"></i> 
 
\t \t \t \t </th> \t \t \t 
 
\t \t \t </tr> 
 
\t \t </thead> 
 
\t \t <tbody> 
 
\t \t \t <tr v-for="item in data 
 
       | filterBy searchKey 
 
       | orderBy sortKey columns[$index].sortOrder" 
 
       > 
 
       <td v-for="cell in columns"> 
 
       {{{ display(item,cell.key,$index)}}} 
 
       </td> 
 
      </tr> 
 
\t \t </tbody> 
 
\t </table> 
 
</script> 
 

 
<div id="test"> 
 
    <testnetic-table 
 
    :columns="columns" 
 
    :data="data" 
 
    ></testnetic-table> 
 
</div> 
 

 
    </body> 
 
    
 
</html>

+0

'列[$インデックス] .sortOrder'行から来ている、いない列の' $のindex' 。 –

答えて

0

あなたはsortKeyと同じレベルで定義されたsortOrderを持っている必要があります。列がクリックされたときに列が既にSORTKEY場合、

  1. 、いずれの場合にも列のはsortOrder(そうでなければ、設定SORTKEY)
  2. 逆設定列のsortOrder
から sortOrderその

var columns = [{ 
 
    name: 'Type', 
 
    key: 'type', 
 
    sortOrder: 1, 
 
    callback: function(item) { 
 
    if (item.type = 'issue') 
 
     return '<i class="fa fa-bars"></i> I-' + item.id; 
 
    else 
 
     return '<i class="fa fa-check-square-o"></i> R-' + item.id; 
 
    } 
 
}, { 
 
    name: 'Name', 
 
    key: 'name', 
 
    sortOrder: 1 
 
}, { 
 
    name: 'Created On', 
 
    key: 'created_at', 
 
    sortOrder: 1 
 
}, { 
 
    name: 'Due Date', 
 
    key: 'due_date', 
 
    sortOrder: 1 
 
}, { 
 
    name: 'Priority', 
 
    key: 'priority', 
 
    sortOrder: 1 
 
}, { 
 
    name: 'Assigned To', 
 
    key: 'email', 
 
    sortOrder: 1 
 
}, { 
 
    name: 'Severity', 
 
    key: 'severity', 
 
    sortOrder: 1 
 
}, { 
 
    name: 'Workflow', 
 
    key: 'workflow', 
 
    sortOrder: 1 
 
}]; 
 

 
var data = [{ 
 
    id: 81, 
 
    name: "a", 
 
    workflow: "backlog", 
 
    created_at: "1", 
 
    user_id: 1, 
 
    due_date: "04:09:19 \t 2016-08-06", 
 
    severity: "se", 
 
    priority: "1", 
 
    type: "1", 
 
    email: "[email protected]" 
 
}, { 
 
    id: 83, 
 
    name: "Add files or images", 
 
    workflow: "deployed", 
 
    created_at: "2016-08-01 03:09:19", 
 
    user_id: 5, 
 
    due_date: "2016-08-06", 
 
    severity: "Major", 
 
    priority: "1", 
 
    type: "issue", 
 
    email: "[email protected]" 
 
}]; 
 

 

 

 
Vue.config.debug = true; 
 

 
Vue.component('testnetic-table', { 
 
    template: '#testnetic-table', 
 
    props: ['data', 'columns'], 
 
    data: function() { 
 
    return { 
 
     searchKey: '', 
 
     sortKey: '', 
 
     sortOrder: -1 
 
    }; 
 
    }, 
 
    methods: { 
 
    sortBy: function(index) { 
 
     const col = this.columns[index]; 
 
     if (this.sortKey === col.key) { 
 
     col.sortOrder *= -1; 
 
     } else { 
 
     this.sortKey = this.columns[index].key; 
 
     } 
 
     this.sortOrder = col.sortOrder; 
 
    }, 
 
    display: function(item, key, index) { 
 
     if (this.columns[index].callback) { 
 
     return this.columns[index].callback(item); 
 
     } else 
 
     return item[key]; 
 
    } 
 
    } 
 
}); 
 

 

 

 
new Vue({ 
 
    el: '#test', 
 
    data: function() { 
 
    return { 
 
     columns: columns, 
 
     data: data 
 
    } 
 
    } 
 
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.17/vue.js"></script> 
 
<script src="https://cdn.jsdelivr.net/lodash/4/lodash.min.js"></script> 
 
<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.css" /> 
 
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css"> </head> 
 

 
    <body> 
 

 
    <script type="text/x-template" id="testnetic-table"> 
 
     <input type="text" v-model="searchKey"> 
 
     <table class="table table-hover table-light table-responsive"> 
 
     <thead> 
 
      <tr> 
 
      <th v-for="item in columns" @click="sortBy($index)"> 
 
       {{ item.name | capitalize}} 
 
       <i class="fa" :class="item.sortOrder > 0 ? 'fa-sort-desc' : 'fa-sort-asc'"></i> 
 
      </th> 
 
      </tr> 
 
     </thead> 
 
     <tbody> 
 
      <tr v-for="item in data 
 
       | filterBy searchKey 
 
       | orderBy sortKey sortOrder"> 
 
      <td v-for="cell in columns"> 
 
       {{{ display(item,cell.key,$index)}}} 
 
      </td> 
 
      </tr> 
 
     </tbody> 
 
     </table> 
 
    </script> 
 

 
    <div id="test"> 
 
     <testnetic-table :columns="columns" :data="data"></testnetic-table> 
 
    </div> 
 

 
    </body> 
 

 
</html>

+0

男、あなたは私に掘り出し物をたくさん救った。どうもありがとう。 – Bazinga777

関連する問題