javascript - MySQL数据显示不同价格JavaScript

标签 javascript mysql codeigniter

我有以下与简单销售管理系统相关的表格

商店商品

+---------+-----------+-----------------------+--------------+
| item_id | item_code |       item_name       | retail_price |
+---------+-----------+-----------------------+--------------+
|       1 |  60002332 | Coolant 1L            |          250 |
|       2 |  60002333 | Coolant 4L            |          950 |
|       3 |  60002334 | GEARON X (SAE90)-20L  |         2500 |
|       4 |  60002335 | GEARON X (SAE90)-210L |        30000 |
+---------+-----------+-----------------------+--------------+

store_update_stock

+-----------------+------------+------------+--------------+
| update_stock_id | invoice_no |    date    | order_status |
+-----------------+------------+------------+--------------+
|               1 | INV001     | 2019-10-01 | purchase     |
|               2 | INV002     | 2019-10-02 | purchase     |
|               3 | INV003     | 2019-10-03 | purchase     |
+-----------------+------------+------------+--------------+

store_update_stock_details

+

-------------------------+-----------------+------+-----+------------+---------------+
| update_stock_details_id | update_stock_id | item | qty | unit_price | available_qty |
+-------------------------+-----------------+------+-----+------------+---------------+
|                       1 |               1 |    1 |   2 |        250 |             2 |
|                       2 |               1 |    1 |  10 |        260 |            10 |
|                       3 |               2 |    2 |   4 |        950 |             4 |
|                       4 |               3 |    4 |   5 |      30000 |             5 |
+-------------------------+-----------------+------+-----+------------+---------------+

我尝试使用商品名称、数量和单价进行销售

我的名为 Issue.php 的 Controller 也包含以下行

$this->data['products'] = $this->Item_Model->getExistProducts();

Item_Model中相关函数如下

public function isExistProduct($q)
    {
        if (!empty($q)) {

            $this->db->select("store_item.*,store_update_stock.*,sum(qty) as qty, store_update_stock_details.unit_price");
            $this->db->from('store_update_stock_details');
            $this->db->join('store_update_stock', 'store_update_stock_details.update_stock_id=store_update_stock.update_stock_id');
            $this->db->join('store_item', 'store_update_stock_details.item=store_item.item_id', 'right');
            $this->db->where("store_item.item_id= $q");
            $this->db->group_by('store_item.item_id, store_update_stock_details.unit_price');
            $q1 = $this->db->get();


            if ($q1->num_rows() > 0) {
                return $q1->result_array();
            }

            return 0;
        }
    }

名为 addIssue.php 的 View 包含以下 JavaScript 代码和 HTML 代码

<script type="text/javascript">


    $(document).on("change", "#item", function () {
        //  console.log($('#item').val());
        $("#salesList").show();
        $.ajax({
            'url': '<?=site_url("issue/isExistProduct/?q=")?>' + $('#item').val(),
            'method': 'GET',
            'success': function (data) {

                var jData = JSON.parse(data);
                if (jData.status == true) {
                    //console.log(jData);
                    $('#request_table').append('<tr>' +
                        '<td ><span id="product" >' + jData.data[0].item_name + '</span>' +
                        '<input type="hidden" id="item_id[]" name="item_id[]" value="' + jData.data[0].item_id + '">' +
                        '</td>' +
                        '<td class="text-center">' + jData.data[0].qty + '</td>' +
                        '<td class="text-center"><input class="form-control text-center rquantity" data-qty-bal="' + jData.data[0].qty + '" autofocus required type="number" step="any" id="qty[]" name="qty[]" ></td>' +
                        '<td class="text-center"><input class="form-control text-right" autofocus required type="number" step="any" id="sales_price[]" name="sales_price[]" value="' + jData.data[0].unit_price + '"></td>' +
                        '<td class="text-center"><input class="form-control text-right" autofocus type="number" step="any" id="discount_price[]" name="discount_price[]" ></td>' +
                        '<td class="text-center" ><i class="fa fa-remove remove" style="cursor: pointer"></i></td>' +
                        '</tr>');
                }

            },
            'error': function () {

            }
        });


    });

    $(document).on("click", ".remove", function () {
        $(this).closest('tr').remove();
    });

    var old_row_qty;
    $(document).on("focus", '.rquantity', function () {
        old_row_qty = $(this).val();
    }).on("change", '.rquantity', function () {
        var row = $(this).closest('tr');
        if ($(this).val() > $(this).data('qty-bal')) {
            $(row).addClass('danger');
            $('#add_sale').attr('disabled', true);
        } else {
            $(row).removeClass('danger');
            $('#add_sale').attr('disabled', false);
        }
        var amount = 0, subTotal = 0;
        $('#request_table >tbody').find('tr').each(function () {
            var rate = parseFloat($(this).find('td:eq(2) > .rquantity').val());
//            $(this).find('.ssubtotal').text(formatNumber(rate));
            amount += rate;
//            subTotal += getNumber($(this).find('td:eq(5)').text());
        });
        $('#request_table  >tfoot>tr').find('th:eq(1)').text(amount);
//        $('#payTable  >tfoot>tr').find('th:eq(2)').text(formatNumber(subTotal));
    });

</script>
<script type="text/javascript">
    $(document).ready(function () {
        $("#salesList").hide();
        var oTable = $('#ExData').dataTable({

            "aaSorting": [[0, "asc"]],
            "aLengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "ALL"]],
            "iDisplayLength": 10,
            'bProcessing': true, 'bServerSide': true,
            'sAjaxSource': '<?= base_url() . "issue/getIssue" ?>',
            'fnServerData': function (sSource, aoData, fnCallback) {
                aoData.push({
                    "name": "type",
                    "value": "1 "
                });
                $.ajax({'dataType': 'json', 'type': 'POST', 'url': sSource, 'data': aoData, 'success': fnCallback});
            },
            'fnRowCallback': function (nRow, aData, iDisplayIndex) {

            },
            "aoColumns": [{'sName': 'invoice_no'}, {'sName': 'sales_date'}, {'sName': 'rep'}, {'sName': 'dealer_code'}, {'sName': 'dealer_name'}, {'mRender':formatMoney}, {'mRender':formatMoney}, {'mRender': pay_status}, null],
            "fnFooterCallback": function (nRow, aaData, iStart, iEnd, aiDisplay) {

            }

        });


    });


</script>
<div class="box box-info">
    <div class="box-body" style="display: block">
        <form action="<?= site_url('issue/addIssue') ?>" method="post">
            <div class="row">
                <div class="col-md-3">
                    <div class="form-group"><label>Sales Date</label>
                        <div class="input-group">
                                <span class="input-group-addon">
                                    <i class="glyphicon glyphicon-calendar"></i>
                                </span>
                            <input type="text" name="sales_date" id="sales_date" class="form-control datepicker"
                                   style="cursor: pointer"
                                   value="<?= date('Y-m-d') ?>" readonly>
                        </div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="form-group"><label>Dealer</label>
                        <select name="dealer" id="dealer" class="form-control select2" required>
                            <option value="">Select Dealer</option>
                            <?php
                            if (!empty($dealer)) {
                                foreach ($dealer as $row) {

                                    ?>

                                    <option value="<?= $row->dealer_id ?>"><?= $row->dealer_code . " - " . $row->dealer_name ?></option>
                                    <?php
                                }
                            }

                            ?>
                        </select>

                    </div>
                </div>
                <div class="col-md-3">
                    <div class="form-group"><label>Rep Code</label>
                        <select name="rep" id="rep" class="form-control select2" required>
                            <option value="">Select Rep</option>
                            <?php
                            if (!empty($rep)) {
                                foreach ($rep as $row) {

                                    ?>

                                    <option value="<?= $row->rep_id ?>"><?= $row->rep_name ?></option>
                                    <?php
                                }
                            }

                            ?>
                        </select>

                    </div>
                </div>

                <div class="col-md-3">
                    <div class="form-group"><label>Invoice No</label>
                        <input type="text" name="invoice_no" id="invoice_no" class="form-control"
                               value="<?= $autoInv ?>">
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-6">
                    <div class="form-group"><label>Select Item</label>
                        <select name="item" id="item" class="form-control select2" required>
                            <option value="">Select Item</option>
                            <?php
                            if (!empty($products)) {
                                foreach ($products as $row) {

                                    ?>

                                    <option value="<?= $row->item_id ?>"><?= $row->item_name ?></option>
                                    <?php
                                }
                            }

                            ?>
                        </select>

                    </div>
                </div>

            </div>


            <!--table-->
            <div id="salesList">
                <div class="col-md-12 column">
                    <div class="col-md-12">
                        <div class="control-group table-group">
                            <label class="table-label">Sale Items *</label>

                            <div class="controls table-controls">
                                <table id="request_table" width="100%"
                                       class="table items table-striped table-bordered table-condensed table-hover">
                                    <thead>
                                    <tr style="background-color: #3c8dbc;color: #ffffff;">
                                        <th class="col-md-3">Item Name</th>
                                        <th class="text-center col-md-2">Available Qty</th>
                                        <th class="text-center col-md-2">Issue Qty</th>
                                        <th class="text-center col-md-2">Unit Price</th>
                                        <th class="text-center col-md-2">Discount</th>

                                        </th>
                                        <th class="col-md-1" style="width: 30px !important; text-align: center;">
                                            <i class="fa fa-trash-o"
                                               style="opacity:0.5; filter:alpha(opacity=50);"></i>
                                        </th>
                                    </tr>
                                    </thead>
                                    <tbody></tbody>
                                    <tfoot>
                                    <tr id="tfoot" class="tfoot active">
                                        <th colspan="2">Total</th>
                                        <th class="text-center">0</th>
                                        <th class="text-right"></th>
                                        <th class="text-right"></th>
                                        <th class="text-center"><i class="fa fa-trash-o"
                                                                   style="opacity:0.5; filter:alpha(opacity=50);"></i>
                                        </th>
                                    </tr>
                                    </tfoot>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-8"></div>
                    <div class="col-md-4">
                        <button type="submit" id="add_sale" class="btn btn-primary btn-block"><i
                                    class="fa fa-shopping-cart" aria-hidden="true"></i> Add Sale
                        </button>
                    </div>
                </div>
            </div>


        </form>

        <!--end of table-->
    </div>

Sales View

一切都工作正常。

问题

表 store_update_stock_details 有两条 item_id -> 1 的记录,具有不同的 unit_price (250, 260)。

期望的输出

我想在执行销售和更新可用数量时将这两个带有商品名称和数量的单价显示为列表中的两行。商品和可用数量正确显示,但单价未显示在 View 中。

出了什么问题。谁能帮我吗?

最佳答案

$('#request_table').append('<tr>' +
  '<td ><span id="product" >' + jData.data[0].item_name + '</span>' +
  '<input type="hidden" id="item_id[]" name="item_id[]" value="' + jData.data[0].item_id 
  + '">' +
  '</td>' +
  '<td class="text-center">' + jData.data[0].qty + '</td>' +
  '<td class="text-center"><input class="form-control text-center rquantity" data-qty-bal="' + jData.data[0].qty + '" autofocus required type="number" step="any" id="qty[]" name="qty[]" ></td>' +
  '<td class="text-center"><input class="form-control text-right" autofocus required type="number" step="any" id="sales_price[]" name="sales_price[]" value="' + jData.data[0].unit_price + '"></td>' +
  '<td class="text-center"><input class="form-control text-right" autofocus type="number" step="any" id="discount_price[]" name="discount_price[]" ></td>' +
  '<td class="text-center" ><i class="fa fa-remove remove" style="cursor: pointer"></i> 
   </td>' +
'</tr>');

因为上面的函数只追加第一条记录到表中,所以需要使用foreach语句来追加整条记录

关于javascript - MySQL数据显示不同价格JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58567604/

相关文章:

php - 如何从 PHP mySQL 查询中回显图像数组?

Git 和 Github 工作流程,使用父级更改更新子树

javascript - 如何在谷歌地图上显示圆圈?

javascript - 从 jquery ui 按钮中删除悬停和默认状态

mysql - 我想将列表值存储为 MYSQL 列中的数组。最佳做法是什么?

MySQL SUM 函数和 CASE 语句

Node.js + 代码点火器

php - Mysql 不返回特定数字的选择查询

javascript - 如何以编程方式将 Bootstrap 下拉菜单更改为特定状态?

事件处理程序中的 JavaScript 同步和关键部分