在 bootstrap table 中插入输入类型列

insert input type column in bootstrap table

我在 bootstrap table 中遇到问题,我有一个 bootstrap table 由数据库中的数据填充,使用 JQuery AJAX ,我想要做的是在每条记录的最后一列 insert/add <input type="text"/>

我的Jquery脚本:

<script type="text/javascript">
    $(function(){

        var baseurl = "<?php print site_url('/quotes/get'); ?>";                  

            $.ajax({
                method: "POST",
                url: baseurl,
                data: { analid: 1 },
                dataType: 'json'
            })
                .done(function( msg ) {

                console.log(msg);

                $('#qtable').bootstrapTable({
                    data: msg
                });

            });

    });
</script>

上面的脚本正在显示数据库中的数据,然后我找到了这个参考资料(LINK HERE),在那个网站的底部我看到了一些 bootstraptable 的方法,比如添加静态列。

更新代码 Jquery 脚本:

 $(function() {
    var baseurl = "<?php print site_url('index.php/quotes/get'); ?>";
    $.ajax({
            method: "POST",
            url: baseurl,
            data: {
                analid: 1
            },
            dataType: 'json'
        })
        .done(function(msg) {

            console.log(msg);

            $('#qtable').bootstrapTable({
                data: msg,
                columns: [{ //<--- here is where I lost.. I don't know what to do here or what should I add..
                    field: 'operate',
                    title: 'Item Operate',
                    align: 'center',
                    valign: 'middle',
                    clickToSelect: false,
                    formatter: operateFormatter,
                    events: operateEvents
                }]

            });

        });
});

非常感谢任何替代和优化的解决方案。

谢谢!

我不熟悉这个库,但我查看了文档,这就是我认为你需要的。 formatter 可以是这样的匿名函数:

formatter : function(value) {
    //value contains the current value in the cell.  whatever you return will be the new value of the cell.
       return '<input name="myinput" />';
}

您可以使用列选项 formatter。参见示例 HERE

formatter : function(value,row,index) {
   return '<input name="elementname"  value="'+value+'"/>';
   //return '<input name="elementname"  value="'+row.id+'"/>';     here id is your field name
 }     

单元格格式化函数,带三个参数:

  1. value:字段值。
  2. row:行记录数据。
  3. index: 行索引。

在这种情况下,您的代码将如下所示。(假设 'operate' 是您的最后一列名称)

$('#qtable').bootstrapTable({
            data: msg,
            columns: [{ //<--- here is where I lost.. I don't know what to do here or what should I add..
                field: 'operate',
                title: 'Item Operate',
                align: 'center',
                valign: 'middle',
                clickToSelect: false,
                formatter : function(value,row,index) {
                   return '<input name="elementname"  value="'+value+'"/>';
                   //return '<input name="elementname"  value="'+row.id+'"/>';     here id is your field name 
                }  
            }]

        });

如果你想return/在已知位置呈现列,只需使用以下功能块

formatter : function(value,row,index) {
   return '<input name="elementname"  value="'+value+'"/>';
 }   

如果您想排除/跳过某些列或想要呈现特定列(添加空花括号对 {}、{}、{},根据您需要移动的列数,像这样) 例如,

columns: [ {},{},{},  

           {
             field: 'operate',
             title: 'Item Operate',
             align: 'center',
             valign: 'middle',
             clickToSelect: false,
             formatter : function(value,row,index) {
               return '<input name="elementname"  value="'+value+'"/>';
             }
           }
         ]