简体   繁体   English

如何在Asp.Net Core中动态更新表的每一行

[英]How to update each row of a table dynamically in a Asp.Net Core

I'm developing a web application using Asp.Net Core(Razor Pages),and I have a table with three columns,one representing cellphone numbers,the other one text messages that are supposed to be sent to each one and the last one showing results.I'm looking for a way to update the last column of each row and have it highlighted as each messages is being sent to the cellphone number by clicking the Send To All button down the table.How can I accomplish it? 我正在使用Asp.Net Core(Razor Pages)开发一个Web应用程序,并且我有一个包含三列的表格,一个代表手机号码,另一个应发送给每个人的短信,最后一个显示结果,我正在寻找一种方法来更新每行的最后一列,并在每条消息都通过单击表格下方的全部发送按钮发送到手机号码时突出显示它,该如何完成呢? Thanks for your responses in advance. 感谢您的提前答复。

    <div class="row mt-2">
        <table class="table table-striped table-responsive-md">
            <thead class="thead-dark">
                <tr>
                    <th>
                        @Html.DisplayNameFor(model => model.MessageList[0].ReceiverCellPhone)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.MessageList[0].Text)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.MessageList[0].Result)
                    </th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model.MessageList)
                {
                    <tr>
                        <td>
                            @Html.DisplayFor(modelItem => item.ReceiverCellPhone)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.Text)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.Result)
                        </td>
                        <td></td>
                    </tr>
                }
            </tbody>
        </table>
    <button type=submit value=Send to All/>
    </div>

I haven't used Razor syntax for a while but this is the idea: 我已经有一段时间没有使用Razor语法了,但这就是这个主意:

An array of model comes to the view and your model needs to have an ID property. 模型数组进入视图,并且您的模型需要具有ID属性。

When you're rendering the html use that ID to identify each row of your model, for example: 呈现html时,请使用该ID来标识模型的每一行,例如:

<tr id="@item.Id">

and each row can have its own trigger like this for example: 每行可以有自己的触发器,例如:

<button onClick="sendMessage(@item.Id)">

a JavaScript function can take that function sendMessage(id) and then you can query that row and update its UI. JavaScript函数可以使用该function sendMessage(id) ,然后您可以查询该行并更新其UI。 for example before sending the request you can create an loading element and using JavaScript promise, update that to a success or fail icon. 例如,在发送请求之前,您可以创建一个加载元素,并使用JavaScript promise将其更新为成功或失败图标。

But if I'm understanding right you want to have a send all button. 但是,如果我理解正确,那么您想要一个“全部发送”按钮。 In that case you can just query third <td> of each row and update its UI: 在这种情况下,您只需查询每行的第三<td>并更新其UI:

document.querySelectorAll('td:nth-child(3)').forEach(e => {
  //update UI
})

Hope this was helpful. 希望这会有所帮助。

I think in scenarios like this, we want to have our javascript as unobtrusive as possible by attaching events to actions through javascript rather than through the html attributes. 我认为在这种情况下,我们希望通过将事件附加到通过javascript(而不是通过html属性)到动作的方法,来使我们的javascript尽可能不引人注意。

with that, you would probably want something like the following: 这样,您可能想要以下内容:

document.getElementById('ButtonId').addEventListener('click', function() {
    // you are now in the button click context

    // you can now either fire one asyncronous request which encapsulates all the 
       rows or you can iterate through the table rows and fire individual requests. The below shows the second example

    var table = document.getElementById('TableId');
    var url = "url to endpoint you want to call';

    for (var i = 1; i < table.rows.length; i++) { // 1 so we miss the header
         // get the relevant id of the request you want to send
         var id = "whatever cell you need";
         let request = new XMLHttpRequest();
         request.onreadystatechange = function () {
             if (this.readyState === 4) {
                 //success so you can change the cell you want to change
             } else {
                 document.body.className = 'error';
             }
         }
         request.open("post", url, true);
         request.send('this should be whatever you want to send to the request - id object maybe");
     }
 });

if you want to put the function as a proper function or variable then you can easily do that as well to make the code a little easier to read 如果您想将函数作为适当的函数或变量,那么您也可以轻松地做到这一点,以使代码更易于阅读

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

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