Laravel Blade - Append view to table tbody - php

I'm trying to append rows to a table but it don't pass <tr> and <td>, it's wired. Pass all without this two tags.
My jquery code is that:
$.get('/remove-day-travel/'+id, function(data)
{
$('#travelTable tbody').html('');
$('#travelTable tbody').append(data);
addDayTravel();
removeDayTravel();
});
My controller:
public function removeDayTravel($id)
{
$intervention = DB::table('interventionDays')->select('interventionId')->where('id', $id)->first();
//DB::table('interventionDays')->where('id', $id)->delete();
$travelHours = DB::table('interventionDays')
->where('interventionId', $intervention->interventionId)
->orderBy('day', 'ASC')
->get();
return view('current-dayTravel', compact('travelHours'));
}
And my view is that:
#if(count($travelHours) > 0)
<?php $count = 1; ?>
#foreach ($travelHours as $key => $value)
<input type="hidden" name="day-travel-id[{{$count}}]" value="{{$value->id}}">
<input type="hidden" name="day-travel-remove[{{$count}}]" value="0">
<tr>
<td><label>Dia {{$count}}:</label></td>
<td><label>Ida</label></td>
<td class="#if ($errors->has('hour-day-1[' . $count . ']')) has-error #endif">
{!! Form::text('hour-day-1[' . $count . ']', $value->morningStart , ['class' => 'form-control travelGoStartHour', 'placeholder'=> '']) !!}
</td>
<td class="#if ($errors->has('hour-day-2[{{$count}}]')) has-error #endif">
{!! Form::text('hour-day-2[' . $count. ']', $value->morningEnd, ['class' => 'form-control travelGoEndHour', 'placeholder'=> '']) !!}
</td>
<td>
<label>Volta</label>
</td>
<td class="#if ($errors->has('hour-day-3[' . $count . ']')) has-error #endif">
{!! Form::text('hour-day-3[' . $count . ']', $value->afternoonStart, ['class' => 'form-control travelBackStartHour', 'placeholder'=> '']) !!}
</td>
<td class="#if ($errors->has('hour-day-4[' . $count . ']')) has-error #endif">
{!! Form::text('hour-day-4[' . $count . ']', $value->afternoonEnd, ['class' => 'form-control travelBackEndHour', 'placeholder'=> '']) !!}
</td>
<td>
{!! Form::checkbox('dinner[' . $count . ']', 0,$value->dinner) !!}
</td>
<td>
{!! Form::checkbox('stay[' . $count . ']', 0,$value->stay) !!}
</td>
<td>
#if($key == count($travelHours) -1)
<i class="icon-plus-circled add-new-day-travel" title="Adicionar Novo dia" data-id="1"></i>
#endif
</td>
<td>
#if($key == count($travelHours) -1)
<i class="icon-cancel remove-day-travel" title="Remover dia" data-id="{{$count}}" data-record="{{$value->id}}"></i>
#endif
</td>
</tr>
<?php $count += 1; ?>
#endforeach
#endif
But returns me this:
I don't understand why? What I'm doing wrong?
Thank you

$('#travelTable tbody').html(data);
Use the above in your JS instead of what you currently have

When you render a tr or th or even td tag outside a tbody or thead or tfooter html ignores them and replace them by a empty space because those tags can only be placed in between opening and closing table tags.
Browsers ignore the <tbody>, <tr>, and <td> tags and the corresponding end tags. This has not been specified in HTML specifications, since they do not define the parsing of syntactically erroneous documents. In the HTML5 draft, however, there is a description that defines the browser behavior in a manner that corresponds to the what browsers actually do: Tree construction.
This means that you cannot write an HTML document that contains, say, a element outside a table element. The HTML parser in a browser simply does not construct such document trees. (You can, however, construct such a document dynamically with client-side scription.)
See previous answer here:
How do browsers analyze <tr> <td> without <table>?

Related

Data doesn't display

Need some help here, I'm using Laravel app, my problem is my data wont display on the table. I tried some ways to display my data but it doesn't work.
Here's my code.
<tbody>
#if (count($expenses) > 0)
#foreach ($expenses as $expense)
<tr data-entry-id="{{ $expense->id }}">
<td field-key='expense_category'>{{ $expense->expense_category->name or '' }}</td>
<td field-key='entry_date'>{{ $expense->entry_date }}</td>
<td field-key='amount'>{{ $expense->amount }}</td>
<td field-key='created_by'>{{ $expense->created_by->name or '' }}</td>
<td>
#can('view')
#lang('quickadmin.qa_view')
#endcan
#can('edit')
#lang('quickadmin.qa_edit')
#endcan
#can('delete')
{!! Form::open(array(
'style' => 'display: inline-block;',
'method' => 'DELETE',
'onsubmit' => "return confirm('".trans("quickadmin.qa_are_you_sure")."');",
'route' => ['expenses.destroy', $expense->id])) !!}
{!! Form::submit(trans('quickadmin.qa_delete'), array('class' => 'btn btn-xs btn-danger')) !!}
{!! Form::close() !!}
#endcan
</td>
</tr>
#endforeach
#else
<tr>
<td colspan="9">#lang('quickadmin.qa_no_entries_in_table')</td>
</tr>
#endif
</tbody>
here is my expenseController.
but i have never touched this code, i always work on the table form.
class ExpensesController extends Controller
{
/**
* Display a listing of Expense.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
if (! Gate::allows('expense_access')) {
return abort(401);
}
if ($filterBy = Input::get('filter')) {
if ($filterBy == 'all') {
Session::put('Expense.filter', 'all');
} elseif ($filterBy == 'my') {
Session::put('Expense.filter', 'my');
}
}
$expenses = Expense::all();
return view('admin.expenses.index', compact('expenses'));
}
}
try to find are you getting data from DB or not ....
$expenses = Expense::all();
dd($expenses);
It will show your data collection.
As I Think you are not getting any data from here.

How to pass values to vue data from laravel blade?

I am using Vue.js with Axios to post my data. But Axios returns NULL from my Laravel Controller. Input text is empty when using v-model (update form - blade laravel).
Pretty new to this awesome framework. I need to make an AJAX call for a text input in an update form (blade laravel), however in the form the input is blank and I’m having trouble to figure out if I’m missing something.
Question:
How do I send the input value from the blade template when the form is loaded to the Vue data instance
Blade:
{!! Form::open(['url' => '','class'=>'form-horizontal', 'id'=>'smv-cost-form','enctype'=>'multipart/form-data']) !!}
{!! Form::hidden('trans_id_d','36', ['class' => 'custom-input-bom','readonly'=>'readonly']) !!}
<tr id="smvCostAdd">
<td colspan="2">
{!! Form::text('knittingCost','',['class'=>'input-custom form-control','id'=>'knittingCost','v-model'=>'smvCostModel.knittingCost']) !!}
<p class="error" v-if="knittingCost_error">#{{knittingCost_error}}</p>
</td>
<td colspan="2">
{!! Form::text('linkingCost','',['class'=>'input-custom form-control','id'=>'linkingCost','v-model'=>'smvCostModel.linkingCost']) !!}
<p class="error" v-if="linkingCost_error">#{{linkingCost_error}}</p>
</td>
<td colspan="2">
{!! Form::text('tremingCost','',['class'=>'input-custom form-control','id'=>'tremingCost','v-model'=>'smvCostModel.tremingCost']) !!}
<p class="error" v-if="tremingCost_error">#{{tremingCost_error}}</p>
</td>
<td colspan="2">
{!! Form::text('mendingCost','',['class'=>'input-custom form-control','id'=>'mendingCost','v-model'=>'smvCostModel.mendingCost']) !!}
<p class="error" v-if="mendingCost_error">#{{mendingCost_error}}</p>
</td>
<td colspan="2">
{!! Form::text('washCost','',['class'=>'input-custom form-control','id'=>'washCost','v-model'=>'smvCostModel.washCost']) !!}
<p class="error" v-if="washCost_error">#{{washCost_error}}</p>
</td>
<td colspan="2">
{!! Form::text('pcqCost','',['class'=>'input-custom form-control','id'=>'pcqCost','v-model'=>'smvCostModel.pcqCost']) !!}
<p class="error" v-if="pcqCost_error">#{{pcqCost_error}}</p>
</td>
<td>
{!! Form::text('ironCost','',['class'=>'input-custom form-control','id'=>'ironCost','v-model'=>'smvCostModel.ironCost']) !!}
<p class="error" v-if="ironCost_error">#{{ironCost_error}}</p>
</td>
<td>
{!! Form::text('sewingCost','',['class'=>'input-custom form-control','id'=>'sewingCost','v-model'=>'smvCostModel.sewingCost']) !!}
<p class="error" v-if="sewingCost_error">#{{sewingCost_error}}</p>
</td>
<td>
{!! Form::text('packingCost','',['class'=>'input-custom form-control','id'=>'packingCost','v-model'=>'smvCostModel.packingCost']) !!}
<p class="error" v-if="packingCost_error">#{{packingCost_error}</p>
</td>
<td></td>
<td>{!! Form::button('Save', ['class' => 'btn btn-primary', 'v-on:click.prevent'=>'saveSMVcost();'] ) !!}</td>
</tr>
{!! Form::close() !!}
JS:
saveSMVcost: function () {
/*var request = new XMLHttpRequest();*/
var form = document.querySelectorAll("#smv-cost-form");
var formData = new FormData(form);
var base_url = window.location.origin;
var page_url = base_url + '/bca/smv-cost-action';
$('#ajax-call-effect').show();
console.log(formData.knittingCost);
alert(formData.knittingCost);
axios.post(page_url, formData).then(
function (response) {
$('#ajax-call-effect').hide();
if (response.data.errors) {
response.data.errors.knittingCost ? app.knittingCost_error = response.data.errors.knittingCost[0] : app.knittingCost_error = '';
response.data.errors.linkingCost ? app.linkingCost_error = response.data.errors.linkingCost[0] : app.linkingCost_error = '';
response.data.errors.tremingCost ? app.tremingCost_error = response.data.errors.tremingCost[0] : app.tremingCost_error = '';
response.data.errors.mendingCost ? app.mendingCost_error = response.data.errors.mendingCost[0] : app.mendingCost_error = '';
response.data.errors.washCost ? app.washCost_error = response.data.errors.washCost[0] : app.washCost_error = '';
response.data.errors.pcqCost ? app.pcqCost_error = response.data.errors.pcqCost[0] : app.pcqCost_error = '';
response.data.errors.ironCost ? app.ironCost_error = response.data.errors.ironCost[0] : app.ironCost_error = '';
response.data.errors.sewingCost ? app.sewingCost_error = response.data.errors.sewingCost[0] : app.sewingCost_error = '';
response.data.errors.packingCost ? app.packingCost_error = response.data.errors.packingCost[0] : app.packingCost_error = '';
}
else {
/* $('#knittingCost').val('');
$('#linkingCost').val('');
$('#tremingCost').val('');
$('#mendingCost').val('');
$('#washCost').val('');
$('#pcqCost').val('');
$('#ironCost').val('');
$('#sewingCost').val('');
$('#packingCost').val('');*/
app.knittingCost_error = '';
app.linkingCost_error = '';
app.tremingCost_error = '';
app.mendingCost_error = '';
app.washCost_error = '';
app.pcqCost_error = '';
app.ironCost_error = '';
app.sewingCost_error = '';
app.packingCost_error = '';
app.showNotification('top', 'center', response.data.status, response.data.message);
}
})
}
There are a few ways, but one would be to json_encode the object or array, as shown below. I ususally set a global object and then grab that in my Javascript, but you could technically set this value directly to a JS variable. Use #php and #endphp if you want more blade-like syntax.
<!-- Global App Object -->
<script>
window.App = <?php echo json_encode(
'isAdmin' => user()->isAdmin(),
'credentials' => user()->softphone()->credentials(),
'forwardToCellphone' => user()->forward_to_cellphone,
'status' => (new App\Http\Controllers\Users\UserStatusController)->details(),
); ?>;
</script>

Laravel Parameter passing error from controller to view

I want to pass the parameter $questions to view, but it gives the following error:
ErrorException (E_ERROR)
Undefined variable: questions (View: C:\Users\Krishan\Documents\GitHub\GroupProject\lcurve\resources\views\quizz\questions\index.blade.php)
This is my controller index function part:
public function index()
{
$questions = Question::all();
return view('quizz/questions.index', compact('questions'));
}
This is a part Of my view:
<tbody>
#if (count($questions_options) > 0)
#foreach ($questions_options as $questions_option)
<tr data-entry-id="{{ $questions_option->id }}">
<td></td>
<td>{{ $questions_option->question->question_text or '' }}</td>
<td>{{ $questions_option->option }}</td>
<td>{{ $questions_option->correct == 1 ? 'Yes' : 'No' }}</td>
<td>
View-->
<!--Edit-->
{!! Form::open(array(
'style' => 'display: inline-block;',
'method' => 'DELETE',
'onsubmit' => "return confirm('".trans("quickadmin.are_you_sure")."');",
'route' => ['questions_options.destroy', $questions_option->id])) !!}
{!! Form::submit(trans('quickadmin.delete'), array('class' => 'btn btn-xs btn-danger')) !!}
{!! Form::close() !!}
</td>
</tr>
#endforeach
#else
<tr>
<td colspan="5">no_entries_in_table</td>
</tr>
#endif
</tbody>
enter image description here
where is questions_options coming from? You are passing questions. So your for loop should be
#if (count($questions) > 0)
#foreach ($questions as $question)
//rest of your code
#endforeach
#endif
and your return view part can be return view(quizz.questions.index, compact('questions'))
Firstly, the error message you have mention should be shown. The error message should be:
Undefined variable: questions_options (View:C:\Users\Krishan\Do........
Because you are passing questions to view but you are accessing question_options in view. So, it should say question_options in undefined in view.
Besides, do you know you can avoid this count check? You can use laravel's forelse tag here a below:
#forelse($questions as $question)
//Your table goes here
#empty
<tr>
<td colspan="5">no_entries_in_table</td>
</tr>
#endforelse

Blade template and radio buttons - select first in foreach loop

I have the following Blade template entry, which creates (as part of a table row) a column of radio buttons.
I want to have only the first radio genereated selected, and I want to do this via the PHP, no js post page load.
How do I check if this is the "first" entry in my collection and thus place the string checked as an attribute in the HTML? My latest code permutation is below, but it does not work.
#foreach ($organisationsOwned as $organisationOwned)
<tr class="organisations-table">
<td><input type="radio" name="organisations" id="{!! $organisationOwned->id !!}" #if ($organisationsOwned{0}) {!! "checked" !!} #endif></td>
<td>{!! $organisationOwned->org_title !!}</td>
<td>{!! $organisationOwned->plan_title !!}</td>
<td style="text-align: center;">{!! currency_format($organisationOwned->gst_amount) !!}</td>
<td style="text-align: right;">{!! $organisationOwned->subscription_ends_at !!}</td>
</tr>
#endforeach
More precisely, here's the line for the input:
<input type="radio" name="organisations" id="{!! $organisationOwned->id !!}" #if ($organisationsOwned{0}) {!! "checked" !!} #endif>
I have tried variations such as:
{{ head($organisationsOwned) ? checked : '' }}>
and
{{ $organisationsOwned{0} ? checked : '' }}>
and even this suggestion shown as a working model of my question:
#if ($organisationOwned == reset($organisationsOwned)) checked #endif
Thanks and happy new year!
If you did not specify keys for the elements of $organisationsOwned you can use the element's index to determine if it's the first one:
#foreach($organisationsOwned as $index => $organisationOwned)
...
<td><input type="radio" name="organisations" id="{!! $organisationOwned->id !!}" #if (!$index) {!! "checked" !!} #endif></td>
...
#endforeach

custom foreach with respective title

I have the loop here see the image for reference..
I have the column called Supplier Name
For example there are two row at the same supplier name
so i need the one heading
my expected ouptut is
ABS Agencies
Supplier name - here should be the heading
ABS Agencies - heading
ABS Agencies
ABS Agencies
mycode here
#foreach($prsdkgrids as $prsdkgrid)
<?php if($prsdkgrid->SupplierName == $prsdkgrid->SupplierName){ $SupplierName[] = $prsdkgrid->SupplierName; } ?>
#endforeach
#foreach($prsdkgrids as $prsdkgrid)
<?php if(count($SupplierName) == $SupplierName[$f]) { }else{ echo $f."<tr><td><h3 style='font-size: 15px;'>$prsdkgrid->SupplierName</h3></td></tr>"; } ?>
<tr class="psr_table1_tr" id="PSupdetID<?php echo $prsdkgrid->PSupdetID; ?>" <?php if($prsdkgrid->ApprovalStatus == 1){ echo 'style="background: #d9edf7;"';} ?> >
<td> {{ Form::label('', $prsdkgrid->ArticleNo) }} </td>
<td> {{ Form::label('', $prsdkgrid->Size) }} </td>
<td> {{ Form::label('', $prsdkgrid->SpecialDesc) }} </td>
<td> {{ Form::label('', $prsdkgrid->QtyExp) }} </td>
<td> {{ Form::label('', $prsdkgrid->Coloring) }} </td>
<td> {{ Form::label('', $prsdkgrid->Packaging) }} </td>
<td> {{ Form::label('', $prsdkgrid->Packing) }} </td>
<td> {{ Form::label('', $prsdkgrid->TargetPrice) }} </td>
<td> {{ Form::label('', $prsdkgrid->SupPrice) }} </td>
<td> {{ Form::label('', $prsdkgrid->SupplierName) }}</td>
<td> {{ Form::label('', $prsdkgrid->SupComments) }} </td>
<td> <?php if($prsdkgrid->ApprovalStatus == 0){ ?><div align="center" class="floatleft approvedk" id="ApprvedbyResp<?php echo $prsdkgrid->PSupdetID; ?>">
<li><a class="prs_add_link" href="#"><input class="save_btn Approve" type="button" onclick="ApprvedbyResp(<?php echo $prsdkgrid->PSupdetID; ?>)" value="Approve"></a></li></div><?php } ?> </td>
</tr> <?php $f++; ?>
#endforeach
You can store the supplier_name in a variable and in the loop you can just compare the variable and supplier name. A simple approach is like this since you're using foreach()
$tmp_supplier_name = '';
foreach($prsdkgrids as $prsdkgrid){
echo ($tmp_supplier_name != $prsdkgrid->supplier_name) ? '<tr>SUppliername</tr>' : ''; //compare
$tmp_supplier_name = $prsdkgrid->supplier_name; //store the suppliername that was compared
}
Or another approach is try to manipulate your array and it should be something like this , much readable.
Array(
['supplierA'] =>
Array(
[0] => array(.....)
[1] => array(.....)
),
['supplierB'] =>
Array(
[0] => array(.....)
[1] => array(.....)
)
)
Recreate the array example:
$new_array = [];
$tmp_supplier_name = '';
foreach($prsdkgrids as $prsdkgrid){
if($tmp_supplier_name != $prsdkgrid->supplier_name){
$tmp_supplier_name = $prsdkgrid->supplier_name;
}else{
$new_array[$prsdkgrid->supplier_name]['opt1'] = $prsdkgrid->opt1;
$new_array[$prsdkgrid->supplier_name]['opt2'] = $prsdkgrid->opt2;
//and so on
}
}

Categories