简体   繁体   English

我如何在jQuery中创建可编辑的表列

[英]How can i create an editable table column in jquery

<script type="text/javascript" language="javascript">
$(document).ready(function () {
    var table = "<table id='tblItemList'>" +
                   "<tr>" +
                       "<th style='width:20px;'>&nbsp;</th>" +
                       "<th style='width:100px;'><b>ItemId</b></th>" +
                       "<th style='width:100px;'><b>Item</b></th>" +
                       "<th style='width:100px;'><b>Quantity</b></th>" +
                   "</tr>";

    $("#ddlItemID").dropdownchecklist({forceMultiple: true , onComplete: function (selector) {
    var values = "";
    for (i = 0; i < selector.options.length; i++) {
        if (selector.options[i].selected && (selector.options[i].value != "")) {
            if (values != "") values += ";";
            values += selector.options[i].value;

            var chkId = 'chkId' + i;
            var itemId = 'itemId' + i;
            var ItemName = 'ItemName' + i;
            var qty = 'qty' + i;

            var itemData = "<tr>" +
                           "<td><input type='checkbox' id='" + chkId + "' /></td>" +
                           "<td id='" + itemId + "' class='selectedItemId'>" + selector.options[i].value + "</td>" +
                           "<td id='" + ItemName + "' class='selectedItemName'>" + selector.options[i].text + "</td>" +
                           "<td id='" + qty + "' class='selectedItemQty'>" + 0 + "</td>" +
                       "</tr>";
            table += itemData;
        }
    }

    table += "</table>";
    $("#trItemsList").show();
    $("#trItemsList").html(table);
}
, onItemClick: function (checkbox, selector) {
    var justChecked = checkbox.prop("checked");
    var checkCount = (justChecked) ? 1 : -1;
    for (i = 0; i < selector.options.length; i++) {
        if (selector.options[i].selected) checkCount += 1;
    }
    if (checkCount > 3) {
        alert("Limit is 3");
        throw "Too many items were selected";
    }
}
    });
});
</script>

I have the above jQuery code that creates an a table listing items selected from a dropdownchecklist that supports multiple select. 我上面的jQuery代码创建了一个表格,该表格列出了从支持多种选择的dropdownchecklist中选择的项目。

After a user has selected items in the dropdownchecklist, on onComplete, a "mini" table is created just below the dropdownchecklist with the users selected items. 用户在下拉列表中选择项目之后,在onComplete上,下拉列表的正下方会创建一个“迷你”表,其中包含用户选择的项目。

Currently, the created table is read-only but what i want to achieve is the user to proceed and update the created tables last column (Quantity) with a numeric value for each item (row) in the table. 当前,创建的表是只读的,但是我要实现的是用户继续进行操作,并使用表中每个项目(行)的数值更新创建的表的最后一列(数量)。

For example, if i selected Item_one, Item_five & Item_twenty from the dropdownchecklist, a table of 3 rows will be created. 例如,如果我从下拉列表中选择Item_one,Item_five和Item_twenty,则将创建3行的表。 In that table i will have values for Item_ID and Item_Name. 在该表中,我将具有Item_ID和Item_Name的值。 Now i want to be able to edit that table and input Item_Quantity against each row, after which i will finally save to the database. 现在,我希望能够编辑该表并针对每一行输入Item_Quantity,之后,我最终将其保存到数据库中。

Does any one how i can modify the above code such that the table created has an editable third column (Quantity), that users can update? 有没有人可以修改上面的代码,使创建的表具有可编辑的第三列(数量),用户可以更新?

Below is the html for the dropdownlist 以下是下拉列表的html

<td>
@Html.DropDownList("ddlItemID", ViewBag.GatePassItemsList as SelectList, new { @multiple = "multiple" })
</td>

Ok there you go!! 好吧,你去!

Jus replace your last td in itemData with below Jus用下面的内容替换了itemData中的最后一个td

'<td id="' + qty + '" class="selectedItemQty"><input type="text" placeholder="Enter quantity" id="txt_'+qty+'"></td>'

That's it. 而已。

Let me know if you face any issues!! 让我知道您是否遇到任何问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM