简体   繁体   English

Liftweb CRUD:如何在HTML表格中获取已编辑的内容并将其重新提升?

[英]Liftweb CRUD : How to get edited content in an HTML table an return it to lift?

I have a lift code that render an HTML table with some cells with editable content. 我有一个电梯代码,用于呈现带有可编辑内容的一些单元格的HTML表格。

For editing I'm using editinplace.js 为了编辑我正在使用editinplace.js

<script src="/js/jquery.editinplace.js"></script> 

I set the editable attribute in lift rendering the table: 我在电梯渲染表中设置了editable属性:

def renderTable(columns: List[(String,List[Model])]) = {
          {val entityCountMax = columns.maxBy(_._2.size)
           for(i <- 0 until entityCountMax._2.size) yield {
            <table style="font-size:80%; float: left; margin-left: 30px; width: auto;" class="table table-striped table-condensed table-hover">
                <tr>{ <th> { prettifyClassName(entityCountMax._2(i))} </th> % Attribute(None, "colspan", Text((columns.size+1).toString), Null)}</tr>
                {renderHeader(columns.map(_._1))}
            {for((key,value) <- entityCountMax._2(i).fields) yield {
            <tr>
            <th style="text-align: right;">{key}</th>
            {for(j <- 0 until columns.size) yield {
              <td>{try{
                            printField(columns(j)._2(i).fields.get(key))
                  }catch { case e:Exception => ""}
             }</td> % Attribute(None, "class", Text({ if(columns(j)._1 contains "DB") "editable" else "" }), Null)
            }}
            </tr>
        }}
        </table> 
         }} ++
          <script><![CDATA[ 
            $(document).ready(function(){
            $('.editable').editInPlace({ callback: function(unused, enteredText){ 
            $(this).css('color','red'); return enteredText;},
            default_text: ''
            })});]]>
          </script>
        } 

Don't look too much at rendering function above, it's working fine and I can edit the wanted cells with no problems. 不要过多地考虑上面的渲染功能,它工作正常,我可以毫无问题地编辑想要的单元格。 Edited cells are colored in red. 编辑的细胞用红色着色。

What I want to do now is to get the content of edited cells and return it to my scala application but I have no idea how to do it. 我现在要做的是获取已编辑单元格的内容并将其返回到我的scala应用程序,但我不知道该怎么做。

What I was thinking to do is to build a DIY lift CRUD for my model. 我想要做的是为我的模型建造一个DIY升降机CRUD。 The problem is that this way (I'm guessing) I have to make an html page to edit every cells one-by-one but I don't like this solution very much. 问题是这种方式(我猜)我必须制作一个html页面来逐个编辑每个单元格,但我不太喜欢这个解决方案。

So here comes the real question: 所以这是真正的问题:

Is it possible to directly map the html table with my data structure in scala? 是否可以使用scala中的数据结构直接映射html表? And If it is, how can I do it? 如果是,我该怎么办?

If you are just looking to directly map a variable to the server, and have changes sent automatically - Lift provides a wiring mechanism , which will send updates as they happen back to the server as well as update any dependent data. 如果您只是想直接将变量映射到服务器,并自动发送更改 - Lift提供了一种连接机制 ,它会在更新发回服务器时发送更新,并更新任何相关数据。 You'd still need to handle the event to persist any changes. 您仍然需要处理该事件以保留任何更改。 Also, I am not sure how well it will work with the mechanism you have created. 另外,我不确定它对你创建的机制有多好。

I know you are using an external library, but out of the box Lift offers SHtml.swappable which can provide you with a mechanism for toggling between an editable state and a non-editable state. 我知道你正在使用一个外部库,但开箱即用的Lift提供SHtml.swappable它可以为您提供了一个可编辑状态和非编辑状态之间切换的机制。 When used in combination with an ajaxText box, will provide you with a way of editing data in place, ie: 当与ajaxText框结合使用时,将为您提供一种编辑数据的方法,即:

var name="myname"
SHtml.swappable(
  <span>{name}</span>, 
  SHtml.ajaxText(name, (v) => name = v)
)

If you are just generating a NodeSeq and want to return that to the browser processed by Lift , you can take a look at LiftSession.processSurroundAndInclude 如果您只是生成NodeSeq并希望将其返回到Lift处理的浏览器,您可以查看LiftSession.processSurroundAndInclude

I find Lift's functions work best if you use their CSS Selectors and response processing. 如果您使用CSS选择器和响应处理,我发现Lift的功能最有效。 However, if you are creating the forms using Javascript, and your library is responsible for managing the data, you'd probably be better off using a Rest type service to get the data back into your app. 但是,如果您使用Javascript创建表单,并且您的库负责管理数据,那么最好使用Rest类型服务将数据恢复到您的应用程序中。 RestHelper makes that pretty easy. RestHelper使这很容易。 I've used it often with AngularJS for doing a similar type app to what it sounds like you are trying for. 我经常和AngularJS一起使用它来做类似的应用程序,就像你想要的那样。

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

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