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 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛