-
Notifications
You must be signed in to change notification settings - Fork 920
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug] Fixes the visibility of buttons when using max/min on table inputs #198
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Not sure if this is worth a new PR, but i've also got a variation which allows a select2 if anybody wants the code its <!-- array input -->
<?php
$max = isset($field['max']) ? $field['max'] : -1;
$min = isset($field['min']) ? $field['min'] : -1;
$item_name = strtolower( isset($field['entity_singular']) && !empty($field['entity_singular']) ? $field['entity_singular'] : $field['label']);
$items = old($field['name']) ? (old($field['name'])) : (isset($field['value']) ? ($field['value']) : (isset($field['default']) ? ($field['default']) : '' ));
// make sure not matter the attribute casting
// the $items variable contains a properly defined JSON
if(is_array($items)) {
if (count($items)) {
$items = json_encode($items);
}
else
{
$items = '[]';
}
} elseif (is_string($items) && !is_array(json_decode($items))) {
$items = '[]';
}
$needsSelect2 = false;
?>
<div ng-app="backPackTableApp" ng-controller="tableController" @include('crud::inc.field_wrapper_attributes') >
<label>{!! $field['label'] !!}</label>
<input class="array-json" type="hidden" id="{{ $field['name'] }}" name="{{ $field['name'] }}">
<div class="array-container form-group">
<table class="table table-bordered table-striped m-b-0" ng-init="field = '#{{ $field['name'] }}'; items = {{ $items }}; max = {{$max}}; min = {{$min}}; maxErrorTitle = '{{trans('backpack::crud.table_cant_add', ['entity' => $item_name])}}'; maxErrorMessage = '{{trans('backpack::crud.table_max_reached', ['max' => $max])}}'">
<thead>
<tr>
@foreach( $field['columns'] as $prop )
<th style="font-weight: 600!important;">
@if( is_array($prop) )
{{ $prop['label'] }}
@else
{{ $prop }}
@endif
</th>
@endforeach
<th class="text-center" ng-if="max == -1 || max > 1"> {{-- <i class="fa fa-sort"></i> --}} </th>
<th class="text-center" ng-if="max == -1 || max > 1"> {{-- <i class="fa fa-trash"></i> --}} </th>
</tr>
</thead>
<tbody ui-sortable="sortableOptions" ng-model="items" class="table-striped">
<tr ng-repeat="item in items" class="array-row">
@foreach( $field['columns'] as $prop => $label)
<td>
@if( isset($label['options']) )
@php
$needsSelect2 = true
@endphp
<select class="form-control input-sm select2" ng-model="item.{{ $prop }}">
@foreach( $label['options'] as $option)
<option value="{{ $option->getKey() }}">{{ $option->{ $label['name'] } }}</option>
@endforeach
</select>
@else
<input class="form-control input-sm" type="text" ng-model="item.{{ $prop }}">
@endif
</td>
@endforeach
<td ng-if="max == -1 || max > 1">
<span class="btn btn-sm btn-default sort-handle"><span class="sr-only">sort item</span><i class="fa fa-sort" role="presentation" aria-hidden="true"></i></span>
</td>
<td ng-if="max == -1 || max > 1">
<button ng-hide="min > -1 && $index < min" class="btn btn-sm btn-default" type="button" ng-click="removeItem(item);"><span class="sr-only">delete item</span><i class="fa fa-trash" role="presentation" aria-hidden="true"></i></button>
</td>
</tr>
</tbody>
</table>
<div class="array-controls btn-group m-t-10">
<button ng-if="max == -1 || items.length < max" class="btn btn-sm btn-default" type="button" ng-click="addItem()"><i class="fa fa-plus"></i> Add {{ $item_name }}</button>
</div>
</div>
{{-- HINT --}}
@if (isset($field['hint']))
<p class="help-block">{!! $field['hint'] !!}</p>
@endif
</div>
{{-- ########################################## --}}
{{-- Extra CSS and JS for this particular field --}}
@if ( $needsSelect2 )
@push('crud_fields_styles')
<link href="{{ asset('vendor/backpack/select2/select2.css') }}" rel="stylesheet" type="text/css" />
<link href="{{ asset('vendor/backpack/select2/select2-bootstrap-dick.css') }}" rel="stylesheet" type="text/css" />
@endpush
@push('crud_fields_scripts')
<script src="{{ asset('vendor/backpack/select2/select2.js') }}"></script>
@endpush
@endif
{{-- If a field type is shown multiple times on a form, the CSS and JS will only be loaded once --}}
@if ($crud->checkIfFieldIsFirstOfItsType($field, $fields))
{{-- FIELD CSS - will be loaded in the after_styles section --}}
@push('crud_fields_styles')
{{-- @push('crud_fields_styles')
{{-- YOUR CSS HERE --}}
@if ( $needsSelect2 )
<link href="{{ asset('vendor/backpack/select2/select2.css') }}" rel="stylesheet" type="text/css" />
<link href="{{ asset('vendor/backpack/select2/select2-bootstrap-dick.css') }}" rel="stylesheet" type="text/css" />
@endif
@endpush
{{-- FIELD JS - will be loaded in the after_scripts section --}}
@push('crud_fields_scripts')
{{-- YOUR JS HERE --}}
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-sortable/0.14.3/sortable.min.js"></script>
<script>
window.angularApp = window.angularApp || angular.module('backPackTableApp', ['ui.sortable'], function($interpolateProvider){
$interpolateProvider.startSymbol('<%');
$interpolateProvider.endSymbol('%>');
});
window.angularApp.controller('tableController', function($scope){
$scope.sortableOptions = {
handle: '.sort-handle'
};
var select2Loaded = false;
$scope.addItem = function(){
if( $scope.max > -1 ){
if( $scope.items.length < $scope.max ){
var item = {};
$scope.items.push(item);
window.select2ize();
} else {
new PNotify({
title: $scope.maxErrorTitle,
text: $scope.maxErrorMessage,
type: 'error'
});
}
}
else {
var item = {};
$scope.items.push(item);
window.select2ize();
}
}
$scope.removeItem = function(item){
var index = $scope.items.indexOf(item);
$scope.items.splice(index, 1);
}
$scope.$watch('items', function(a, b){
if( $scope.min > -1 ){
while($scope.items.length < $scope.min){
$scope.addItem();
}
}
if( typeof $scope.items != 'undefined' && $scope.items.length ){
if( typeof $scope.field != 'undefined'){
if( typeof $scope.field == 'string' ){
$scope.field = $($scope.field);
}
$scope.field.val( angular.toJson($scope.items) );
}
}
}, true);
if( $scope.min > -1 ){
for(var i = 0; i < $scope.min; i++){
$scope.addItem();
}
}
});
angular.element(document).ready(function(){
window.select2ize = function(){
setTimeout(function(){
angular.element('.select2').each(function (i, obj) {
if ( !$(obj).data('select2') ) {
$(obj).select2();
}
});
}, 200);
};
angular.forEach(angular.element('[ng-app]'), function(ctrl){
var ctrlDom = angular.element(ctrl);
if( !ctrlDom.hasClass('ng-scope') ){
angular.bootstrap(ctrl, [ctrlDom.attr('ng-app')]);
}
window.select2ize();
});
})
</script>
@endpush
@endif
{{-- End of Extra CSS and JS --}}
{{-- ########################################## --}} then example usage would be $this->crud->addField([
'name' => 'tags_needed',
'label' => 'Tags needed',
'type' => 'table',
'entity_singular' => 'Tag',
'columns' => [
'tag' => [
'label' => 'Tag',
'options' => Tag::all(),
'name' => 'name'
],
'count' => 'Amount'
],
'max' => 2,
'min' => 1
]); |
This was referenced Apr 2, 2020
Closed
Closed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
No description provided.