Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在现有表中添加新行_Javascript_Model_Sapui5 - Fatal编程技术网

Javascript 在现有表中添加新行

Javascript 在现有表中添加新行,javascript,model,sapui5,Javascript,Model,Sapui5,我想在现有表中添加新行 我的代码: onInit:function(){ var-oModel=new-sap.ui.model.json.JSONModel(“model/toughts.json”); this.getView().setModel(oModel); var table=this.getView().byId(“tableid”); 表.bindItems(“/catalog/clothing/categories”,新sap.m.ColumnListItem({ 键入:“导

我想在现有表中添加新行

我的代码:

onInit:function(){
var-oModel=new-sap.ui.model.json.JSONModel(“model/toughts.json”);
this.getView().setModel(oModel);
var table=this.getView().byId(“tableid”);
表.bindItems(“/catalog/clothing/categories”,新sap.m.ColumnListItem({
键入:“导航”,
按下:函数(evt){},
单元格:[
新sap.m.Text({
文本:“{name}”
}),新sap.m.Text({
正文:“{amount}”
}),新sap.m.Text({
案文:“{货币}”
}),新sap.m.Text({
文本:“{size}”
})
]
}));
表2.setModel(oModel);
},
onPress:功能(oEvent){
var table=this.getView().byId(“tableid”);
var items=table.getSelectedItems();
对于(变量i=0;i

这是我的输出图像

现在正是我想要实现的

  • 我选择任何复选框
  • 然后我将按下
    Edit
    按钮
  • 现在按
    Edit
    按钮
    我想在选中的复选框行下面再添加一行。
  • 所以我可以做到这一点

    注意现在新行最后添加到表中

    用于在指定索引处插入项

    它有两个论点,即。要添加的项及其要添加的索引

    所以,首先获取要添加项的项的索引,然后使用该索引
    +1
    作为新项的索引


    是演示。

    如果使用数据绑定,则绝对不应添加新的表行控件,而是向模型节点添加新条目:

    编辑:更新我的答案:

    onPress : function(oEvent) {
        var oModel   = this.getView().getModel();                               // the model
        var aRows    = oModel.getProperty("/catalog/clothing/categories");      // array with all rows in the model
        var oThisObj = oEvent.getSource().getBindingContext().getObject();      // the current object
        var item     = { name: null, amount: null, currency: null, size: null } // an empty object
    
        var index = $.map(aRows, function(obj, index) {                         // get the index of the selected item in your array
            if(obj === oThisObj) {
                return index;
            }
        })
    
        aRows.splice(index, 0, item);                                           // add the item at the index
    
        oModel.setProperty("/catalog/clothing/categories", aRows);              // store the array back to the model
    }
    

    谢谢@Dopedev,但如果我选择所有三行并按下编辑按钮,那么新行将添加到每个选定行的下方..现在它只添加到索引0..那么我该怎么做呢?我累了,但没有得到…@quality你能分享演示吗..ThanksIt只是简单的Javascript真的。。。。查看我的最新答案