【问题标题】:Jquery Render Json Content with ajax on datatableJquery在数据表上使用ajax渲染Json内容
【发布时间】:2021-02-19 10:36:23
【问题描述】:

我想像这样呈现我的数据表的列:

My example

如您所见,datatable 说“表中没有可用数据”

我的代码是:

$(function() {
  $('#Spécialité').DataTable(
  {
    "ajax": {
      "processing": false,
      "url": "show_spe",
      "type": "GET",
      "datatype":'json',
      "async": 'true',
      success: function(resp) {
        let string = JSON.stringify(resp);
        let obj = JSON.parse(string);
        $.each( obj, function( key, value )
        {
        $('#Spécialité').append('<tbody><tr><td>'+obj[key]["id"]+'</td> <td>'+obj[key]["description"]+'</td><td><button type="button" class="btn btn-warning" id="edit'+obj[key]["id"]+'">Editer</button>&nbsp;&nbsp;<button type="button" class="btn btn-danger" id="edit'+obj[key]["id"]+'">Supprimer</button></td></tr></tbody>');
        });
      },

  },
  } );

我的 json 是:

0 {
id : "0"
description : "test" }
1 {
id  : "1" 
description : "ligne2" }

感谢您的帮助。问候:)

【问题讨论】:

    标签: javascript json ajax datatable


    【解决方案1】:

    我解决了这个问题。

    $(function() {
    
    var table = $('#Spécialité').DataTable({
      "ajax": {
        "url": "url",
        "method": "GET",
        "dataSrc": ""
        },
        "columns": [
          { "data": "id"},
          { "data": "description"},
            {
              data: 'id',
              render: function(data)
              {
               return '<button type="button" class="btn btn-warning" onclick="modalEdit('+data+')" name="edit" id="edit'+data+'">Editer</button>&nbsp;&nbsp;<button class="btn btn-danger" type="button" onclick="modalDelete('+data+')" name="delete" id="delete'+data+'">Supprimer</button> ';
              }
            }
          ],
        "order": [[1, "asc"]]
    });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-04
      • 2014-05-31
      • 2018-01-23
      • 1970-01-01
      • 1970-01-01
      • 2011-10-17
      • 1970-01-01
      • 2016-12-19
      相关资源
      最近更新 更多