简体   繁体   English

如何通过Javascript函数正确传递表数据

[英]How to properly pass table data through Javascript function

I am using django to set up a basic web app. 我正在使用django设置基本的Web应用程序。 I have a Handsontable, which using a form, allows users to update the table, press a SAVE button and save the changes to a csv file. 我有一个Handsontable,它使用表格允许用户更新表,按SAVE按钮并将更改保存到csv文件。 My problem is that I cannot figure out how to pass the data properly. 我的问题是我无法弄清楚如何正确传递数据。

My approach is that the afterChange function should, when the table is updated, pass the data into the table_data_dump div in order for this to be used in python code pressing the SAVE button executes. 我的方法是,在更新表时,afterChange函数应将数据传递到table_data_dump div中,以便在执行SAVE按钮的python代码中使用该数据。

However it doesnt seem to be doing this, and I can't figure out the proper usage of the data variable. 但是,它似乎没有这样做,并且我无法弄清楚数据变量的正确用法。 I'm new to JS so getting confused pretty easily. 我是JS的新手,所以很容易感到困惑。

Full JS below: 完整的JS如下:

    <script type="text/javascript">

        $.ajaxSetup ({
            // Disable caching of AJAX responses
            cache: false
        });

        var data = [['', '', '', ''], ['', '', '', ''], ['', '', '', ''],
                    ['', '', '', ''], ['', '', '', ''], ['', '', '', ''],
                    ['', '', '', ''], ['', '', '', ''], ['', '', '', '']];

        var dataArray = {};

            dataArray['bnb'] = '/static/data/oim-oimom/lookup_bnb.csv'

            dataArray['testsplit'] = '/static/data/oim-oimom/lookup_testsplit.csv'

            dataArray['test two'] = '/static/data/oim-oimom/lookup_test-two.csv'


        var afterChange = function() {
            console.log(JSON.stringify(data));
            document.getElementById('table_data_dump').innerHTML = JSON.stringify(data)
        };

        var createTable = function(data) {

            var container = document.getElementById('lookup');
            var hot = new Handsontable(container, {
                data: data,
                afterChange: afterChange,
                minSpareRows: 1,
                rowHeaders: true,
                colHeaders: ['col1','col2','col3', 'Assignment'],
                columns: [
                    {readOnly: true},
                    {readOnly: true},
                    {readOnly: true},
                    {}
                ],
                colWidths: [335, 335, 335, 335]
                })};

        createTable(data);

        var SelectBoxChange = function() {
            var e = document.getElementById("split_name_select");
            var split_select = e.options[e.selectedIndex].text;
            $.get(dataArray[split_select], function(data) {
                document.getElementById('lookup').innerHTML='';
                data = $.csv.toArrays(data);
                createTable(data);

            });

        };

    </script>

I've figured this out, so just in case it is of use to anyone else... 我已经弄清楚了,所以以防万一它对其他人有用...

I should have been using the getData() method of the Handsontable here, however it was giving me an undefined error for hot.getData() because of the scope of my variables. 我应该在这里使用Handsontable的getData()方法,但是由于变量的范围,它给了hot.getData()一个未定义的错误。 I changed these to global and then could access the data. 我将它们更改为全局,然后可以访问数据。

Further to this afterChange needs to be set using the updateSettings() method after the table is created. 此外,在创建表之后,需要使用updateSettings()方法设置afterChange This is to allow the container to be defined, to then be referenced in the afterChange function before being assigned. 这是为了允许定义容器,然后在分配afterChange之前在afterChange函数中对其进行引用。

Thanks 谢谢

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

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