Skip to content

Commit ae8c91a

Browse files
thepillYousef Sultan
authored and
Yousef Sultan
committed
Add row click event (#12)
* Use emit for row click instead of onClick prop
1 parent dea1457 commit ae8c91a

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ rows => Rows. => [
8787
]
8888
perPage => Number of rows per.. page => 10 (default) // Results per page
8989
onClick => Func. to execute on click => console.log // Function, row 1st param
90+
clickable => Rows are clickable. => true (default) //row is passed in the event payload
91+
Will fire `row-click` event //see react to click on row (below)
9092
sortable => Cause column-click to sort => true (default) // Whether sortable
9193
searchable => Add fuzzy search functionality => true (default) // Whether searchable
9294
exactSearch => Disable fuzzy search => true (default) // Whether only exact matches are returned
@@ -101,3 +103,28 @@ customButtons => Custom buttons thingy => [
101103
}
102104
]
103105
```
106+
107+
### React to click on row
108+
109+
The datatable will emit the `row-click` event if `clickable` is set to `true` (default).
110+
111+
The events payload will contain the `row object`, you can bind to the event like this:
112+
113+
```html
114+
<datatable v-on:row-click="onRowClick"></datatable>
115+
116+
<script>
117+
var app = new Vue({
118+
el: '#app',
119+
...
120+
methods: {
121+
onRowClick: function (row) {
122+
//row contains the clicked object from `rows`
123+
console.log(row)
124+
  }
125+
},
126+
})
127+
</script>
128+
...
129+
130+
```

src/DataTable.vue

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
</thead>
5757

5858
<tbody>
59-
<tr v-for="(row, index) in paginated" :class="onClick ? 'clickable' : ''" @click="click(row, index)">
59+
<tr v-for="(row, index) in paginated" :class="{ clickable : clickable }" @click="click(row)">
6060
<td v-for="column in columns" :class="column.numeric ? 'numeric' : ''" v-if="!column.html">
6161
{{ collect(row, column.field) }}
6262
</td>
@@ -111,7 +111,7 @@
111111
title: '',
112112
columns: {},
113113
rows: {},
114-
onClick: {},
114+
clickable: {default: true},
115115
customButtons: {default: () => []},
116116
perPage: {default: 10},
117117
sortable: {default: true},
@@ -164,9 +164,12 @@
164164
this.searching = !this.searching;
165165
},
166166
167-
click: function(row, index) {
168-
if (this.onClick)
169-
this.onClick(row, index);
167+
click: function(row) {
168+
if(!this.clickable){
169+
return
170+
}
171+
172+
this.$emit('row-click', row)
170173
},
171174
172175
exportExcel: function() {

0 commit comments

Comments
 (0)