【问题标题】:How to show an img in datatables while i using ajax使用ajax时如何在数据表中显示图像
【发布时间】:2017-10-19 19:52:47
【问题描述】:

我在 laravel 5.5 中工作,我实现了数据表并将它与 ajax 一起使用,我工作正常,但现在我需要在数据表中显示和 img,但不知道如何。我阅读了渲染文档,但我仍然迷路了。 表格代码:

<div class="row-fluid margin-body">
                <table id="productos" class="table table-hover table-condensed" >
                        <thead>
                                <tr>
                                        <th >Id</th>
                                        <th>Producto</th>
                                        <th>Cantidad </th>
                                        <th>img</th>
                                        <th></th>
                                </tr>
                        </thead>
                </table>
        </div>

Ajax 在表格中创建内容:

$(document).ready(function(){
                listar();
        });
        var listar = function (){
                var table = $('#productos').DataTable({
                        "processing": true,
                        "serverSide": true,
                        "ajax": "ajaxProducto",
                        "columns":[
                                {data:'id'},
                                {data:'nombre'},
                                {data:'stock'},
                                {data: 'imagen',
                                  "render": function(data, type, row) {
                                    return '<img src="'+data+'" />';
                                }},
                                {defaultContent:
                                        "<a type='button' class='editar btn btn-xs btn-mini btn-primary' data-toggle='modal' data-target='#myModalEditar'>Editar</a><a type='button'  class='eliminar btn btn-xs btn-mini btn-danger' >Eliminar</a><a type='button' class='btn btn-xs btn-mini btn-success detalles' data-toggle='modal' data-target='#myModalDetalles'>Detalles</a>"
                                }
                        ],
                        "language": idioma_esp

                });


}

这是我想展示我的img的地方

    {data: 'imagen',
           "render": function(data, type, row) {
  return '<img src="img/productos/'+data+'" height="42" width="42"/>';    }}

我的路线:

Route::get('/ajaxProducto', function() {

    return datatables()->collection(App\Producto::all())->toJson();
});

【问题讨论】:

    标签: php ajax datatable


    【解决方案1】:

    我在写问题时发现了我的错误。这段代码应该适合你。

    【讨论】:

    • 如果您发现错误并且问题仍然有价值,您应该在此处的答案部分中发布解决方案的说明。如果问题不再有价值,则应将其删除。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-08
    • 1970-01-01
    • 1970-01-01
    • 2019-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多