Skip to content

Commit 719da3f

Browse files
committed
update actions
1 parent cfcea82 commit 719da3f

9 files changed

+354
-52
lines changed

Diff for: README.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export default {
8888
## Documentation
8989
Since it's a wrapper, all/most features are provided by the [jQuery DataTable](https://datatables.net/manual/) library.
9090

91-
More documentation for this library, to come.
91+
More documentations below.
9292

9393
## Additional Headers
9494
Since options are completely exposed, simply use the native method per [jQuery DataTable example](https://editor.datatables.net/manual/security#Prevention)
@@ -108,4 +108,16 @@ options: {
108108

109109
If you haven't already figured it out, ajax is basically the signature of [jQuery.ajax](http://api.jquery.com/jquery.ajax/) which is demonstrated here wrapped as [jQuery DataTable ajax pipeline](https://datatables.net/examples/server_side/pipeline.html)
110110

111+
## Row Action Buttons
112+
Use `data-action` attribute to automatically wire up any action button/elements. To render action button/element in a row, simply define field like below (also, see example App):
113+
```javascript
114+
actions: {
115+
label: 'Actions',
116+
render: () => {
117+
return '<a href="javascript:void(0);" data-action="edit" class="btn btn-primary btn-sm"><i class="mdi mdi-square-edit-outline"></i> Edit</a>' +
118+
'<span data-action="delete" class="btn btn-danger btn-sm"><i class="mdi mdi-delete"></i> Delete</span>'
119+
}
120+
}
121+
```
122+
111123
# MIT

Diff for: dist/build.js

+280-31
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: dist/build.js.map

-1
This file was deleted.

Diff for: example/App.vue

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
<template>
22
<div id="app">
3-
<vdtnet-table :fields="fields" :opts="options" />
3+
<vdtnet-table
4+
:fields="fields"
5+
:opts="options"
6+
@edit="alert('row edit button clicked')"
7+
@delete="alert('row delete button clicked')"
8+
/>
49
</div>
510
</template>
611

@@ -60,9 +65,21 @@ export default {
6065
}
6166
},
6267
phone: { label: 'Phone' },
63-
website: { label: 'Website' }
68+
website: { label: 'Website' },
69+
actions: {
70+
label: 'Actions',
71+
render: () => {
72+
return '<a href="javascript:void(0);" data-action="edit" class="btn btn-primary btn-sm"><i class="mdi mdi-square-edit-outline"></i> Edit</a>' +
73+
'<span data-action="delete" class="btn btn-danger btn-sm"><i class="mdi mdi-delete"></i> Delete</span>'
74+
}
75+
}
6476
}
6577
}
78+
},
79+
methods: {
80+
alert(msg) {
81+
window.alert(msg)
82+
}
6683
}
6784
}
6885
</script>

Diff for: lib/vue-datatables-net.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: lib/vue-datatables-net.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vue-datatables-net",
33
"description": "Vue jQuery DataTable.net wrapper component ",
4-
"version": "0.7.4",
4+
"version": "0.7.5",
55
"author": "[email protected]",
66
"license": "MIT",
77
"main": "lib/vue-datatables-net.min.js",
@@ -11,7 +11,7 @@
1111
"scripts": {
1212
"dev": "cross-env NODE_ENV=development webpack-dev-server --open --hot",
1313
"build:lib": "rm -rf ./lib && bili src/index.js",
14-
"build:example": "rm -rf ./dist && cross-env NODE_ENV=production webpack --progress --hide-modules",
14+
"build:example": "rm -rf ./dist && cross-env NODE_ENV=demo webpack --progress --hide-modules",
1515
"lint": "eslint --ext .js,.vue src/",
1616
"lint-fix": "eslint --fix --ext .js,.vue src/",
1717
"check-outdated": "npm outdated"

Diff for: src/VdtnetTable.vue

+36-10
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ export default {
4747
},
4848
fields: {
4949
type: Object
50+
},
51+
jquery: {
52+
type: Object
5053
}
5154
},
5255
data() {
@@ -64,10 +67,11 @@ export default {
6467
},
6568
mounted() {
6669
const vm = this
70+
const jq = vm.jquery || window.jQuery
6771
6872
// allow user to override default options
6973
if (vm.opts) {
70-
vm.options = window.jQuery.extend({}, vm.options, vm.opts)
74+
vm.options = jq.extend({}, vm.options, vm.opts)
7175
}
7276
7377
if (vm.url) {
@@ -112,20 +116,42 @@ export default {
112116
}
113117
}
114118
115-
const $el = window.jQuery(vm.$refs.table)
119+
const $el = jq(vm.$refs.table)
116120
vm.dataTable = $el.DataTable(vm.options)
117121
118-
// wire up view, edit, and/or delete button
119-
// var d = table.row( this ).data();
120-
$el.on('click', 'tbody > tr [data-action]', (e) => {
122+
// wire up edit, delete, and/or action buttons
123+
$el.on('click', '[data-action]', (e) => {
121124
e.preventDefault()
122125
e.stopPropagation()
123-
const $this = $(this)
124-
const action = $this.data('action')
126+
const target = jq(e.target)
127+
let that = target
128+
let action = that.attr('data-action')
129+
while(!action) {
130+
// don't let it propagate outside of container
131+
if (that.hasClass('vdtnet-container')) {
132+
// no action, simply exit
133+
return
134+
}
135+
that = that.parent()
136+
action = that.attr('data-action')
137+
}
138+
139+
// only emit if there is action
125140
if (action) {
126-
const row = vm.dataTable.row($this.closest('tr'))
127-
const data = row.data()
128-
vm.$emit(action, {data, row, $this})
141+
// detect if row action
142+
let tr = that.closest('tr')
143+
if (tr) {
144+
if (tr.hasClass('child')) {
145+
tr = tr.prev()
146+
}
147+
const row = vm.dataTable.row(tr)
148+
const data = row.data()
149+
vm.$emit(action, {data, row, that})
150+
} else {
151+
// not a row click, must be other kind of action
152+
// such as bulk, csv, pdf, etc...
153+
vm.$emit(action, {target})
154+
}
129155
}
130156
})
131157
}

Diff for: webpack.config.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ module.exports = {
1010
},
1111
externals: {
1212
jquery: 'jQuery',
13-
vue: 'Vue',
14-
vue: 'Vue'
13+
Vue: 'Vue'
1514
},
1615
module: {
1716
rules: [

0 commit comments

Comments
 (0)