Sorting datatable not working when setting rowspan - datatable

I am using jquery datatable.
Sorting in datatable is not working.
The problem here is:
initially if 2 columns with the same rowspan attribute are adjacent to each other it works fine, but if I split one column at the beginning 1 column at the end of the table it doesn't work anymore.
I am getting:
Uncaught TypeError: Cannot read properties of undefined (reading
'unique')
Please help me.Thanks!
like this it will work:
table working
it won't work:table notworking
here is my code
$(document).ready(function() {
dataTable();
});
function dataTable() {
var oTable = $("#tb__qsc").DataTable({
scrollX: false,
paging: false,
processing: false,
serverSide: false,
searching: false,
deferRender: true,
stateSave: false,
orderCellsTop: true,
language: {
url: datatableLanguge,
},
columnDefs: [{
searchable: false,
orderable: false,
targets: [],
}, ],
order: [],
// fixedHeader: {
// header: true,
// headerOffset: 1,
// },
pagingType: 'simple_numbers',
// bLengthChange: false,
paging: false
});
}
<table id="tb__qsc" class="display table__form tb__form--color2 w-full border-collapse border">
<thead class="text-white border-gray-300">
<tr>
<th rowspan="4"
class="text-center py-3 px-4 bg-white text-black font-semibold text-sm w-52 relative whitespace-nowrap">
店舗</th>
<th colspan="3"
class="text-center py-3 px-4 bg__color--red200 font-semibold text-sm whitespace-nowrap">
実績
</th>
<th colspan="8" class="text-center py-2 px-1 font-semibold text-sm whitespace-nowrap">
QSC項目</th>
<th rowspan="4"
class="text-center py-3 px-4 bg-white text-black font-semibold text-sm w-52 relative whitespace-nowrap">
実施日</th>
</tr>
<tr class="bg__color--red100 text-black">
<th rowspan="3"
class="text-center py-2 px-1 font-semibold text-sm relative whitespace-nowrap">得点合計
</th>
<th rowspan="3"
class="text-center py-2 px-1 font-semibold text-sm relative whitespace-nowrap">達成率
</th>
<th rowspan="3"
class="text-center py-2 px-1 font-semibold text-sm relative whitespace-nowrap">ランク
</th>
</tr>
<tr>
<th colspan="2" class="text-center py-2 px-1 font-normal text-sm whitespace-nowrap">店舗環境
</th>
<th colspan="2" class="text-center py-2 px-1 font-normal text-sm whitespace-nowrap">基本事項
</th>
<th colspan="2" class="text-center py-2 px-1 font-normal text-sm whitespace-nowrap">サービス
</th>
<th colspan="2" class="text-center py-2 px-1 font-normal text-sm whitespace-nowrap">厨房衛生
</th>
</tr>
<tr class="bg__color--gray800 text-black">
<th
class="text-center py-2 px-1 font-semibold text-sm min-w-max relative whitespace-nowrap">
得点</th>
<th
class="text-center py-2 px-1 font-semibold text-sm min-w-max relative whitespace-nowrap">
達成率</th>
<th
class="text-center py-2 px-1 font-semibold text-sm min-w-max relative whitespace-nowrap">
得点</th>
<th
class="text-center py-2 px-1 font-semibold text-sm min-w-max relative whitespace-nowrap">
達成率</th>
<th
class="text-center py-2 px-1 font-semibold text-sm min-w-max relative whitespace-nowrap">
得点</th>
<th
class="text-center py-2 px-1 font-semibold text-sm min-w-max relative whitespace-nowrap">
達成率</th>
<th
class="text-center py-2 px-1 font-semibold text-sm min-w-max relative whitespace-nowrap">
得点</th>
<th
class="text-center py-2 px-1 font-semibold text-sm min-w-max relative whitespace-nowrap">
達成率</th>
</tr>
</thead>
<tbody class="text-gray-700 text-center">
<tr>
<td class="text-center py-3 px-4">A店</td>
<td class="text-center py-3 px-4">60</td>
<td class="text-center py-3 px-4">58%</td>
<td class="text-center py-3 px-4">B</td>
<td class="text-center py-3 px-4">15</td>
<td class="text-center py-3 px-4">60%</td>
<td class="text-center py-3 px-4">15</td>
<td class="text-center py-3 px-4">55%</td>
<td class="text-center py-3 px-4">15</td>
<td class="text-center py-3 px-4">62%</td>
<td class="text-center py-3 px-4">15</td>
<td class="text-center py-3 px-4">53%</td>
<td class="text-center py-3 px-4">2021.01.14</td>
</tr>
</tbody>
</table>

Related

Trying to check if a database cell contains a certain string then change the badge color

So Iam using vuex and laravel for this project and i have had some difficulties with trying to change the badge depending on the displayed data in my dashboard. Currently my dashboard looks like this:
Now what i would like to happen is when an Error Message contains for instance the string 99012. That the badge color according to the Error message code. I havent been able to make the {"Result":[{"ERRORS":"99012: Weborder number already exists : 20211049"}]} in JSON format that is why its been displayed like this.
Currently my Controller looks like this:
class GencomController extends Controller
{
/**
* Display a listing of the resource.
*
*
*/
public function index(Request $request)
{
//
// $log = Log::get('RESPONSE');
// Get all the Response rows from database.
$data = Log::all();
// Convert data into json array
//$decoded = json_encode($data, true);
$decoded = json_decode($data);
return ($decoded);
}
This is how my store.js looks:
api_logs: {
loading: false,
data: [],
},
and here is my .vue component:
<div class="mt-8 flex flex-col">
<div class="-my-2 -mx-4 overflow-x-auto sm:-mx-6 lg:-mx-8">
<div class="inline-block py-2 align-middle">
<div class="overflow-auto relative shadow-sm ring-1 ring-black ring-opacity-5">
<table class="table-fixed divide-y divide-gray-300">
<thead class="bg-gray-50">
<tr>
<th scope="col" class="w-1/15 py-3.5 pl-4 pr-3 text-left text-sm font-semibold text-gray-900 sm:pl-6 lg:pl-8">ID</th>
<th scope="col" class="w-1/13 px-3 py-3.5 text-left text-sm font-semibold text-gray-900">Date & Time</th>
<th scope="col" class=" px-3 py-3.5 text-left text-sm font-semibold text-gray-900">Error Message</th>
<th scope="col" class=" px-3 py-3.5 text-left text-sm font-semibold text-gray-900">Status Badge</th>
<th scope="col" class=" px-3 py-3.5 text-left text-sm font-semibold text-gray-900">Edit</th>
<th scope="col" class=" relative py-3.5 pl-3 pr-4 sm:pr-6 lg:pr-8">
<span class="sr-only">Edit</span>
</th>
</tr>
</thead>
<tbody class="divide-y divide-gray-200 bg-white">
<tr v-for="log in logs" :key="log.APIGROUPNO">
<td class="w-1/15 py-4 pl-4 pr-3 text-sm font-medium text-gray-900 sm:pl-6 lg:pl-8">{{ log.APIGROUPNO }}</td>
<td class=" px-3 py-4 text-sm text-gray-500">{{ log.DATETIMEEXECUTED }}</td>
<!-- <td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500">{{ log.JSONBDY }}</td>-->
<td class="break-all px-3 py-4 text-sm text-gray-500">{{ log.RESPONSE }}</td>
<td class="break-all px-3 py-4 text-sm text-gray-500"><span class="bg-red-100 text-red-800 text-xs font-semibold mr-2 px-2.5 py-0.5 rounded dark:bg-red-200 dark:text-red-900">Error</span></td>
<td class="relative py-4 pl-3 pr-4 text-right text-sm font-medium sm:pr-6 lg:pr-8">
<a href="#" class="text-indigo-600 hover:text-indigo-900"
>Edit<span class="sr-only">, {{ log.APIGROUPNO }}</span></a
>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
And here is my Model:
class Log extends Model
{
use HasFactory;
protected $connection = 'gencom';
protected $table = 'GMAPILOG';
protected $primaryKey = 'APIGROUPNO';
public $timestamps = false;
protected $casts = ["Result"=>'array'];
protected $fillable = [
'APIGROUPNO',
'DATETIMEEXECUTED',
'PACKAGENAME',
'TOKEN',
'APINAME',
'PARAMETERLIST',
'JSONBDY',
'RESPONSE'
];
}
The json response i get returned from the db:
RESPONSE: "{\"Result\":[{\"ERRORS\":\"99012: Weborder number already exists : 20211049\"}]}"
So to make a quick summary, i would like the badge color to change according to the code gotten in the message. I have tried it for a while but i cant seem to figure it out as i do struggle a bit with vuex. I hope some could explain me their thought process.
Thanks in advance.

Getting stored Json query from database and extracting only the values i want

Currently iam working on a dashboard where i have to get stored JSON query's from the database and then select only select the keys i want to use. Iam working with laravel and Vuex.
Currently my controller looks like this.
public function index()
{
//
$log = Log::all();
return (json_encode($log));
}
At the moment this returns the table that i want and I'am able to select the column i want.
This is how my HTML looks
<tr v-for="log in logs" :key="log.APIGROUPNO">
<td class="whitespace-nowrap py-4 pl-4 pr-3 text-sm font-medium text-gray-900 sm:pl-6 lg:pl-8">{{ log.APIGROUPNO }}</td>
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500">{{ log.DATETIMEEXECUTED }}</td>
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500">{{ log.JSONBDY }}</td>
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500">{{ log.RESPONSE }}</td>
<td class="relative whitespace-nowrap py-4 pl-3 pr-4 text-right text-sm font-medium sm:pr-6 lg:pr-8">
<a href="#" class="text-indigo-600 hover:text-indigo-900"
>Edit<span class="sr-only">, {{ log.APIGROUPNO }}</span></a
>
</td>
</tr>
The following code gives me this result.
What i would like to get back is the following:
How would i be able to fix this, i have searched far and wide on the internet but cant seem to find a fitting solution. Any help and/or suggestions would be greatly appreciated.

Tailwind CSS table not applying styles

I'm currently learning basic Laravel and have already installed the laravel breeze starter kit.
Currently trying to display a table but the tailwind CSS is not being applied to this table:
This is the code i've copied from the tailwind documentation:
<table class="table-auto">
<thead>
<tr>
<th>Song</th>
<th>Artist</th>
<th>Year</th>
</tr>
</thead>
<tbody>
<tr>
<td>The Sliding Mr. Bones (Next Stop, Pottersville)</td>
<td>Malcolm Lockyer</td>
<td>1961</td>
</tr>
<tr>
<td>Witchy Woman</td>
<td>The Eagles</td>
<td>1972</td>
</tr>
<tr>
<td>Shining Star</td>
<td>Earth, Wind, and Fire</td>
<td>1975</td>
</tr>
</tbody>
</table>
I've inserted the code into a blade file as below:
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
{{ __('Cases') }}
</h2>
</x-slot>
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
<div class="p-6 bg-white border-b border-gray-200">
<table class="table-auto">
<thead>
<tr>
<th>Song</th>
<th>Artist</th>
<th>Year</th>
</tr>
</thead>
<tbody>
<tr>
<td>The Sliding Mr. Bones (Next Stop, Pottersville)</td>
<td>Malcolm Lockyer</td>
<td>1961</td>
</tr>
<tr>
<td>Witchy Woman</td>
<td>The Eagles</td>
<td>1972</td>
</tr>
<tr>
<td>Shining Star</td>
<td>Earth, Wind, and Fire</td>
<td>1975</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</x-app-layout>
webpack.mix.js already has tailwindcss included as shown below:
I've also run npm run dev
What am I missing here?
You've only specified how the table columns behave given the contents of each cell. The style you've applied (class="table-auto" is occurring, but you don't notice it since the table width isn't being constrained by any parent element.
Make the screen narrower with the code as-is and you should see the long text in the first row start to wrap.
Additionally the documentation example only shows you the relevant class (table-auto) and not all the other styling. Here's what's really being rendered:
<table class="border-collapse table-auto w-full text-sm">
<thead>
<tr>
<th class="border-b dark:border-slate-600 font-medium p-4 pl-8 pt-0 pb-3 text-slate-400 dark:text-slate-200 text-left">Song</th>
<th class="border-b dark:border-slate-600 font-medium p-4 pt-0 pb-3 text-slate-400 dark:text-slate-200 text-left">Artist</th>
<th class="border-b dark:border-slate-600 font-medium p-4 pr-8 pt-0 pb-3 text-slate-400 dark:text-slate-200 text-left">Year</th>
</tr>
</thead>
<tbody class="bg-white dark:bg-slate-800">
<tr>
<td class="border-b border-slate-100 dark:border-slate-700 p-4 pl-8 text-slate-500 dark:text-slate-400">The Sliding Mr. Bones (Next Stop, Pottersville)</td>
<td class="border-b border-slate-100 dark:border-slate-700 p-4 text-slate-500 dark:text-slate-400">Malcolm Lockyer</td>
<td class="border-b border-slate-100 dark:border-slate-700 p-4 pr-8 text-slate-500 dark:text-slate-400">1961</td>
</tr>
<tr>
<td class="border-b border-slate-100 dark:border-slate-700 p-4 pl-8 text-slate-500 dark:text-slate-400">Witchy Woman</td>
<td class="border-b border-slate-100 dark:border-slate-700 p-4 text-slate-500 dark:text-slate-400">The Eagles</td>
<td class="border-b border-slate-100 dark:border-slate-700 p-4 pr-8 text-slate-500 dark:text-slate-400">1972</td>
</tr>
<tr>
<td class="border-b border-slate-200 dark:border-slate-600 p-4 pl-8 text-slate-500 dark:text-slate-400">Shining Star</td>
<td class="border-b border-slate-200 dark:border-slate-600 p-4 text-slate-500 dark:text-slate-400">Earth, Wind, and Fire</td>
<td class="border-b border-slate-200 dark:border-slate-600 p-4 pr-8 text-slate-500 dark:text-slate-400">1975</td>
</tr>
</tbody>
</table>

Update pivot table in Laravel

I am trying to update a table with spots. I wanted to do it on click with livewire but nothing worked so I figured it would be easier to just use the methods in the Laravel controller that I already had. The attach and detach are used because I have two tables in my view, one with all the stations and the other one, the one I am trying to update, with the stations that are already in a workbook. With the way I have my method right now I can update everything except for the spot and I am not sure why. It is a many to many relationship between the workbooks and stations and spots are an extra field in the pivot table.
<form action="{{route('admin.workbooks.update', [$client_id, $workbook_id])}}" method="POST" enctype="multipart/form-data">
#csrf
#method('PUT')
<div class="form-group">
<label for="">Media Schedule Name</label>
<input type="text" class="form-control" name="wrkb_name" value="{{$workbook_name}}">
</div>
<table class="table-auto w-full mb-6">
<thead>
<tr>
<th class="px-4 py-2"></th>
#if($showLetter)
<th wire:click="sortBy('call_letter')" style="cursor: pointer;" class="px-4 py-2">Call Letter #include('partials.sort-icon',['field'=>'call_letter'])</th>
#endif
<th style="cursor: pointer;" class="px-4 py-2">Spots #include('partials.sort-icon',['field'=>'spots'])</th>
</tr>
</thead>
<tbody>
#foreach($selected_stations as $key => $selected_station)
<tr>
<td class="border px-4 py-2">
<input name="selected_stations[]" wire:model="selected_already" value="{{ $selected_station->id }}" type="checkbox">
</td>
#if($showLetter)
<td class="border px-4 py-2">{{$selected_station->call_letter}}</td>
#endif
<td class="border px-4 py-2">
<input name="spot" type="text" class="appearance-none block w-full bg-gray-200 text-gray-700 border border-gray-200 rounded py-3 px-4 leading-tight focus:outline-none focus:bg-white focus:border-gray-500" value="{{$selected_station->pivot->spot}}" placeholder="{{$selected_station->pivot->spot}}"></td>
<td>
</td>
</tr>
#endforeach
</tbody>
</table>
<br>
<table class="table-auto w-full mb-6">
<thead>
<tr>
<th class="px-4 py-2"></th>
#if($showLetter)
<th wire:click="sortBy('call_letter')" style="cursor: pointer;" class="px-4 py-2">Call Letter #include('partials.sort-icon',['field'=>'call_letter'])</th>
#endif
</tr>
</thead>
<tbody>
#foreach($stations as $key => $station)
<tr>
<td class="border px-4 py-2">
<input name="stations[]" wire:model="selected" value="{{ $station->id }}" type="checkbox">
</td>
#if($showLetter)
<td class="border px-4 py-2">{{$station->call_letter}}</td>
#endif
<td>
</td>
</tr>
#endforeach
</tbody>
</table>
#else
<p class="text-center">Whoops! No users were found 🙁</p>
#endif
<div class="w-full flex pb-10" >
<div class="w-1/6 relative mx-1 space-x-6">
<button class="block appearance-none w-full bg-black border border-gray-200 text-white py-3 px-4 pr-8 rounded leading-tight focus:outline-none focus:bg-white focus:border-gray-500">Edit Media Schedule</button>
</div>
</div>
</form>
public function update(UpdateWorkbookRequest $request,Client $client, Workbook $workbook)
{
$workbook->update($request->only('wrkb_name'));
$workbook->stations()->detach($request->input('selected_stations', []));
$workbook->stations()->attach($request->input('stations', []));
$workbook->stations()->updateExistingPivot('spot', []);
return redirect()->route('admin.workbooks.edit', [$client->id, $workbook->id])->with('success', 'Successfully Edited the Workbook');
}
This is how one of the relationship looks like, although I don't think this helps much
public function workbooks()
{
return $this->belongsToMany(\App\Models\Workbook::class, 'station_workbook', 'station_id', 'workbook_id')->withPivot('spot')->withTimestamps();;
}
I also found that I could use something like this, but I only want to update that specific record and from this code it seems like it updates every instance of that station, no matter what workbook and it doesn't work anyway.
foreach($workbook->stations as $key => $station){
dd($station->workbooks);
$station->workbooks()->updateExistingPivot($key, ['spot' => $request ->spot]);
}
I have also tried this with no success
$usedStations=$request->input('stations', []);
$workbook->update($request->only('wrkb_name'));
$workbook->stations()->detach($request->input('selected_stations', []));
$workbook->stations()->attach($request->input('stations', []));
foreach($usedStations as $usedStation)
{
$station_id = Station::where('id',$usedStation)->value('id');
$workbook->stations()->updateExistingPivot($station_id, ['spot' => $request ->spot]);
}
Quoting from Laravel Doc: (Link)
When attaching a relationship to a model, you may also pass an array
of additional data to be inserted into the intermediate table
So, you can do :
$workbook->stations()->attach($request->input('stations', []),['spot'=>$request->input('spot')]);
UPDATE
Currently, in your code only single value for 'spot' field is being passed to the request. Change it to:
<input name="spot[{{$selected_station->id}}]" type="text" class="appearance-none block w-full bg-gray-200 text-gray-700 border border-gray-200 rounded py-3 px-4 leading-tight focus:outline-none focus:bg-white focus:border-gray-500" value="{{$selected_station->pivot->spot}}" placeholder="{{$selected_station->pivot->spot}}"></td>
update() method should be:
public function update(UpdateWorkbookRequest $request,Client $client, Workbook $workbook)
{
$workbook->update($request->only('wrkb_name'));
$workbook->stations()->detach($request->input('selected_stations', []));
$workbook->stations()->attach($request->input('stations', []));
//take only the spot values of selected_stations that were unchecked
$spots = array_diff_key($request->input('spot',[]), array_flip($request->input('selected_stations',[])));
foreach($spots as $station_id=>$spot_value){
$workbook->stations()->updateExistingPivot($station_id, ['spot'=>$spot_value]);
}
return redirect()->route('admin.workbooks.edit', [$client->id, $workbook->id])->with('success', 'Successfully Edited the Workbook');
}

Laravel foreach in if statement

This is my code
#if ($story)
<table class="table-auto my-3 w-full">
<thead class="justify-between">
<tr class="bg-gray-800">
<th class="px-16 py-2">
<span class="text-gray-300">#Id</span>
</th>
<th class="px-16 py-2">
<span class="text-gray-300">Name</span>
</th>
<th class="px-16 py-2">
<span class="text-gray-300">Description</span>
</th>
<th class="px-16 py-2">
<span class="text-gray-300">Actions</span>
</th>
</tr>
</thead>
<tbody class="bg-gray-200">
#foreach ($story as $item)
<tr class="bg-white border-4 border-gray-200">
<td class="px-16 py-2 flex flex-row items-center">
{{ $item->id }}
</td>
<td>
{{ Str::words($item->name, 3, $end = '...') }}
</td>
<td class="px-16 py-2">
{!! Str::words($item->description, 6, $end = '...') !!}
</td>
<td class="px-16 py-2">
<a href="{{ route('dashboard.story.edit', $item->id) }}">
<button class="bg-blue-500 text-white px-4 py-1 border rounded-md hover:bg-white hover:border-blue-500 hover:text-black">
Edit
</button>
</a>
</td>
</tr>
#endforeach
</tbody>
</table>
#else
<p>No results found</p>
#endif
This is my controller
public function index()
{
$story = Story::paginate(10);
return view('admin.story.index', ['story' => $story]);
}
I'm using Laravel 8 and I don't want to show table headers when the value is null. Laravel showing tables headers and not showing the 'No results found' message.
Any help would be appreciated.
when you use paginate, your data split. so if you want check this data empty or not just use
#if( $story->total() )
just replace it with
#if ( $story )

Resources