当前位置: 首页 > 知识库问答 >
问题:

单击DIV应用动态数据绑定,使用超文本标记语言输入编辑CSS-knockout.js

程鸿波
2023-03-14

我正在尝试编辑数组的各个元素。我有一个JSON数组,如下所示:

[   {"idComponent":1,"left":"100px","top":"100px","idHTML":"name_div","width":"200px","height":"300px","value":"My name is"}
               ,{"idComponent":2,"left":"200px","top":"200px","idHTML":"date_div","width":"200px","height":"300px","value":"2016-Enero-12"}
               ,{"idComponent":3,"left":"300px","top":"300px","idHTML":"weigth_div","width":"200px","height":"300px","value":"1.5 KG"}
               ,{"idComponent":4,"left":"400px","top":"400px","idHTML":"talla_div","width":"200px","height":"300px","value":"23"}
               ,{"idComponent":5,"left":"500px","top":"500px","idHTML":"description_div","width":"300px","height":"300px","value":"The text"}
            ]

这些被转化为淘汰赛中的可观测值。每一个都绑定到一个DIV元素以显示在屏幕中。“left”、“top”、“height”和“width”是应用于每个CSS属性的CSS属性。

当您用鼠标单击DIV包含的元素时,我试图将元素绑定到超文本标记语言输入以编辑CSS值。你可以在下一张图片中看到这个想法:

模型图像-单击此处查看图像

代码是下一个:

  function convertPixelsToInches(pixels){
    return pixels/96;
  }

  // Elemento regresados desde el servidor.
  var dataFromServer =
  {
     "idTemplate":"1"
    ,"components" :
      [{"idComponent":1,"left":"100px","top":"100px","idHTML":"name_div","width":"200px","height":"300px","value":"Sergio Pinto Fernandez"}
      ,{"idComponent":2,"left":"200px","top":"200px","idHTML":"date_div","width":"200px","height":"300px","value":"2016-Enero-12"}
      ,{"idComponent":3,"left":"300px","top":"300px","idHTML":"weigth_div","width":"200px","height":"300px","value":"1.5 KG"}
      ,{"idComponent":4,"left":"400px","top":"400px","idHTML":"talla_div","width":"200px","height":"300px","value":"23"}
      ,{"idComponent":5,"left":"500px","top":"500px","idHTML":"description_div","width":"300px","height":"300px","value":"Tomar dos cucharadas cada 3 hras."}
      ]
    ,"paperSize":"papersize_USLetter_portrait"
    ,"templateImage":{
      "imageUrl":"images/SolicitudLaboratorioExpress.jpg"
      ,"width":"8.5in"
      ,"height":"11in"
      ,"left":"0px"
      ,"top":"0px"
    }
  };

  function componentModel(dataComponent) {
    if (!dataComponent) {
      dataComponent = {};
    }

    var self = this;
    self.idComponent  = ko.observable(dataComponent.idComponent);
    self.left         = ko.observable(dataComponent.left);
    self.top          = ko.observable(dataComponent.top);
    self.idHTML       = ko.observable(dataComponent.idHTML);
    self.width        = ko.observable(dataComponent.width);
    self.height       = ko.observable(dataComponent.height);
    self.value        = ko.observable(dataComponent.value);
  }

  /**
   * data Json from server.
   *
   */
  function templateModel(data) {
    if (!data) {
      data = {};
    }
    var self = this;

    self.components = ExtractComponents(self, data.components, componentModel);

    self.currentSelectedComponent = ko.observable();
    self.currentSelectedComponentIndex = ko.observable(-1);

    //self.currentSelectedComponentLeft = ko.observable();
    self.currentSelectedComponentLeft = ko.computed(function(){
      var value = self.currentSelectedComponentIndex();
      console.log(typeof value);
      //value=value*1;
      console.log(value);
      if (value ) {
        return "TT";
      }

      // Get "200px y must save as 200"
      //return self.currentSelectedComponent().left;//.substring(0,data.length-2);
      return "FF";
    });
    self.currentSelectedComponentTop = ko.observable();
    self.editComponent = function(component,index){
      self.currentSelectedComponentIndex(index);
      self.currentSelectedComponent(component);

      // Binding the component to the editor.
      // ??
    };
    function bindComponentToEditor() {
      ko.applyBindings()
    }


    self.idTemplate = ko.observable(data.idTemplate);
    self.paperSize = ko.observable(data.paperSize);

    /* */
    self.paperSizeWidth = ko.observable(convertPixelsToInches(1067));
    self.paperSizeHeigth = ko.observable(convertPixelsToInches(1067));
    //
    self.templateImage_imageUrl = ko.observable(data.templateImage.imageUrl);
    self.templateImage_width = ko.observable(data.templateImage.width);
    self.templateImage_height = ko.observable(data.templateImage.height);
    self.templateImage_left = ko.observable(data.templateImage.left);
    self.templateImage_top = ko.observable(data.templateImage.top);
  }

  /**
   * parent: referencia al objeto o funcion que mando a llamar esta fucnion.
   *
   * dataArr: Array de elementos que se desea la funcion ExtractComponents haga mapeo.
   *    Ejemplo de dataArr:
   *
   *        [   {"idComponent":1,"left":"100px","top":"100px","idHTML":"name_div","width":"200px","height":"300px","value":"Sergio Pinto Fernandez"}
           ,{"idComponent":2,"left":"200px","top":"200px","idHTML":"date_div","width":"200px","height":"300px","value":"2016-Enero-12"}
           ,{"idComponent":3,"left":"300px","top":"300px","idHTML":"weigth_div","width":"200px","height":"300px","value":"1.5 KG"}
           ,{"idComponent":4,"left":"400px","top":"400px","idHTML":"talla_div","width":"200px","height":"300px","value":"23"}
           ,{"idComponent":5,"left":"500px","top":"500px","idHTML":"description_div","width":"300px","height":"300px","value":"Tomar dos cucharadas cada 3 hras."}
        ]
   *
   * modelConstructor:  funcion con que se creara un nuevo componente, es decir el modelo.
   *
   */
  function ExtractComponents(parent, dataArr, modelConstructor) {
    var components = [];
    if (dataArr == null) {
      return components;
    }

    for (var i = 0; i < dataArr.length; i++) {
      var dataArrElement = dataArr[i];
      var component = new modelConstructor(dataArrElement,parent);
      components.push(component);
    }
    return components;
  }

  ko.applyBindings(new templateModel(dataFromServer));

我有两个问题:

>

自己currentSelectedComponentLeft=ko。computed(function(){var value=self.currentSelectedComponentIndex();console.log(type of value);//value=value*1;console.log(value);if(value){返回“TT”;}

  // Get "200px and save as 200"
  //return self.currentSelectedComponent().left;//.substring(0,data.length-2);
  return "FF";
});

主要问题是。。。当我单击右边的输入时,如何绑定DIV元素?我想我需要动态绑定,或者动态订阅,但是找不到动态双数据绑定问题的答案。

这是HTML:

<div id="mainContainer">
  <div id="contentPrint_div" class="page" data-bind="css: paperSize">

    <img id="template_img" data-bind="attr: {src: templateImage_imageUrl},
      style: {width: templateImage_width, height: templateImage_height, left: templateImage_left, top: templateImage_top}">

    <div id="fieldsArea_div" class="" data-bind="foreach: components">
      <div class="field" data-bind="html: value, style: {width: width, left:left, top:top},
         attr: {id: idHTML}, click: $parent.editComponent($data,$index)"></div>
    </div>
  </div>

  <div id="toolsbar">
    <div id="toolbarPanel">
      ID template:<span data-bind='text: idTemplate'></span>

      <div id="panelMenuInfoElements_div">
        Elemento actual: <span data-bind='text: idTemplate'></span>
        Posicion
        X:<input type="text" class="form-control" id="" placeholder="x" min="0" step="0.01"
            data-bind="attr: {max: paperSizeWidth}, value: currentSelectedComponentTop">
        Y:<input type="text" class="form-control" id="" placeholder="y" min="0" step="0.01"
            data-bind="attr: {max: paperSizeHeigth}, value: currentSelectedComponentLeft">
      </div>

    </div>
    <div id="toolbarCode">
      <pre data-bind="text: ko.toJSON($root, null, 2)"></pre>
    </div>

  </div>
</div>

共有1个答案

益稳
2023-03-14

你发布了相当多的代码;我不能100%确定我是否正确地回答了你所有的问题。但是我试图创造一个例子,我认为它展示了如何解决你的问题。

  • 我使用了一个computed来创建一个style对象,该对象将xy作为数字的可观察对象重写为样式绑定兼容:{top:0px,left:0px}
var Block = function(name, x, y) {
  this.label = name;
  this.x = ko.observable(x);
  this.y = ko.observable(y);

  this.style = ko.computed(function() {
    return {
      top: this.y() + "px",
      left: this.x() + "px"
    }
  }, this);
}


var VM = function() {

  this.blocks = [
    new Block("Block 1", 100, 100),
    new Block("Block 2", 200, 100),
    new Block("Block 3", 300, 100),
  ];

  this.selectedBlock = ko.observable(this.blocks[0]);
};

ko.applyBindings(new VM());
.Block {
  position: absolute;
  background: red;
  padding: 1rem;
  list-style: none;
}
.Block.is-selected { background: yellow; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<div class="Inputs" data-bind="with: selectedBlock">
  <div>
    Editing <span data-bind="text: label"></span> 
  </div>
  <div>Left:
    <input type="number" data-bind="value: x">
  </div>
  <div>Top:
    <input type="number" data-bind="value: y">
  </div>
</div>

<ul data-bind="foreach: blocks">
  <li class="Block" data-bind="
    click: $parent.selectedBlock,   
    style: style,
    text: label,
    css: { 'is-selected': $data == $parent.selectedBlock()}"></li>
</ul>
 类似资料:
  • 因此,这又和我的另一个问题链接到同一个网站,这是另一个定位问题,可能很简单。我有一个容器div,我想在里面放两个div,一个占据容器的三分之一,在右边包含图片,一个包含文本,在左边。然而,由于某种原因,当告诉两个内部div向左浮动时,容器似乎消失了,而当使用inspect元素时,它处于一个我无法解释的奇怪位置。 HTML: CSS: Jscript/jQuery文件只是淡入淡出,但对定位没有影响

  • 对于上面的html内容,我如何使用Jsoup解析并获取文本 当我使用 我得到了这样的东西

  • 我有一点HTML应该调用/nodes/calibleph?值=浮点数: 编辑:这家伙似乎也有同样的问题

  • 我正在将与一起使用-直到最近它都运行良好。这是出现此问题的布局之一: 但是现在,每当用户尝试输入任何内容时,应用程序都会冻结,会被一遍又一遍的重复消息淹没: 如果我删除问题就不复存在了,似乎是因为试图在无休止的循环中更改xml字段 直到几天前,它还很好用。我试过了: > < li> 删除< code > Android:ime options = " action done " 删除 从视图层中删

  • 我是新来的。我想解析html,但问题是我们必须在中指定的URL,我将在运行时从其他页面响应此URL。有没有办法将收到的网址传递到中?我读过这样的东西: 但是我不知道如何使用它。我很想知道是否有其他方法比jsoup更好。

  • 我正在尝试使用放心来检查服务器返回的HTML文档的一些属性。演示该问题的SSCCE如下所示: 现在,此尝试以,这是由所有可能的错误大约 30 秒左右后超时! 如果我用< code>xmlPathConfig()删除这一行。用()。功能(...)当特性“http://Apache . org/XML/features/disallow-DOCTYPE-decl”设置为true时,由于< code>D