如何在制表器中向嵌套树数据动态添加行?

编程入门 行业动态 更新时间:2024-10-10 10:26:31
本文介绍了如何在制表器中向嵌套树数据动态添加行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

对于我的项目,我需要根据用户提交的表单数据向数据树中的父行添加新的子行.我无法在文档中找到如何执行此操作的示例.这可以使用 addRow({...}) 函数吗?我将如何声明要添加子行的父级?或者我是否需要构建一个自定义函数,将新行插入表 JSON 对象并重绘表?

For my project, I need to add new child rows to parent rows in a datatree based on user submitted form data. I wasn't able to find an example of how to do this in the documentation. Is this possible using the addRow({...}) function? How would I declare which parent to add the child row? Or would I need to build out a custom function which inserts the new row into the table JSON object and redraw the table?

感谢您的帮助!

推荐答案

我使用的解决方案是将新行对象添加到父行的 _children 数组的副本,然后向父行发送更新.为此,您需要找到父行,获取其数据(将包括子行对象的 _children 数组),将新行数据添加到 _children,并更新数据表中的父行数据.

The solution I used is to add new row objects to a copy of the _children array of the parent row and then send an update to the parent row. To do this, you need to find the parent row, get it's data (which will include the _children array of child row objects), add the new row of data to _children, and update the parent row data in the data table.

$("#add-child-row").click(function(){
    //Get values for child row form fields
    var childFields = $("#child-form").serializeArray().reduce(function(obj, item) {
        obj[item.name] = item.value;
        return obj;
    }, {});

    var newRow = {
        name: childFields.name,
        location: childFields.location,
        gender: childFields.gender,
        col: childFields.color,
        dob: childFields.dob,
    };

    //Find row to add child
    //searchRows() returns array
    //In my case, I am only expecting one matching row so use index 0
    var parentRow = table.searchRows("name","=","Oli Bob");

    //Get data for the parent row so we can update it's _children array
    var tempParentRowData = parentRow[0].getData();

    //Add new row to children array
    tempParentRowData._children.push(newRow);

    //Update data table row with new children array
    parentRow[0].update({_children:tempParentRowData._children});
});

如果您希望有大量子行,我不知道这会有多好.如果上述解决方案或更好的解决方案有任何缺陷,我很乐意看到.

I don't know how well this would work if you are expecting to have a huge number of children rows. If there is anything flawed with the above solution or a better solution out there, I would love to see it.

这篇关于如何在制表器中向嵌套树数据动态添加行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 10:44:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1408544.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   器中   动态   数据   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!