【问题标题】:JQuery DataTable add attribute to customize buttonJQuery DataTable 添加属性以自定义按钮
【发布时间】:2015-07-12 04:43:55
【问题描述】:

我正在尝试使用自定义按钮构建 JQuery DataTable。这是我的代码:

<table id="myDataTable" class="display" width="100%">
            <thead>
                <tr>      
                    <th> </th>                
                    <th>Company name</th>
                    <th>Address</th>
                    <th>Town</th>                             
                    <th></th>                                         
                </tr>
            </thead>
            <tbody>                
            </tbody>
        </table>
    </div>
</div>

<script>
    $(document).ready(function () {

        $('#myDataTable').dataTable({
            "bServerSide": true,
            "sAjaxSource": "Home/GetDataTable",
            "bProcessing": true,
            "aoColumns": [
            {"data": "ID"},
            { "data": "Name" },
            { "data": "Address" },
            { "mData": "Town" },           
            {
                 bSortable: false,
                 data: null,
                 className: "center",
                 defaultContent: '<button class="btn btn-danger data-id="" ">edit</button> <button class="btn btn-primary">delete</button>'
             }
            ],
            "columnDefs": [
           {
               "targets": [ 0 ],
               "visible": false,
               "searchable": false
           },          
            ]      
        });
    });
</script>

这是我在Action中的Controller

return Json(new
            {
                sEcho = param.sEcho,
                iTotalRecords = result.Count(),
                iTotalDisplayRecords = result.Count(),
                aaData = result
            },
        JsonRequestBehavior.AllowGet);
        }

这段代码运行良好,但现在我想将data-id 属性添加到我的按钮。我想将每行的 ID field (我隐藏)的值设置为 data-id 属性。我该如何实施?

【问题讨论】:

    标签: javascript jquery model-view-controller datatables


    【解决方案1】:

    你好,你能测试一下这段代码吗:

             {
                    bSortable: false,
                    data: null,
                    render: function(d) {
                        return "<button class="btn btn-danger data-id="'+ d.YourModelID +'" ">edit</button> <button class="btn btn-primary">delete</button>";
                    },
                    className: "center",
                }
    

    而不是这个:

             {
                 bSortable: false,
                 data: null,
                 className: "center",
                 defaultContent: '<button class="btn btn-danger data-id="" ">edit</button> <button class="btn btn-primary">delete</button>'
             }
    

    【讨论】:

      【解决方案2】:
      <script>
          $(document).ready(function () {
      
              $('#myDataTable').dataTable({
                  "bServerSide": true,
                  "sAjaxSource": "Home/GetDataTable",
                  "bProcessing": true,
                  "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
                      // if your button is in 5th column then index will be 4 because it starts from 1
                     $('td:eq(4)', nRow).find( 'button' ).attr('id',aData[0]); 
                     //assuming your id is in the 1st element of data
      
                  },
                  "aoColumns": [
                  {"data": "ID"},
                  { "data": "Name" },
                  { "data": "Address" },
                  { "mData": "Town" },           
                  {
                       bSortable: false,
                       data: null,
                       className: "center",
                       defaultContent: '<button class="btn btn-danger data-id="" ">edit</button> <button class="btn btn-primary">delete</button>'
                   }
                  ],
                  "columnDefs": [
                 {
                     "targets": [ 0 ],
                     "visible": false,
                     "searchable": false
                 },          
                  ]      
              });
          });
      </script>
      

      【讨论】:

      • 感谢您的帮助。所以,如果我想为每个按钮设置 'data-id' 我需要这样做: $('td:eq(4)', nRow).find( 'button' ).attr('data-id',数据[0]);我说的对吗?
      • 这个答案需要一些解释。仅代码的答案在这里通常不受欢迎。
      • 是的,它的作用是选择索引为 4 的 td,这是 tr 上下文中的第 5 个 td,并且在该 td 中找到按钮,然后为其添加属性。 :)
      猜你喜欢
      • 1970-01-01
      • 2017-11-01
      • 2013-01-09
      • 2020-12-12
      • 2017-02-08
      • 1970-01-01
      • 2018-12-24
      • 2020-12-14
      • 1970-01-01
      相关资源
      最近更新 更多