简体   繁体   English

ASP.NET MVC / jQuery:就地编辑

[英]ASP.NET MVC/jQuery: Inplace Editing

I want to do inplace editing where I have regular text (coming from a Model) with an edit button next to it. 我想在常规文本(来自模型)及其旁边的编辑按钮的地方进行就地编辑。 When the edit button is clicked, I want a editable textbox to appear where the regular text was, and then commit a change via AJAX. 单击编辑按钮时,我希望可编辑文本框出现在常规文本的位置,然后通过AJAX进行更改。 I already solved how to commit, but I don't know how to place the editing control accurately where the text was before. 我已经解决了如何提交的问题,但是我不知道如何将编辑控件准确地放置在文本之前的位置。 This probably easy for you JS gurus out there. 这对您的JS专家可能很容易。

Besides the placement issue I wondered if I should fetch the appropriate edit box (textbox for regular text, calendar for dates etc.) via AJAX when the edit button is clicked or would it be better to just send it off with the page and keep it hidden until needed? 除了放置问题之外,我想知道是否应该在单击编辑按钮时通过AJAX来获取适当的编辑框(常规文本的文本框,日期的日历等),还是最好随页面一起发送并保留它隐藏直到需要? Or can I reuse a single instance on the client side somehow? 还是可以以某种方式在客户端重用单个实例?

Sorry this is kind of two questions in one but they are tightly related. 抱歉,这是两个问题合而为一,但它们是紧密相关的。

Thank you! 谢谢!

Not sure to understand the button placement problem. 不确定是否了解按钮放置问题。 However, there are a number of jquery plugins designed for this purpose. 但是,有许多为此目的而设计的jQuery插件。 One of these is Jeditable, find a demo here ! 其中之一是可编辑的,请在此处找到演示!

Store the markup for the edit in a variable, store the current markup before the edit in a variable (so you can revert if the cancel button is pressed) and then switch it out using jquery: 将编辑的标记存储在变量中,将编辑之前的当前标记存储在变量中(以便您可以在按下取消按钮的情况下还原),然后使用jquery将其切换出来:

var editmarkupvariable = [editmarkup];
var revertmarkup = $("#item").html();
<div class="item">
   [markup]
</div>

$("#item").html(editmarkupvariable);

This tutorial gives more detail: http://15daysofjquery.com/edit-in-place-with-ajax-using-jquery-javascript-brary/15/ 本教程提供了更多详细信息: http : //15daysofjquery.com/edit-in-place-with-ajax-using-jquery-javascript-brary/15/

That should answer both your questions. 那应该回答您的两个问题。

I would google for a jquery plugin to to do this, unless you have to hand code it. 我会用谷歌搜索一个jquery插件来做到这一点,除非您必须手工编写它。 Here is one: http://aktagon.com/projects/jquery/in-place-edit 这是一个: http : //aktagon.com/projects/jquery/in-place-edit

I would recommend JEditable I have used it before and it works very well. 我会推荐我以前使用过的JEditable,它工作得很好。

An example: 一个例子:

The Markup 标记

<div class="editable_text" id="my_text">My Text</div><a href="#" class="trigger_editable">Edit</a>

The Jquery jQuery的

$(".editable_text").editable("YOUREDITURLHERE", { event : "edit" });
$(".trigger_editable").bind("click", function() {
        $(this).prev().trigger("edit");
});

Hope this helps. 希望这可以帮助。

如果问题是严格的文本编辑,那么为什么在单击编辑按钮时不能同时使用单个文本区域和切换disable属性(可能还有background-colorborder样式)?

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

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