derek
derek

Reputation: 173

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.

Upvotes: 1

Views: 19311

Answers (4)

Jeff Bluemel
Jeff Bluemel

Reputation: 486

I just worked out the ajax side of this yesterday. I will include the required library files and the part of the code required to initiate ajax (assuming you will be doing that since you are loading with json).

<link rel="stylesheet" href="/Includes/jquery/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="/Includes/jquery/bootstrapTable/bootstrap-table.min.css">
<link rel="stylesheet" type="text/css" href="/Includes/jquery/bootstrap3-editable/css/bootstrap-editable.css">

<script src="/Includes/jquery/jquery-3.1.1.min.js"></script>
<script src="/Includes/jquery/bootstrap/js/bootstrap.min.js"></script>
<script src="/Includes/jquery/bootstrapTable/bootstrap-table.min.js"></script>
<script src="../../Includes/jquery/bootstrap3-editable/js/bootstrap-editable.min.js"></script>
<script src="/Includes/jquery/bootstrapTable/bootstrap-table-editable.min.js"></script>

    idField: 'id',
    columns: [
        {field: 'id', title: 'ID', visible: false},
        {
            field: 'deadline', 
            title: 'Scheduling<br>Deadline', 
            editable: {
                type: 'text',
                url: './edit/edit_well_list.php'
            }
        },
        {
            field: 'visit_date', 
            title: 'Visit<br>Date',
            editable: {
                type: 'text',
                url: './edit/edit_well_list.php'
            }
        },
    ]

Upvotes: 0

Wagner Bertolini Junior
Wagner Bertolini Junior

Reputation: 1200

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

Upvotes: 5

Calvin
Calvin

Reputation: 21

Seems you are missing some css (bootstrap-editable.css) file and js (bootstrap-editable.js) file, also you included some other js (jquery.dataTables.min.js).

Try to review the source again.

Upvotes: 0

kannan D.S
kannan D.S

Reputation: 1101

Editable Table is a table manipulation plugin that turns a standard Html table into a responsive in-place editable spreadsheet with input validation based on jQuery and bootstrap 2/3. try this link

http://www.jqueryscript.net/table/Stylish-Editable-Table-Plugin-with-jQuery-Bootstrap-2-3-Editable-Table.html

Upvotes: 0

Related Questions