Handsontable 提供意想不到的列数

Handsontable giving unexpected number of columns

我已经实施了 handsontable 来从用户那里获取数据。 这是我的脚本

<script type="text/javascript">
    $(document).ready(function() {
        $('#quiz_questions').handsontable({
            rowHeaders: true,
            colHeaders: ['Question', 'Option 1', 'Option 2', 'Option 3', 'Option 4', 'Answer', 'Marks'],
            columns: [
                {
                  type: 'text',
                  allowEmpty: false
                },
                {
                  type: 'text',
                  allowEmpty: false
                },
                {
                  type: 'text',
                  allowEmpty: false
                },
                {
                  type: 'text',
                  allowEmpty: true
                },
                {
                  type: 'text',
                  allowEmpty: true
                },
                {
                  type: 'dropdown',
                  source: ['Option 1', 'Option 2', 'Option 3', 'Option 4'],
                  allowEmpty: false
                },
                {
                  type: 'numeric',
                  allowEmpty: false
                }
            ],
            stretchH: 'all',
            minSpareRows: 0,
            minSpareColumns: 0,
            minRows : 25
        });

        var hotInstance = $("#quiz_questions").handsontable('getInstance');

        $('#btnSave').click(function(e){
            e.preventDefault();
            $('#btnSave').prop("disabled", true);
            //alert('btnclicked');

            var dynFrm = $('<form>', {
                'action': '{{ action('QuizQuestionController@storeBulk') }}',
                'method': 'POST'
            }).append($('<input>', {
                'type': 'hidden',
                'name': '_token',
                'value': '{{ csrf_token() }}'
            })).append($('<input>', {
                'type': 'hidden',
                'name': 'quiz_id',
                'value': '{{ $quiz->quiz_id }}'
            })).append($('<input>', {
                'type': 'hidden',
                'name': 'data',
                'value': JSON.stringify(hotInstance.getData())
            }));
            dynFrm.appendTo(document.body).submit();
        });

    });

</script>

QuizQuestionControllerstoreBulk()函数处理数据。

public function storeBulk()
    {
        // get the quiz model
        $quiz = Quiz::findOrFail(Input::get('quiz_id'));

        // get the data
        $data = Input::get('data');
        $jData = json_decode($data);

        //process the recevied data
        foreach($jData as $row) {
            $quizQuestion = new QuizQuestion();

            $quizQuestion->quiz_id = $quiz->quiz_id;
            $quizQuestion->question_no = $cnt;
            $quizQuestion->question_text = trim($row[0]) ? : null;
            $quizQuestion->options = $this->processOptions([
                                        trim($row[1]),
                                        trim($row[2]),
                                        trim($row[3]),
                                        trim($row[4])
                                        ]);
            $quizQuestion->answer = $this->processAnswer($row[5]);
            $quizQuestion->marks = trim($row[6]) ? : null;

            ...
    }

现在的问题是,对于在填充数据时 handsontable 中留空的行,我应该为这些行获取 data 作为 [null,null,null,null,null,null,null].但这种情况并非如此。对于某些行,我得到 [null,null,null,null,null] (只有 5 个值)。因此我得到一个 ErrorExceptionUndefined offset: 5.

我注意到这种情况只发生在前 5 行。可能是什么问题?

您是否尝试过使用 hotInstance.getSourceData() 而不是 hotInstance.getData()?此方法的功能在其最新版本中发生了变化,这给其他人带来了类似的问题。

指出了问题所在。

I have noticed this happens for first 5 rows only. What could be the problem?

handsontable 中有一个 startRows 属性,默认为 5。因此前 5 行的问题。我将 属性 明确设置为

startRows: 0,

并且还修改了 storeBulk() 函数以忽略错误。

$quizQuestion->question_text = trim(@$row[0]) ? : null;
$quizQuestion->options = $this->processOptions([
                                   trim(@$row[2]),
                                   trim(@$row[3]),
                                   trim(@$row[4]),
                                   trim(@$row[5])
                                ]);
$quizQuestion->answer = $this->processAnswer(@$row[6]);
$quizQuestion->marks = trim(@$row[7]) ? : null;

现在一切正常。