首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不使用给定响应初始化jquery

不使用给定响应初始化jquery
EN

Stack Overflow用户
提问于 2018-05-09 18:44:29
回答 1查看 25关注 0票数 0

我想用生成的响应初始化数据表。

我的反应如下

代码语言:javascript
复制
{data: Array(3), status: true}
data :  Array(3)
0  :    {countryId: 1, countryName: "sampleCountry", countryShortCode: "sampleCode", status: "yes"}
1  :    {countryId: 2, countryName: "pakistan", countryShortCode: "pak", status: "yes"}
2  :    {countryId: 3, countryName: "sample2", countryShortCode: "pak", status: "yes"}

请看我的html

代码语言:javascript
复制
<table class="table table-striped" id="countryTable">

                                    <thead>
                                      <tr>
                                        <th>S.NO.</th>
                                        <th>Country Name</th>
                                        <th>Country Short Name</th>


                                      </tr>
                                    </thead>
                                    <tbody>
                                   </tbody>
</table>

请看我的数据表初始化

代码语言:javascript
复制
$.ajax({        
            url : url,

            type:"get",     
            contentType:'application/json; charset=utf-8',  
            dataType: 'json' ,
            async: false,  
            success:function(response) 
            {     
                alert(response.data);  

                  $('#countryTable').DataTable( {
                        "fnRowCallback" : function(nRow, aData, iDisplayIndex){
                            $("td:first", nRow).html(iDisplayIndex +1);
                           return nRow;
                        },
                         destroy: true,
                        mydata: response.data, 
                        columns: [     
                             { mydata:'countryId'},   
                             { mydata:'countryName'},
                             { mydata:'countryShortCode'}     


                        ]  
                    } );         

                console.log(response);
            }
        });

初始化后,数据表显示为,表中没有可用的数据,但表是用datatable插件初始化的。数据不会出现在表中。我的代码出了什么问题请帮帮我。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-10 00:08:40

代码看起来很好,只需将mydata更改为data,如下所示:

代码语言:javascript
复制
var response = {
  data: [{
      countryId: 1,
      countryName: "sampleCountry",
      countryShortCode: "sampleCode",
      status: "yes"
    },
    {
      countryId: 2,
      countryName: "pakistan",
      countryShortCode: "pak",
      status: "yes"
    },
    {
      countryId: 3,
      countryName: "sample2",
      countryShortCode: "pak",
      status: "yes"
    }
  ],
  status: true
}

$('#countryTable').DataTable({
  "fnRowCallback": function(nRow, aData, iDisplayIndex) {
    $("td:first", nRow).html(iDisplayIndex + 1);
    return nRow;
  },
  destroy: true,
  data: response.data,
  columns: [{
      data: 'countryId'
    },
    {
      data: 'countryName'
    },
    {
      data: 'countryShortCode'
    }


  ]
});
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<link href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css" rel="stylesheet" />
<table class="table table-striped" id="countryTable">

  <thead>
    <tr>
      <th>S.NO.</th>
      <th>Country Name</th>
      <th>Country Short Name</th>
    </tr>
  </thead>
  <tbody>
  </tbody>
</table>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50260141

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档