如果來源是ajax, 請問怎样加property和class到data table?

請問怎样加property data-idtr和class到td內?

如果資料來源是在html里面, 我懂得直接加在html里面. 但不知道當資料來源是由ajax的處理方法.

資料在html里面的處理

            <table id="demo" class="table table-condensed table-striped">
              <thead>
                <tr>
                  <th class="cell-customer">新會員</th>
                  <th class="cell-qty">數量</th>
                </tr>
              </thead>
              <tbody>
                <tr data-id="1">
                  <td>1</td>
                  <td class="cell-customer">John</td>
                  <td class="cell-qty">1</td>
                </tr>
              </tobdy>
            </table>
            <script>
              $('#demo').dataTable();
            </script>

用ajax的處理方法:

            <table id="demo" class="table table-condensed table-striped">
              <thead>
                <tr>
                  <th class="cell-customer">新會員</th>
                  <th class="cell-qty">數量</th>
                </tr>
              </thead>
            </table>
            <script>
                $('#demo').dataTable({
                  ajax: "api/order.php",
                  columns: [
                    {data: 'customer'},
                    {data: 'qty'},
                  ],
                  createdRow: function(row,data,index){
                  $(row).attr('data-id',data.id); //這里不明為何$row.prop()會無效
                  }                
                });     
            </script>
阅读 2.4k
1 个回答

我能想到的方法就是根据ajax返回的数据拼接html字符串。另外,面向jQ编程可不是长久之计

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏