如何在我想插入项目时修复负值?

时间:2017-07-03 00:34:45

标签: php datatables codeigniter-3

我正在使用datatables和codeigniter,但是当我从datatables的加号按钮插入项目时,插件仍然在计数,当datatables列等于零时,则计算负数,但是我支持的不是允许负数,并返回无符号错误。问题是给我负数是这一行

columnas[2].innerHTML = (columnas[2].innerHTML - 1); 

我该如何解决?

控制器

public function add_cart(){
        $this->session->set_userdata("carrito", $this->sale->checar_existe_carrito());
        $response = array(
            "status" => 0
        );
        $producto_actual = array(
            'id' => $this->input->post("id"),
            'name' => $this->input->post("name"),
            'qty' => $this->input->post("qty"),
            'price' => $this->input->post("price"),
        );
        $tipo_operacion = $this->input->post("tipo_operacion");
        $producto = $this->products->get_product(md5($producto_actual['id']));
        $valor_compara = $tipo_operacion == 1 ? $producto->stock - $producto_actual['qty'] : $producto->stock - ($producto_actual['qty'] - $this->sale->get_current_cart_qty($this->session->carrito, $producto_actual['id']));
        if ($producto->stock == 0) {
               $response['error'] = "There is not enough stock of this product to add to cart";
               $this->json(array('error' => $response['error']));
        }else{
            echo 'bien';
        }
    }

AJAX

$('#btn-search').click(function (e) {
    BootstrapDialog.show({
        title: 'Search Product',
        message: function (dialog) {
            var $message = $('<div></div>');
            var pageToLoad = dialog.getData('pageToLoad');
            $message.load(pageToLoad);
            return $message;
        },
        data: {
            pageToLoad: URL_GET_VIEW_SEARCH_PRODUCT
        },
        onshown: function (dialog) {
            $('#example').DataTable({
                lengthChange: false,
                responsive: true,
                ajax: {
                    url: URL_GET_DATATABLE,
                    type: 'POST',
                },
                columnDefs: [{
                    targets: 4,
                    data: null,
                    defaultContent: "<a href='#' class='add_product'><span class='glyphicon glyphicon-plus'></span></a>"
                }]
            });
            $('.dataTable').on('click', 'tbody tr td a.add_product', function () {
                var columnas = $(this).parent().parent().children();
                var id = columnas[0].innerHTML;
                var name = columnas[1].innerHTML;
                var qty = 1;
                var price = columnas[3].innerHTML;
                add_cart(id, name, qty, price, 1);
                columnas[2].innerHTML = (columnas[2].innerHTML - 1);
            });

        }
    });
});

stock_column_negative stock_column_negative

1 个答案:

答案 0 :(得分:0)

由于您只需要将innerHTML值更改为负值的行为,只需将值设置为新值,如果值小于零则为零。

var myNewValue = (columnas[2].innerHTML - 1) < 0 ? 0 ; (columnas[2].innerHTML - 1);

如果结果值小于零,则将其设为零,如果不是,则使用结果。

function myFunction() {
  var printResult = document.getElementById('result');
  var getNumber = document.getElementById('number');
  printResult.innerHTML = (getNumber.value - 1) < 0 ? 0 : (getNumber.value - 1)
};
<input type="number" id="number">
<button id="button" onclick="myFunction()">push</button>
<div id="result"></div>