简体   繁体   English

在SAPUI5中使所选行的单元格可编辑

[英]Make cells of selected rows editable in SAPUI5

I got a table where i need to edit 2 cells of selected rows i tryed this first on my own with this code rubbish because get rows always got index of 0-9 and my getSelectedIndices get the real index. 我得到了一个表格,我需要编辑所选行的2个单元格,我首先使用这个代码垃圾自己尝试这个,因为获取行总是得到0-9的索引,而我的getSelectedIndices得到真正的索引。

var oTable1 = this.byId("table1");
var haveToChange = oTable1.getSelectedIndices();
var rows = oTable1.getRows();
for (i = 0; i < haveToChange.length; i++) {
    var cells = rows[haveToChange[i]].getCells();
    if (cells[10].mProperties.editable == false) {
        cells[10].setEditable(true);
        cells[12].setEditable(true);
        cells[13].setEditable(true);
    }
    else
    {
        cells[10].setEditable(false);
        var getit = cells[10].getValue();


        if (cells[6]._sRenderedValue.indexOf(",") > -1)
        {
            var Woerter = cells[6]._sRenderedValue.split(',');
            var zahl = Woerter[0] + ".";
            for (var i = 1 ; i < Woerter.length; i++) {
                zahl = zahl + Woerter[i];
            }
            if (getit.indexOf(".") > -1)
            {
                var arrays = getit.split(".");
                getit = "";
                for(var i = 0; i<arrays.length; i++)
                {            
                    getit = getit + arrays[i];
                }
            }


            var honorar = parseInt(getit) * parseFloat(zahl);
        }
        else
        {
            var zahl = "";
            if (cells[6]._sRenderedValue.indexOf(".") > -1) {
                var Woerter = cells[6]._sRenderedValue.split('.');
                for ( var i = 0 ; i < Woerter.length; i++) {
                    zahl = zahl + Woerter[i];
                }
                if (getit.indexOf(".") > -1) {
                    var arrays = getit.split(".");
                    getit = "";
                    for (var i = 0; i < arrays.length; i++) {
                        getit = getit + arrays[i];
                    }
                }


            }
            honorar = parseInt(getit) * parseInt(zahl);
        }
        cells[11].setValue(x);
        cells[12].setEditable(false);
        cells[13].setEditable(false);
    }
}

} }

Secondly i tried to follow some tutorial the problem is that it makes the complete row editable but too the stat edit is not on my row so this won't work anyway: 其次,我尝试按照一些教程,问题是它使完整的行可编辑,但是统计编辑也不在我的行上,所以这无论如何都不会起作用:

var model = this.getView().getModel("jsonmodel");
var rowPath = this.byId("table1").getBindingInfo('rows').path;
var rows = model.getProperty(rowPath);
var haveToChange = this.byId("table1")..getSelectedIndices();
for (x = 0; x < haveToChange.length; x++) {
    for (i = 0; i < rows.length; i++) {
        row = rows[i];
        if (i == haveToChange[x]) {
            row['edit'] = true; // make the selected row editable and rest non editable
        } else {
            row['edit'] = false;
        }
    }
}

so how can i archiev too get the cells 10 and 12 editable after an Index of 10? 所以我怎么能在索引为10之后将单元格10和12编辑为可编辑?

Generally, if you need to modify UI behavior (enabling/disabling, showing/hiding, etc) you should use the power of databinding 通常,如果您需要修改UI行为(启用/禁用,显示/隐藏等),您应该使用数据绑定的强大功能

In your case, you could use the editable property of the table cell's TextField control to toggle editing/readonly state upon selection, in this case I defined a property isEnabled : 在您的情况下,您可以使用表格单元格的TextField控件的editable属性在选择时切换编辑/只读状态,在这种情况下我定义了一个属性isEnabled

<TextField value="{col1}" editable="{isEnabled}" />

This implies you use a JSONModel for your table, so you can add extra UI specifiec properties, but since you want to edit a table, an ODataModel doesn't make much sense anyway in that case ;-) 这意味着您为表使用了JSONModel,因此您可以添加额外的UI特定属性,但由于您要编辑表,因此在这种情况下ODataModel无论如何都没有多大意义;-)

The table's rowSelectionChange event could call a method which then sets the property, and toggles the editable state: 表的rowSelectionChange事件可以调用一个方法然后设置属性,并切换可编辑状态:

toggleEditableFields : function(oEvent) {
    var aIndices = oEvent.getSource().getSelectedIndices(),
        myData = this.getView().getModel().getProperty("/data"),
        that = this;

    // first, set all the isEnabled properties of all records to readonly state
    $.each(myData,function(i, v){
        that.getView().getModel().setProperty("/data/" + v + "/isEnabled", false);
    });
    // then, set all the isEnabled properties of the selected records to editable state
    $.each(aIndices,function(i, v){
        that.getView().getModel().setProperty("/data/" + v + "/isEnabled", true);
    });
}

See this working example: click one or more rows in the table using the table's row selectors, and the editable state of the input fields are enabled/diabled accordingly: 请参阅此工作示例:使用表的行选择器单击表中的一行或多行,并相应地启用/禁用输入字段的可编辑状态:

 sap.ui.controller("view1.initial", { onInit : function(oEvent) { var oModel = new sap.ui.model.json.JSONModel(); oModel.setData({ data : [ { "col1": "at curabitur vestibulum", "col2": "porttitor pharetra rutrum", "col3": 93, "isEnabled": false }, { "col1": "hendrerit dui fringilla", "col2": "adipiscing suspendisse lorem", "col3": 36, "isEnabled": false }, { "col1": "placerat vel placerat", "col2": "suspendisse quis sit", "col3": 9, "isEnabled": false }, { "col1": "sagittis at sed", "col2": "malesuada aliquam sit", "col3": 26, "isEnabled": false }, { "col1": "donec donec sed", "col2": "dui tempor nunc", "col3": 38, "isEnabled": false }, { "col1": "sed vitae fringilla", "col2": "vestibulum pretium dolor", "col3": 17, "isEnabled": false } ], selectedIndices : [], }); this.getView().setModel(oModel); }, toggleEditableFields : function(oEvent) { var aIndices = oEvent.getSource().getSelectedIndices(), myData = this.getView().getModel().getProperty("/data"), that = this; $.each(myData,function(i, v){ that.getView().getModel().setProperty("/data/" + v + "/isEnabled", false); }); $.each(aIndices,function(i, v){ that.getView().getModel().setProperty("/data/" + v + "/isEnabled", true); }); } }); sap.ui.xmlview("main", { viewContent: jQuery("#view1").html() }) .placeAt("uiArea"); 
 <script id="sap-ui-bootstrap" src="https://sapui5.hana.ondemand.com/resources/sap-ui-core.js" data-sap-ui-theme="sap_bluecrystal" data-sap-ui-xx-bindingSyntax="complex" data-sap-ui-libs="sap.m"></script> <div id="uiArea"></div> <script id="view1" type="ui5/xmlview"> <mvc:View controllerName="view1.initial" xmlns="sap.ui.commons" xmlns:l="sap.ui.commons.layout" xmlns:t="sap.ui.table" xmlns:core="sap.ui.core" xmlns:mvc="sap.ui.core.mvc" > <t:Table id="tbl" rows="{/data}" selectionMode="MultiToggle" navigationMode="Paginator" rowSelectionChange="toggleEditableFields" visibleRowCount="{/data/length}"> <t:columns> <t:Column> <t:label><Label text="col1" /></t:label> <t:template><TextField value="{col1}" editable="{isEnabled}" /></t:template> </t:Column> <t:Column> <t:label><Label text="col2" /></t:label> <t:template><TextField value="{col2}" editable="{isEnabled}" /></t:template> </t:Column> </t:columns> </t:Table> </mvc:View> </script> 

Note: I used XMLViews because I found these significantly easier to write, but the same applies for Javascript views 注意:我使用XMLViews因为我发现这些更容易编写,但同样适用于Javascript视图

As the Table does only create the Controls for visible Rows i would not recommend modify the Controls of the Rows directly. 由于Table仅为可见行创建控件,因此我不建议直接修改行的控件。 If you select Rows that are not visible (scrolled outside of view) you cannot modify their Controls because the do not exist. 如果选择不可见的行(在视图外滚动),则无法修改其控件,因为它们不存在。

Instead i would recommend to add an attribute to the model objects of the rows (only on the client in your onEditRow method) and bind editable to it. 相反,我建议将属性添加到行的模型对象(仅在onEditRow方法的客户端上)并将可编辑绑定到它。 Use expression-binding to force the value to false if its not defined in the model: 如果未在模型中定义,则使用表达式绑定将值强制为false

<TextField id="SatzNeu" editable="{= !!${jsonmodel>editable}}" value="{jsonmodel>SatzNeu}" />

The indices that Table.getSelectedIndices() returns can be used to get the binding contexts of the rows and with them the paths of the selected rows in the model ( jsbin ): Table.getSelectedIndices()返回的索引可用于获取行的绑定上下文,并使用它们获取模型中所选行的路径( jsbin ):

var oTable1 = this.byId("table1");
var haveToChange = oTable1.getSelectedIndices();
haveToChange.forEach(function(index){
  var context = oTable1.getContextByIndex(index);
  var path = context.getPath();
  context.getModel().setProperty(path+"/editable", true);
});  

But if i get it right, you want to make deselected rows non-editable too, so you have to iterate over all items to disable/enable where appropriate ( jsbin ): 但是如果我做对了,你想让取消选择的行也不可编辑,所以你必须迭代所有项目以禁用/启用适当的地方( jsbin ):

var oTable1 = this.byId("table1");
var haveToChange = oTable1.getSelectedIndices();
var model = oTable1.getModel("jsonmodel");
var length = model.getProperty("/DLSet/results").length;
for (var i = 0; i<length; i++){
  var path = "/DLSet/results/"+i+"/editable";
  var editable = haveToChange.indexOf(i) >= 0;
  if (model.getProperty(path) != editable) model.setProperty(path, editable);
}

You can use the first approach if you want to discard the edit button and use the rowSelectionChange event to update editable/noneditable as the event gives you both: the selected and the deselected rows. 如果要放弃编辑按钮并使用rowSelectionChange事件更新可编辑/不可编辑事件,则可以使用第一种方法,因为事件会同时为您提供:选定行和取消选择的行。

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

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