bootstrap的DataTable行单元格增加按钮并绑定事件

背景

本人前端是一塌糊涂,这几年没怎么搞过前端,因为有需要,所以用了bootrstrap。

现在有个需求,我有一个table,需要在每一行的最后一个单元格内增加两个按扭,并绑定点击事件,点击这个按扭的事件,获得这一行的数据,然后进行相关业务处理,如下:

在最后1列增加扣费和充值2个按钮。

这个列表用的是DataTable。

在网上搜了几篇解决方案,千篇一律,我也就没试可行不,不符合我的期望。

翻了官方文档和源码,被我发现了2个属性,想到了2个方案解决了。

我的html代码:

 <table id="vip-user-list" class="table table-bordered table-hover ">
                               
                            </table>

服务器数据:

{
  "data": [
    {
      "name": "Tiger Nixon",
      "id": "12345678",
      "tel": "187xxxxxxxx",
      "money": "$320,800",
      "updateTime": "2019/04/25"
    },
    {
      "name": "Hello World",
      "id": "87654321",
      "tel": "187xxxxxxxx",
      "money": "$58,800",
      "updateTime": "2019/10/25"
    }
  ]
}

方案一

使用ajax.dataSrc属性,处理原始数据,以字符串拼接的方式,拼接上这两个按钮标签,并绑定点击事件。

关于这个属性描述如下,文档地址:http://datatables.club/reference/option/ajax.dataSrc.html

ajaxOption ajax不定时一讲 选项基本继承了 jQuery.ajax 所有的选项,但是Datatables额外提供了 dataSrc属性来改变从服务器返回的数据给Datatables,或者是操作数据从一种形式转换成另一种形式(比如xml、json、yaml等)。 这么做是因为 ajaxOption ajax不定时一讲 的 success 选项不能被改变-Datatables内部自己加载数据完成时使用。

代码如下:

<script>
    $(function () {
        $('#vip-user-list').DataTable({
            'paging': true,
            'lengthChange': false,
            'searching': true,
            'ordering': false,
            'info': true,
            'autoWidth': false,
            "ajax": {
                "url": './data/vip_user_list.json',
                "dataSrc": function (json) {
                    for (var i = 0, ien = json.data.length; i < ien; i++) {
                        var data = json.data[i];
                        var rechargeBtn = "recharge" + data.id;
                        var deductionBtn = "deduction" + data.id;
                        data.op = "<button type=\"button\" class=\"btn  btn-sm   btn-primary\"" + " id=" + rechargeBtn + " >扣费</button>   <button type=\"button\" class=\"btn  btn-sm  btn-yellow\"" + " id=" + deductionBtn + " >充值</button>";
                        var recharge = function(){
                            console.log(this);
                        }
                        $('#vip-user-list').on('click', 'tbody #'+rechargeBtn, recharge.bind(data))
                        var deduction = function() {
                            console.log(this);
                        }
                        $('#vip-user-list').on('click', 'tbody #'+deductionBtn, deduction.bind(data))
                    }
                    return json.data;
                }
            },
            "columns": [
                {
                    "title": "姓名",
                    "data": "name"
                },
                {
                    "title": "卡号",
                    "data": "id"
                },
                {
                    "title": "手机号",
                    "data": "tel"
                },
                {
                    "title": "余额",
                    "data": "money"
                },
                {
                    "title": "更新日期",
                    "data": "updateTime"
                },
                {
                    "title": "操作",
                    "data": "op"
                }
            ]
        })
    })
</script>

方案二

使用columns的render属性,这个是我在翻源码的时候看到的:

/**
		 * This property is the rendering partner to `data` and it is suggested that
		 * when you want to manipulate data for display (including filtering,
		 * sorting etc) without altering the underlying data for the table, use this
		 * property. `render` can be considered to be the the read only companion to
		 * `data` which is read / write (then as such more complex). Like `data`
		 * this option can be given in a number of different ways to effect its
		 * behaviour:
		 *
		 * * `integer` - treated as an array index for the data source. This is the
		 *   default that DataTables uses (incrementally increased for each column).
		 * * `string` - read an object property from the data source. There are
		 *   three 'special' options that can be used in the string to alter how
		 *   DataTables reads the data from the source object:
		 *    * `.` - Dotted Javascript notation. Just as you use a `.` in
		 *      Javascript to read from nested objects, so to can the options
		 *      specified in `data`. For example: `browser.version` or
		 *      `browser.name`. If your object parameter name contains a period, use
		 *      `\\` to escape it - i.e. `first\\.name`.
		 *    * `[]` - Array notation. DataTables can automatically combine data
		 *      from and array source, joining the data with the characters provided
		 *      between the two brackets. For example: `name[, ]` would provide a
		 *      comma-space separated list from the source array. If no characters
		 *      are provided between the brackets, the original array source is
		 *      returned.
		 *    * `()` - Function notation. Adding `()` to the end of a parameter will
		 *      execute a function of the name given. For example: `browser()` for a
		 *      simple function on the data source, `browser.version()` for a
		 *      function in a nested property or even `browser().version` to get an
		 *      object property if the function called returns an object.
		 * * `object` - use different data for the different data types requested by
		 *   DataTables ('filter', 'display', 'type' or 'sort'). The property names
		 *   of the object is the data type the property refers to and the value can
		 *   defined using an integer, string or function using the same rules as
		 *   `render` normally does. Note that an `_` option _must_ be specified.
		 *   This is the default value to use if you haven't specified a value for
		 *   the data type requested by DataTables.
		 * * `function` - the function given will be executed whenever DataTables
		 *   needs to set or get the data for a cell in the column. The function
		 *   takes three parameters:
		 *    * Parameters:
		 *      * {array|object} The data source for the row (based on `data`)
		 *      * {string} The type call data requested - this will be 'filter',
		 *        'display', 'type' or 'sort'.
		 *      * {array|object} The full data source for the row (not based on
		 *        `data`)
		 *    * Return:
		 *      * The return value from the function is what will be used for the
		 *        data requested.
		 *
		 *  @type string|int|function|object|null
		 *  @default null Use the data source value.
		 *
		 *  @name DataTable.defaults.column.render
		 *  @dtopt Columns
		 *
		 *  @example
		 *    // Create a comma separated list from an array of objects
		 *    $(document).ready( function() {
		 *      $('#example').dataTable( {
		 *        "ajaxSource": "sources/deep.txt",
		 *        "columns": [
		 *          { "data": "engine" },
		 *          { "data": "browser" },
		 *          {
		 *            "data": "platform",
		 *            "render": "[, ].name"
		 *          }
		 *        ]
		 *      } );
		 *    } );
		 *
		 *  @example
		 *    // Execute a function to obtain data
		 *    $(document).ready( function() {
		 *      $('#example').dataTable( {
		 *        "columnDefs": [ {
		 *          "targets": [ 0 ],
		 *          "data": null, // Use the full data source object for the renderer's source
		 *          "render": "browserName()"
		 *        } ]
		 *      } );
		 *    } );
		 *
		 *  @example
		 *    // As an object, extracting different data for the different types
		 *    // This would be used with a data source such as:
		 *    //   { "phone": 5552368, "phone_filter": "5552368 555-2368", "phone_display": "555-2368" }
		 *    // Here the `phone` integer is used for sorting and type detection, while `phone_filter`
		 *    // (which has both forms) is used for filtering for if a user inputs either format, while
		 *    // the formatted phone number is the one that is shown in the table.
		 *    $(document).ready( function() {
		 *      $('#example').dataTable( {
		 *        "columnDefs": [ {
		 *          "targets": [ 0 ],
		 *          "data": null, // Use the full data source object for the renderer's source
		 *          "render": {
		 *            "_": "phone",
		 *            "filter": "phone_filter",
		 *            "display": "phone_display"
		 *          }
		 *        } ]
		 *      } );
		 *    } );
		 *
		 *  @example
		 *    // Use as a function to create a link from the data source
		 *    $(document).ready( function() {
		 *      $('#example').dataTable( {
		 *        "columnDefs": [ {
		 *          "targets": [ 0 ],
		 *          "data": "download_link",
		 *          "render": function ( data, type, full ) {
		 *            return '<a href="'+data+'">Download</a>';
		 *          }
		 *        } ]
		 *      } );
		 *    } );
		 */
		"mRender": null,

我的代码如下,也是字符串拼接标签,但是要注意这个方法会被调用多次,需要先取消绑定的事件,避免重复绑定:

<script>
    $(function () {
        $('#vip-user-list').DataTable({
            'paging': true,
            'lengthChange': false,
            'searching': true,
            'ordering': false,
            'info': true,
            'autoWidth': false,
            "ajax": {
                "url": './data/vip_user_list.json'
            },
            "columns": [
                {
                    "title": "姓名",
                    "data": "name"
                },
                {
                    "title": "卡号",
                    "data": "id"
                },
                {
                    "title": "手机号",
                    "data": "tel"
                },
                {
                    "title": "余额",
                    "data": "money"
                },
                {
                    "title": "更新日期",
                    "data": "updateTime"
                },
                {
                    "title": "操作",
                    "data": "op",
                    "render": function (data, type, full) {
                        var rechargeBtn = "recharge" + full.id;
                        var deductionBtn = "deduction" + full.id;
                        $('#vip-user-list ').undelegate('tbody #'+rechargeBtn, 'click');

                        $('#vip-user-list').on('click', 'tbody #'+rechargeBtn, function () {
                            console.log(full);
                        })
                        $('#vip-user-list ').undelegate('tbody #'+deductionBtn, 'click');
                        $('#vip-user-list').on('click', 'tbody #'+deductionBtn, function () {
                            console.log(full);

                        })

                        return "<button type=\"button\" class=\"btn  btn-sm   btn-primary\"" + " id=" + rechargeBtn + " >扣费</button>   <button type=\"button\" class=\"btn  btn-sm  btn-yellow\"" + " id=" + deductionBtn + " >充值</button>";

                    }
                }
            ]
        })
    })
</script>

点击这几个按钮,执行结果如下,输出这一行的值:

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,可以参考以下代码: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>扫描二维码</title> <!-- 引入Bootstrap的CSS文件 --> <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.min.css"> </head> <body> <div class="container"> <div class="row"> <div class="col-12"> <button id="scan-btn" class="btn btn-primary">扫描二维码</button> </div> </div> </div> <!-- 引入jQuery和Bootstrap的JS文件 --> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.6.0/js/bootstrap.min.js"></script> <!-- 引入Quagga库的JS文件 --> <script src="https://cdn.bootcdn.net/ajax/libs/quagga/0.12.1/quagga.min.js"></script> <script> $(document).ready(function() { // 绑扫描二维码的事件 $('#scan-btn').click(function() { // 设置了扫描的配置参数 let config = { inputStream: { type: "LiveStream", constraints: { width: {min: 640}, height: {min: 480}, facingMode: "environment" } }, locator: { patchSize: "medium", halfSample: true }, numOfWorkers: navigator.hardwareConcurrency ?? 4, decoder: { readers: ["code_128_reader", "ean_reader", "ean_8_reader", "code_39_reader", "code_39_vin_reader", "codabar_reader", "upc_reader", "upc_e_reader", "i2of5_reader"] }, locate: true }; // 开始初始化Quagga库 Quagga.init(config, function(err) { if (err) { console.log(err); return; } // 初始化成功后开始开始开始监听扫描事件 Quagga.start(); }); // 监听扫描到二维码的事件 Quagga.onDetected(function(result) { console.log(result.codeResult.code); // 扫描到二维码后停止扫描 Quagga.stop(); }); }); }); </script> </body> </html> ``` 这段代码的实现涉及到了一个叫做 `Quagga` 的开源库,它可以通过 Web 端程序扫描二维码。具体来说,在代码中: 1. 引入了 Bootstrap 的 CSS 文件,以及 jQuery 和 Bootstrap 的 JS 文件和 `Quagga` 库的 JS 文件; 2. 设置一个带有 ID 的按钮,按钮的 click 事件上扫描二维码的逻辑; 3. 在事件中初始化了 `Quagga` 库,设置了扫描的配置参数,并开始监听二维码扫描事件。 4. 扫描到二维码后,将二维码的值打印到控制台,并停止扫描。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不识君的荒漠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值