在bootstrap表bug中添加行

时间:2017-10-11 08:28:12

标签: jquery twitter-bootstrap forms laravel html-table

我需要在表格中帮助我的表格。 代码如下所示:

<div class="col-md-6">
    <table class="table">
        <tbody id="request_profile_language_table">  
            <tr>                    
                <td id="request_profile_languages">
                    @isset($requestProfile)
                    @foreach($requestProfile->languages as $index=>$requestProfileLanguage)
                    <select {{ (FALSE == $canEdit) ? 'readonly':'' }} class="form-control form-select" id="{{ 'request_profile_language_'.$index }}" name="{{ 'request_profile_language_'.$index }}" placeholder="Language">
                        @foreach($languages as $r=>$language)
                        <option {{ ($language->name == $requestProfileLanguage->language->name) ? 'selected':'' }} value="{{ $language->value }}" >{{ $language->name }}</option>
                        @endforeach
                    </select>
                    @endforeach
                    @else
                    <select {{ (FALSE == $canEdit) ? 'readonly':'' }} class="form-control form-select" id="request_profile_language_0" name="request_profile_language_0" placeholder="Language">
                        @foreach($languages as $r=>$language)
                        <option {{ ($language->name == 'English') ? 'selected':'' }} value="{{ $language->value }}" >{{ $language->name }}</option>
                        @endforeach
                    </select>
                    @endisset
                </td>
                <td id="request_profile_language_levels">
                    @isset($requestProfile)
                    @foreach($requestProfile->languages as $index=>$requestProfileLanguage)  
                    <select {{ (FALSE == $canEdit) ? 'readonly':'' }} class="form-control form-select" id="{{ 'request_profile_language_level_'.$index }}" name="{{ 'request_profile_language_level_'.$index }}" placeholder="Language level">
                        @foreach($languageLevels as $r=>$languageLevel)
                        <option {{ ($languageLevel->name == $requestProfileLanguage->languageLevel->name) ? 'selected':'' }} value="{{ $languageLevel->value }}" >{{ $languageLevel->name }}</option>
                        @endforeach
                    </select>
                    @endforeach
                    @else
                    <select {{ (FALSE == $canEdit) ? 'readonly':'' }} class="form-control form-select" id="request_profile_language_level_0" name="request_profile_language_level_0" placeholder="Language level">
                        @foreach($languageLevels as $r=>$languageLevel)
                        <option {{ ($languageLevel->name == 'Intermediate') ? 'selected':'' }} value="{{ $languageLevel->value }}" >{{ $languageLevel->name }}</option>
                        @endforeach
                    </select>
                    @endisset
                </td>      
                <td id="request_profile_language_remove">
                    @isset($requestProfile)
                    @foreach($requestProfile->languages as $index=>$requestProfileLanguage)  
                    @if(0 == $index)
                    <button class="glyphicon glyphicon-remove form-control" id="{{ 'request_profile_language_remove_'.$index }}" readonly/>
                    @else
                    <button class="glyphicon glyphicon-remove form-control" id="{{ 'request_profile_language_remove_'.$index }}" name="{{ 'request_profile_language_remove_'.$index }}" onclick="return deleteLanguage(this)"/>
                    @endif
                    @endforeach
                    @else
                    <button class="glyphicon glyphicon-remove form-control" readonly/>
                    @endisset  
                </td>
            </tr>
        </tbody>
    </table>
</div>    
<div class="col-md-3">
    <ul class="list-unstyled" id="button_list">
        <li>
            <button class="btn btn-success btn-small" id="add_new_language">Add New Language</button>              
        </li>
    </ul>
</div>            
</div>

我尝试在<select>之前和之后放置<td>,甚至我试图将所有内容放在2个@isset块中。似乎什么都没有用。 我添加新行的方法是这样的:

<script>
    function addNewLanguage() {
        var count = $('#request_profile_languages').children('tr').length;

        var select_language = $('#request_profile_languages')
                .children().first()
                .children().first()
                .clone()
                .attr('id', idRequestProfileLanguage.concat(count))
                .attr('name', idRequestProfileLanguage.concat(count));

        var select_language_level = $('#request_profile_language_levels')
                .children().first()
                .children().first()
                .clone()
                .attr('id', idRequestProfileLanguageLevel.concat(count))
                .attr('name', idRequestProfileLanguageLevel.concat(count));

        var remove_language = $('<button />')
                .addClass("glyphicon")
                .addClass("glyphicon-remove")
                .addClass("form-control")
                .prop('id', idRequestProfileLanguageRemove.concat(count))
                .prop('name', idRequestProfileLanguageRemove.concat(count))
                .on('click', function () {
                    select_language.remove();
                    select_language_level.remove();
                    remove_language.remove();

                    event.preventDefault();
                });
        $('#request_profile_language_table').append($('<tr>').append($('<td>').append(select_language), $('<td>').append(select_language_level), $('<td>').append(remove_language)));
    }
</script>

无论出于何种原因,当添加一行时,<option>设置为<td>,而不是像我希望的那样整个<select>,它最终看起来像{{ 3}}

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我找到了答案。该方法应如下所示:

<script>
function addNewLanguage() {
    var count = $('#request_profile_languages_table').children('tr').length;

    var select_language = $('#request_profile_languages')
            .clone()
            .attr('id', idRequestProfileLanguage.concat(count))
            .attr('name', idRequestProfileLanguage.concat(count));

    var select_language_level = $('#request_profile_language_levels')
            .clone()
            .attr('id', idRequestProfileLanguageLevel.concat(count))
            .attr('name', idRequestProfileLanguageLevel.concat(count));

    var remove_language = $('<button />')
            .addClass("glyphicon")
            .addClass("glyphicon-remove")
            .addClass("form-control")
            .prop('id', idRequestProfileLanguageRemove.concat(count))
            .prop('name', idRequestProfileLanguageRemove.concat(count))
            .on('click', function () {
                select_language.closest('tr').remove();

                event.preventDefault();
            });
    $('#request_profile_language_table').append($('<tr>').append($('<td>').append(select_language), $('<td>').append(select_language_level), $('<td>').append(remove_language)));
}

在此之后,它运作得很好。