简体   繁体   English

如何仅捕获用户更改值的多行中的那些列?

[英]How do I capture only those columns in multiple rows in which user has changed values?

Are there are any approaches in ASP.NET or JavaScript where i run update stored procedure on only those rows in which specific columns have been updated by user?在 ASP.NET 或 JavaScript 中是否有任何方法可以让我仅在用户更新了特定列的那些行上运行更新存储过程?

For example: if there are multiple rows例如:如果有多行

row1= [column1 column2 column3 column4 coumn 5]
row2= [column1 column2 column3 column4 coumn 5]
row3= [column1 column2 column3 column4 coumn 5]
row4= [column1 column2 column3 column4 coumn 5]

If the column 2 and Column 3 are updated in row 1 and column 1 and column 4 are update in row 3 i want to capture only these rows and columns and pass it as JSON string to List which I am then converting to DataTable?如果第 2 列和第 3 列在第 1 行中更新,第 1 列和第 4 列在第 3 行中更新,我只想捕获这些行和列并将其作为 JSON 字符串传递给 List,然后我将其转换为 DataTable?

What approach should I take ?我应该采取什么方法?

What I have tried:我尝试过的:

Googled some samples but an expert opinion is always best谷歌搜索了一些样本,但专家意见总是最好的

There is a very convoluted way to achieve this.有一种非常复杂的方法来实现这一点。 If anyone knows of a cleaner way, please post it.如果有人知道更清洁的方法,发布。

You need to do the following:您需要执行以下操作:

Add to your Model: - a Boolean property (column) that will be hidden and used as a change Identifier添加到您的模型: - 将被隐藏并用作更改标识符的布尔属性(列)

Set up your table in your view like this (my example is in Razor):像这样在您的视图中设置您的表格(我的示例是在 Razor 中):

@{
int j = 1;
for (var i = 0; i < Model.itemList.Count; i++)
{
     <tr>
          <td>
               @Html.TextBoxFor(model => model.itemList[i].columnOne, new { onkeypress = "markAsChanged(" + @j + ", " + @i + ")" })
          </td>
          <td>
               @Html.TextBoxFor(model => model.itemList[i].columnTwo, new { onkeypress = "markAsChanged(" + @j + ", " + @i + ")" })
          </td>
          <td>
               @Html.TextBoxFor(model => model.itemList[i].columnThree, new { onkeypress = "markAsChanged(" + @j + ", " + @i + ")" })
          </td>
          <td>
               @Html.TextBoxFor(model => model.itemList[i].columnFour, new { onkeypress = "markAsChanged(" + @j + ", " + @i + ")" })
          </td>
          <td>
               @Html.TextBoxFor(model => model.itemList[i].columnFive, new { onkeypress = "markAsChanged(" + @j + ", " + @i + ")" })
          </td>
          <td>
               @Html.HiddenFor(model => model.itemList[i].IsChanged)
          </td>
     </tr>
     j++;
}

Your JavaScript function will edit the hidden element as soon as someone changes a field for a row:一旦有人更改了一行的字段,您的 JavaScript 函数就会编辑隐藏元素:

function rerunOldChange(rowIndex, changeIndex) {
    var table = document.getElementById("changeTable");
    var j = rowIndex;
    var i = changeIndex;

    var currentRow = table.rows[j];
    var cellSix = currentRow.cells[5];

    var thisRowIsChanged = "<input type = 'hidden' id = 'itemList_" + i + "__IsChanged' name = 'itemList[" + i + "].IsChanged' value = 'true' />"

    cellSix.innerHTML = thisRowIsChanged;
}

What it is doing is changing the value of the boolean as soon as someone presses a key on that field.它所做的是在有人按下该字段上的键时立即更改布尔值的值。 So when you pass the list to your controller in order to post, you can do so only for rows that have a boolean value of True.因此,当您将列表传递给控制器​​以发布时,您只能对布尔值为 True 的行执行此操作。

I'll tack on some updates and simplification to @C Murphy's response, but setting a hidden field in your viewmodel is the best option.我将对@C Murphy 的回复进行一些更新和简化,但在您的视图模型中设置一个隐藏字段是最好的选择。 I would recommend using a checkbox, as it is boolean by default and easy to manipulate with its checked attribute.我建议使用复选框,因为默认情况下它是布尔值,并且易于使用其checked属性进行操作。

The keypress event is deprecated , and doesn't fire on all keys - in particular, the Backspace and Delete keys do not trigger it, so someone could delete everything in a field and no update would occur. keypress 事件已弃用,并且不会在所有键上触发 - 特别是BackspaceDelete键不会触发它,因此有人可以删除字段中的所有内容并且不会发生更新。 Instead, use the keydown event;相反,使用keydown事件; this has sort of the opposite effect, as every key triggers it, including Shift and other keys that may not actually change the field.这有点相反的效果,因为每个键都会触发它,包括Shift和其他实际上可能不会改变字段的键。 You can validate changes in other code if you want to.如果需要,您可以验证其他代码中的更改。

I removed the code checking for changes per column, because I assume your update logic asks for all the object's data, or with EF probably expects an entire object to be passed.我删除了检查每列更改的代码,因为我假设您的更新逻辑要求提供所有对象的数据,或者 EF 可能希望传递整个对象。

Razor:剃刀:

// A text field, change PropertyName and add where needed in markup
@Html.TextBoxFor(model => model.itemList[i].PropertyName, new { onkeydown = "markAsChanged(" + i + ")" })

// Hidden checkbox for the IsChanged property
@Html.CheckBoxFor(model => model.itemList[i].PropertyName, new { hidden = "" })

The Javascript: Javascript:

// Modern browsers
var markAsChanged = function(i) {
    document.GetElementById('itemList_' + i + '__IsChanged').checked = true;
};

// Use for legacy support with jQuery
var markAsChanged = function(i) {
    $('#itemList_' + i + '__IsChanged').checked = true;
};

If you wanted to clean up your view and leave out the event handlers, you could add them with some more Javascript.如果您想清理您的视图并忽略事件处理程序,您可以使用更多的 Javascript 添加它们。

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

相关问题 如何仅传递在 POST 请求正文中更改的那些值 - how to pass only those values which changed in POST request body 如何仅从以下包含一些数据的字典中提取那些元素? - How do I extract only those elements from following dictionary which has some data in it? jQuery仅循环其中具有值的那些输入 - Jquery loop through only those inputs which has values in it 如何搜索 HTML 表中的所有列。 我的桌子有多个<th>行 - How do I search all the columns in the HTML table. My table has multiple <th> rows 如何将行转置为值数组中的列? - How do I transpose rows to columns in a array of values? 我只想为那些具有 ManageMes​​sages 权限的角色执行我的 nuke 命令。我该怎么做? - I want to make my nuke command only for those role which have ManageMessages permission.How do i do this? 如何解析具有多个值的属性指令? - How can I parse an attribute directive which has multiple values? 如何使用仅具有“数据捕获”且没有类的JQuery单击按钮? - How do I click a button using JQuery that only has 'Data-capture' and no class? 当数据已更改时,如何在 Realm Javascript 中获取旧的修改值 - How do i get old modification values in Realm Javascript when that data has been changed 如何捕获每个文本框的值(仅JavaScript)并显示它们? - How do I capture values (JavaScript Only) of each text box and display them?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM