【发布时间】:2019-01-25 04:06:58
【问题描述】:
我有一个 html 表有 3 个字段,其中一个是可编辑的
- 我得到的两列数据为
json这两个字段是itemName和itemCode,第三列是我自己创建的Quantity,我将其赋值为0 - 我也有一个下拉列表,它是
category,其中有几个下拉列表第一个是All,最初它显示所有数据 - 之后,如果用户点击
juice类别,则只会填充果汁项目 - 然后用户在那里输入一些数量并且没有保存就转到另一个类别比如
rice并在那里输入一些数量 - 在此之后,当用户返回
juice类别时,他输入的数量全部消失,并显示为0,这是我不想要的
我想显示用户输入的值,即使他/她进入任何其他类别并返回
- 简单地说,我想保留用户输入后的值。
- 我正在进行 ajax 调用以根据
Category选择填充表内的数据
$(document).ready(function() {
$.ajax({ //this ajax is populating for all categories
url: "CategoryOlWiseFilter",
method: "GET",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(tableData) {
addTable(tableData);
}
});
$('#CategoryName').on('change', function() {
var selectedOption = this.value;
$.ajax({ //this one will populate which category is selected
async: true,
url: "ItemCategoryWiseFilter",
method: "POST",
data: {
categoryName: selectedOption,
},
});
$.ajax({
async: true,
url: "ItemCategoryWiseFilter",
method: "GET",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(tableData) {
addTable(tableData);
}
});
});
});
function addTable(tableData) {
var col = Object.keys(tableData[0]);
var countNum = col.filter(i => !isNaN(i)).length;
var num = col.splice(0, countNum);
col = col.concat(num);
var table = document.createElement("table");
var tr = table.insertRow(-1); // TABLE ROW.
var colNum = col.length; //to improve the speed
for (var i = 0; i < colNum + 1; i++) {
var th = document.createElement("th"); // TABLE HEADER.
if (i >= colNum) {
th.innerHTML = "Quantity";
tr.appendChild(th);
tr.classList.add("text-center");
tr.classList.add("head")
} else {
th.innerHTML = col[i];
tr.appendChild(th);
tr.classList.add("text-center");
tr.classList.add("head")
}
}
for (var i = 0; i < tableData.length; i++) {
tr = table.insertRow(-1);
for (var j = 0; j < col.length + 1; j++) {
let tabCell = tr.insertCell(-1);
var hiddenField = document.createElement("input");
hiddenField.style.display = "none";
var tabledata = tableData[i][col[j]];
if (i > -1 && j >= colNum) {
var quantityField = document.createElement("input");
quantityField.style.border = "none";
quantityField.style["text-align"] = "center";
quantityField.setAttribute('name', 'Quantity');
quantityField.setAttribute('autocomplete', 'on');
quantityField.setAttribute('value', '0');
quantityField.setAttribute('type', 'number');
quantityField.setAttribute('required', 'required');
quantityField.classList.add("dataReset");
tabCell.appendChild(quantityField);
} else {
if (tableData[i]['Item Code'] === tableData[i][col[j]]) {
tabCell.innerHTML = tabledata;
hiddenField.setAttribute('name', 'Item_Code');
hiddenField.setAttribute('value', tabledata);
tabCell.appendChild(hiddenField);
}
if (tableData[i]['Item Name'] === tableData[i][col[j]]) {
tabCell.innerHTML = tabledata;
hiddenField.setAttribute('name', 'Item_Name');
hiddenField.setAttribute('value', tabledata);
tabCell.appendChild(hiddenField);
}
if (j > 1)
tabCell.classList.add("text-right");
}
}
}
var divContainer = document.getElementById("HourlysalesSummary");
divContainer.innerHTML = "";
divContainer.appendChild(table);
table.classList.add("table");
table.classList.add("table-striped");
table.classList.add("table-bordered");
table.classList.add("table-hover");
}
<div class="container">
<form action="www.google.com" id="form1">
<div class="row position-relative">
<div class="col-lg-4">
<h5 id="commonHeader">Category</h5>
<select class="test" id="CategoryName" name="categoryCode">
<option>All</option>
<option>juce</option>
<option>rice</option>
<option>roti</option>
</select>
</div>
</div>
<hr style="border: 1px solid black">
<div class="table-responsive">
<table class="w-100" id=HourlysalesSummary></table>
</div>
<div>
<button type="submit" id="save">
<i class="fas fa-save"></i> Save
</button>
<button id="clear">
<i class="fas fa-eraser"></i> Clear
</button>
<button id="print" type="button" onclick="printFunction()">
<i class="fas fa-print"></i> Print
</button>
</div>
</form>
</div>
我从后端得到的 json 是
[{
"Item Code": "1001",
"Item Name": "Beverages",
},
{
"Item Code": "2003",
"Item Name": "Juices",
},
{
"Item Code": "1004",
"Item Name": "Soups",
},
{
"Item Code": "2005",
"Item Name": "Cookies",
},
]
我在ui 上创建的数量,这样我就可以存储下拉列表更改时的值
我只想一旦用户输入任何数量,即使用户转到其他下拉菜单并返回,它也应该在那里
我必须始终给0 的初始值
请大家帮帮我..我被困在这里很久了..没有得到任何 idia 我怎么能做到这一点。
我应该在ui end 或server end 上做,但数量不是来自后端,这就是我在客户端创建它的原因
请大家提供任何类型的指导,如你应该这样做,你应该这样做,这将非常有帮助
【问题讨论】:
-
您要保留输入的值还是只保留实际会话的值?
-
@PabloDarde 我不知道哪个会更好,因为我对此很陌生..当用户点击保存时,无需存储价值
-
您可以监听输入字段的更改,然后在进行更改时将值存储在本地存储中。例如 localStorage.setItem('juiceQuantity', juiceQuantity);然后每次选择一个类别时,首先检查本地存储以查看是否存在值,如果存在则将该值插入到输入字段中。
-
您能否发布一个 tableData 示例(例如果汁类别),以便我尝试一下?
-
@sarah 假设用户点击果汁然后“[{“商品代码”:“1056”,“商品名称”:“香蕉奶昔”,},{“商品代码”:“2883” , "项目名称": "芒果奶昔", }, { "项目代码": "1664", "项目名称": "苹果汁", }, { "项目代码": "2705", "项目名称": "草莓奶昔", }, ] 对其他人相同,数据格式也相同,只是名称已更改
标签: javascript jquery html