从textInput字段自动更新observablearray的当前行中的字段

时间:2015-05-13 12:42:32

标签: javascript knockout.js

我有一个绑定到视图模型的表。当我选择表格行时,字段(注释)会从此更新:

javax.servlet.ServletException: Servlet.init() for servlet com.test.rest.RestApplicationConfig threw exception
    org.apache.tomee.catalina.OpenEJBValve.invoke(OpenEJBValve.java:44)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:745)
root cause

java.lang.RuntimeException: javax.naming.NameNotFoundException: Name [com] is not bound in this Context. Unable to find [com].
    com.sun.jersey.server.impl.cdi.CDIExtension.getInitializedExtension(CDIExtension.java:182)
    com.sun.jersey.server.impl.cdi.CDIComponentProviderFactory.<init>(CDIComponentProviderFactory.java:95)
    com.sun.jersey.server.impl.cdi.CDIComponentProviderFactoryInitializer.initialize(CDIComponentProviderFactoryInitializer.java:76)
    com.sun.jersey.spi.container.servlet.WebComponent.configure(WebComponent.java:572)
    com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.configure(ServletContainer.java:332)
    com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:604)
    com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:207)
    com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:394)
    com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:577)
    javax.servlet.GenericServlet.init(GenericServlet.java:158)
    org.apache.tomee.catalina.OpenEJBValve.invoke(OpenEJBValve.java:44)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:745)
root cause

javax.naming.NameNotFoundException: Name [com] is not bound in this Context. Unable to find [com].
    org.apache.naming.NamingContext.lookup(NamingContext.java:819)
    org.apache.naming.NamingContext.lookup(NamingContext.java:167)
    org.apache.naming.SelectorContext.lookup(SelectorContext.java:156)
    javax.naming.InitialContext.lookup(InitialContext.java:417)
    com.sun.jersey.server.impl.cdi.CDIExtension$2.stepInto(CDIExtension.java:295)
    com.sun.jersey.server.impl.cdi.CDIExtension.diveIntoJNDIContext(CDIExtension.java:272)
    com.sun.jersey.server.impl.cdi.CDIExtension.lookupJerseyConfigJNDIContext(CDIExtension.java:292)
    com.sun.jersey.server.impl.cdi.CDIExtension.getInitializedExtension(CDIExtension.java:180)
    com.sun.jersey.server.impl.cdi.CDIComponentProviderFactory.<init>(CDIComponentProviderFactory.java:95)
    com.sun.jersey.server.impl.cdi.CDIComponentProviderFactoryInitializer.initialize(CDIComponentProviderFactoryInitializer.java:76)
    com.sun.jersey.spi.container.servlet.WebComponent.configure(WebComponent.java:572)
    com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.configure(ServletContainer.java:332)
    com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:604)
    com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:207)
    com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:394)
    com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:577)
    javax.servlet.GenericServlet.init(GenericServlet.java:158)
    org.apache.tomee.catalina.OpenEJBValve.invoke(OpenEJBValve.java:44)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:745)

与表格相同的div中的字段(这是在选择上表中的行时应更新的单个文本区域,并在用户选择另一行时更新表格。)

<tbody data-bind="foreach: namespace.PersonResults.model">
    <tr data-bind="click: $root.selectItem, css: {selected: $root.isSelected($data)}">
        <td data-bind="text: Forename"></td>
        <td data-bind="text: Surname"></td>
        <td data-bind="text: PostCode"></td>
        <td data-bind="text: Notes" style="display: none"></td>
    </tr>
</tbody>

viewModel目前正在执行此操作:

<textarea data-bind="textInput: editNotes"></textarea>

这适用于在var resultsViewModel = function() { var self = this; self.model = ko.observableArray(); self.editNotes = ko.observable(); self.selectItem = function(record) { self.selectedItem(record); self.editNotes(record.Notes); } self.getData () { // some ajax stuff to populate the table } } 中显示注释,但如何让其他方式显示,如果用户更改了内容,则填充textarea中的字段observableArray

2 个答案:

答案 0 :(得分:1)

您需要绑定到value的{​​{1}}:

textarea

我在this fiddle

中敲了一个快速简化的演示

编辑添加:

这是一个更新的小提琴,更符合您的要求:fiddle

答案 1 :(得分:1)

您只需将textarea绑定到与var resultsViewModel = function() { var self = this; self.editNotes = ko.observable('initial value'); } var vm = { selectedResult: ko.observable(null), results: [new resultsViewModel(), new resultsViewModel()] }; vm.selectResult = function(result) { vm.selectedResult(result); }; ko.applyBindings(vm);相同的属性即可。 E.g:

.selected { background-color: pink; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>

<table><tbody data-bind="foreach: results">
    <tr data-bind="css: { selected: $root.selectedResult() === $data }">
        <td data-bind="text: editNotes"></td>
        <td><button data-bind="click: $root.selectResult">Select</button></td>
    </tr>
</tbody></table>

<!-- ko with: selectedResult -->
<textarea data-bind="textInput: editNotes"></textarea>
<!-- /ko -->
POST http://<OUR WSDL>HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: text/xml;charset=UTF-8
SOAPAction: ""
Authorization: Basic c2FtczpzYW1z
Content-Length: 2920
Host: hostnameqa-web.cable.com:80
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://xml.comcast.com/sams/tts/consumerservice/v1_2">
   <soapenv:Header>
      <wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
         <wsse:UsernameToken wsu:Id="UsernameToken-42">
            <wsse:Username>username1</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">pifAlJJNQAo8Tqi+F95kpz+8UvM=</wsse:Password>
            <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">99lpCD9kwSvUd78EA3Humw==</wsse:Nonce>
            <wsu:Created>2015-05-13T15:18:10.100Z</wsu:Created>
         </wsse:UsernameToken>
      </wsse:Security>
   </soapenv:Header>
   <soapenv:Body>
      <v1:create>
         <!--Optional:-->
         <v1:CreateConsTicketRequest>

            <Header>
               <Requester>requestor</Requester>
               <Submitter>
                  <Type>Real</Type> 
                  <Value>Submitter</Value> 
               </Submitter>
               <TimeStamp>
                  <Date>2014-05-30</Date>
                  <Time>22:38:26</Time>
                  <TimeZone>UTC</TimeZone>
               </TimeStamp>
               <TransactionId>1</TransactionId> 
            </Header>

            <Body>
               <CTicket>
                  <ContactInformation>
                     <Name>First Name123, Last Name123</Name>
                     <Phone>1234567893</Phone>
                     <Email>email@testing.com</Email>
                  </ContactInformation>

                  <TimeStart>
                     <Date>2013-09-12</Date>
                     <Time>09:31:23</Time>
                     <TimeZone>UTC</TimeZone>
                  </TimeStart>

               </CTicket>
            </Body>
         </v1:CreateConsTicketRequest>
      </v1:create>
   </soapenv:Body>
</soapenv:Envelope>