Bootstrap Table con Webpack
Aprende a usar el Componente Vue de Bootstrap Table en tu proyecto usando webpack.
Importando JavaScript
Importa el JavaScript de Bootstrap Table agregando esta línea a tu punto de entrada de la aplicación (normalmente index.js o app.js):
import 'bootstrap-table'
Por supuesto, también puedes importar temas, localizaciones o extensiones que necesites:
// importar tema
import 'bootstrap-table/dist/themes/materialize/bootstrap-table-materialize.min.js'
// importar localización
import 'bootstrap-table/dist/locale/bootstrap-table-es-ES.min.js'
// importar extensión y dependencias
import 'tableexport.jquery.plugin'
import 'bootstrap-table/dist/extensions/export/bootstrap-table-export.min.js'
Por defecto, Bootstrap Table depende de jQuery, Bootstrap y Popper, estos se definen como peerDependencies, lo que significa que tendrás que asegurarte de agregarlos a tu package.json usando npm install --save jquery bootstrap popper.js.
Importando CSS
Importa el CSS de Bootstrap Table agregando esta línea a tu punto de entrada de la aplicación:
import 'bootstrap-table/dist/bootstrap-table.min.css'
Por supuesto, también puedes importar temas o extensiones que necesites:
// importar tema
import 'bootstrap-table/dist/themes/materialize/bootstrap-table-materialize.min.css'
// importar extensión
import 'bootstrap-table/dist/extensions/fixed-columns/bootstrap-table-fixed-columns.min.css'
Uso
<template>
<BootstrapTable
:columns="columns"
:data="data"
:options="options"
/>
</template>
<script>
import BootstrapTable from 'bootstrap-table/dist/bootstrap-table-vue.js'
export default {
components: {
BootstrapTable
},
data () {
return {
columns: [
{
title: 'ID del elemento',
field: 'id'
},
{
field: 'name',
title: 'Nombre del elemento'
},
{
field: 'price',
title: 'Precio del elemento'
}
],
data: [
{
id: 1,
name: 'Elemento 1',
price: '$1'
}
],
options: {
search: true,
showColumns: true
}
}
}
}
</script>
Plantilla de inicio
Hay un ejemplo vue-starter en el proyecto bootstrap-table-example.
plugins/jquery.js
import jQuery from 'jquery'
window.jQuery = jQuery
plugins/table.js
import 'bootstrap/dist/css/bootstrap.min.css'
import 'bootstrap-table/dist/bootstrap-table.min.css'
import './jquery.js'
import Vue from 'vue'
import 'bootstrap'
import 'bootstrap-table/dist/bootstrap-table.js'
import BootstrapTable from 'bootstrap-table/dist/bootstrap-table-vue.esm.js'
Vue.component('BootstrapTable', BootstrapTable)
main.js
import './plugins/table.js'
View.vue
<template>
<BootstrapTable :columns="columns" :data="data" :options="options"></BootstrapTable>
</template>
<script>
export default {
data () {
return {
columns: [
{
title: 'ID del elemento',
field: 'id'
},
{
field: 'name',
title: 'Nombre del elemento'
}, {
field: 'price',
title: 'Precio del elemento'
}
],
data: [
{
id: 1,
name: 'Elemento 1',
price: '$1'
},
{
id: 2,
name: 'Elemento 2',
price: '$2'
},
{
id: 3,
name: 'Elemento 3',
price: '$3'
},
{
id: 4,
name: 'Elemento 4',
price: '$4'
},
{
id: 5,
name: 'Elemento 5',
price: '$5'
}
],
options: {
search: true,
showColumns: true
}
}
}
}
</script>