Search code examples
jqueryjsontwitter-bootstrap-3bootstrap-table

Edit fields in a table bootstrap-table


I'm trying to make the fields in a table to Bootstrap-Table that is editable like this example but I can't do it: http://issues.wenzhixin.net.cn/bootstrap-table/#extensions/editable.html

I'm loading a JSON data, sorts the columns, but I can't do that every field in the table is editable.

<head>
    <title>custom-sort</title>
    <meta charset="utf-8">
    <link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="assets/bootstrap/css/bootstrap-table.min.css">
    <link rel="stylesheet" href="assets/examples.css">
    <script src="assets/jquery.min.js"></script>
    <script src="assets/jquery.dataTables.min.js"></script>
    <script src="assets/bootstrap/js/bootstrap.min.js"></script>
    <script src="assets/bootstrap-table/src/bootstrap-table-custom.min.js"></script>
    <script src="assets/bootstrap-table/src/bootstrap-table-editable.js"></script>
    <script src="ga.js"></script>
</head>
<body>
    <div class="container">
        <h1>Custom Sort</h1>
        <p>Use <code>sorter</code> column option to define the custom sort of bootstrap table.</p>
        <table id="table" class="table table-bordered table-striped" data-editable="true" data-toggle="table" data-url="json/data1.json" data-pagination="true"></table>
    </div>
$('#table').bootstrapTable({
    url: 'json/data1.json',
    columns: [{
        field: 'id',
        title: 'Item ID',
        sortable: 'true',
        editable: 'true'
    }, {
        field: 'name',
        title: 'Item Name',
        sortable: 'true',
        editable: 'true'
    }, {
        field: 'price',
        title: 'Item Price',
        sortable: 'true',
        editable: 'true'
    }, ]
});

var $table = $('#table');

$(function () {
    $table.on('click-row.bs.table', function (e, row, $element) {
        $('.success').removeClass('success');
        $($element).addClass('success');
    });
});

JSON

[{
    "id": 0,
    "name": "Item 0",
    "price": "$0"
},{
    "id": 1,
    "name": "Item 1",
    "price": "$1"
},{
    "id": 2,
    "name": "Item 2",
    "price": "$2"
}]

Can you help me to do all table fields editable please.


Solution

  • The below example show a table initialization with a field 'name' that is editable:

     function initTable() {
            $table.bootstrapTable({
                height: getHeight(),
                columns: [
                    [
                        {
                            field: 'state',
                            checkbox: true,
                            rowspan: 2,
                            align: 'center',
                            valign: 'middle'
                        }, {
                            title: 'Item ID',
                            field: 'id',
                            rowspan: 2,
                            align: 'center',
                            valign: 'middle',
                            sortable: true,
                            footerFormatter: totalTextFormatter
                        }, {
                            title: 'Item Detail',
                            colspan: 3,
                            align: 'center'
                        }
                    ],
                    [
                        {
                            field: 'name',
                            title: 'Item Name',
                            sortable: true,
                            editable: true,
                            footerFormatter: totalNameFormatter,
                            align: 'center'
                        }, {
                            field: 'price',
                            title: 'Item Price',
                            sortable: true,
                            align: 'center',
                            editable: {
                                type: 'text',
                                title: 'Item Price',
                                validate: function (value) {
                                    value = $.trim(value);
                                    if (!value) {
                                        return 'This field is required';
                                    }
                                    if (!/^\$/.test(value)) {
                                        return 'This field needs to start width $.'
                                    }
                                    var data = $table.bootstrapTable('getData'),
                                        index = $(this).parents('tr').data('index');
                                    console.log(data[index]);
                                    return '';
                                }
                            },
                            footerFormatter: totalPriceFormatter
                        }, {
                            field: 'operate',
                            title: 'Item Operate',
                            align: 'center',
                            events: operateEvents,
                            formatter: operateFormatter
                        }
                    ]
                ]
            });
    

    Source: http://issues.wenzhixin.net.cn/bootstrap-table/#options/detail-view.html