duanhao8540 2016-04-13 10:57
浏览 107
已采纳

DataTable上的“添加到购物车”ajax按钮

I am started to use DataTables because they provide what I need: ready to use pagination, ready to use how many items to show on the table 10, 25 .. etc. And here is the first problem. I want to put one button Add to cart at the end and with ajax to place items in basket which will downloaded later as zip. So far I was able to put the button but I'm very new in ajax and jquery stuff and can't figured out how to do the cart things. Here is what I have:

The table:

<table id="example" class="display table table-striped table-bordered responsive">
                    <thead>
                        <tr>
                            <th>№</th>
                            <th>Program</th>
                            <th>Title</th>
                            <th>Description</th>                                
                            <th>Add to Cart</th>
                        </tr>
                    </thead>

Here is how I put the custom button at the end

$(document).ready(function() {
  $('#example').DataTable( {
    "processing": true,
    "serverSide": true,
    "ajax": "response.php", 
    "aoColumnDefs": [
    {
        "aTargets": [5],
        "mData": null,
        "mRender": function (data, type, full) {
            return '<a href="#" id="total_items" onclick="(\''+ full[0] +'\');">Add to cart</a>';
        }            

    }]
 });
});

How I trying to put them in the cart

   $(document).ready(function(){

  $.ajax({
    type:'post',
    url:'store_items.php',
    data:{
      total_cart_items:"totalitems"
    },
    success:function(response) {
      document.getElementById("total_items").value=response;
    }
  });

});

function cart(id)
{
  var ele=document.getElementById(upload_id);
  var upload_title=document.getElementById("upload_title").value;
  var upload_description=document.getElementById("upload_description").value;

  $.ajax({
    type:'post',
    url:'store_items.php',
    data:{
      upload_title:upload_title,
      upload_description:upload_description
    },
    success:function(response) {
      document.getElementById("total_items").value=response;
    }
  });

}

function show_cart()
{
  $.ajax({
  type:'post',
  url:'store_items.php',
  data:{
    showcart:"cart"
  },
  success:function(response) {
    document.getElementById("mycart").innerHTML=response;
    $("#mycart").slideToggle();
  }
 });

}

and the php part

  session_start();

if(isset($_POST['total_cart_items']))
{
    echo count($_SESSION['upload_title']);
    exit();
}

if(isset($_POST['upload_title']))
{
    $_SESSION['upload_title'][]=$_POST['upload_title'];
    $_SESSION['upload_description'][]=$_POST['upload_description'];    
    echo count($_SESSION['upload_title']);
    exit();
}

if(isset($_POST['showcart']))
{
   for($i=0;$i<count($_SESSION['upload_title']);$i++)
   {
       echo "<div class='cart_items'>";      
       echo "<p>".$_SESSION['upload_title'][$i]."</p>";
       echo "<p>".$_SESSION['upload_description'][$i]."</p>";
       echo "</div>";
   }
   exit();  
}

So far the main problem that I see is how to pass the ID of the item which I store in cart. On the datatable ID of the item is on the first №. Then I'm not sure how to pass it to the cart with this ajax. If need I can show also the php part of response.php from where the table is populated.

  • 写回答

1条回答 默认 最新

  • doujia1988 2016-04-13 14:15
    关注

    For your MAJOR problem with getting ID you can make this:

    $(document).ready(function() {
    $('#example').DataTable( {
       "processing": true,
       "serverSide": true,
       "ajax": "response.php", 
       "aoColumnDefs": [
       {
          "aTargets": [5],
          "mData": null,
          "mRender": function(data, type, full) {
    return '<a class="btn btn-info btn-sm" href=#' + full[0] + '>' + 'Add to Cart' + '</a>';
          }
        }            
    
        }]
       });
     });
    

    The mRender Function takes three parameters:

    data = The data for this cell, as defined in mData

    type = The datatype (can be ignored mostly)

    full = The full data array for this row.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭