Watch newValue, oldValue from Angular to Vue - arrays
Im converting a site from php and Angular to Vue Im pretty new to both but I'm 99% done. I need to watch when the data changes. its basically a league table and when some one goes up it css they go green when they go down css they go red. Ive tried converting the Angular function here
$scope.$watch(function () {
return $scope.scores;
},
function(newVal, oldVal) {
angular.forEach(newVal, function(value1, key1) {
angular.forEach(oldVal, function(value2, key2) {
if (value1.id === value2.id) {
if (key1 < key2) {
value1.posChang = 'up';
} else if (key1 > key2) {
value1.posChang = 'down';
}
}
});
});
}, true);
and convert to vue here
watch: {
scores: {
immediate: true,
handler (newValue, oldValue) {
console.log(newValue)
console.log(oldValue)
this.newValue.forEach(newValue, function (value1, key1) {
this.oldValue.forEach(oldValue, function (value2, key2) {
if (value1.id === value2.id) {
if (key1 < key2) {
value1.posChang = 'up';
} else if (key1 > key2) {
value1.posChang = 'down';
}
}
});
});
},
deep: true
}
},
it console prints both Old and New but it crashes when it hits the forEach
any help would be great
as requested the whole file :-
<template>
<div id="rScore">
<div class="title">
<h2>{{ data.full_name }} - {{ data.round_head }}</h2>
<br />
<p>
{{ data.course_dates }}
</p>
</div>
<table>
<thead>
<tr class="title" v-if="this.roundsPlayed > 1">
<th :class="cell">
Pos
</th>
<th :class="cell">
Player Name
</th>
<th :class="cell">
Nat.
</th>
<th :class="cell">
Par
</th>
<th
v-for="(i, roundIt) in range(1, roundsPlayed)"
:key="roundIt"
:class="cell"
>
R{{ i }}
</th>
<th :class="cell">
Score
</th>
</tr>
<tr v-else>
<th :class="cell">
Pos
</th>
<th :class="cell">
Player Name
</th>
<th :class="cell">
Nat.
</th>
<th :class="cell">
Par
</th>
<th :class="cell">
Score
</th>
</tr>
</thead>
<tbody v-if="this.roundsPlayed > 1">
<template v-for="(scores, index) in scores">
<tr #click.stop="rowClicked(index)" :key="index + Math.random()">
<td :class="cell">
{{ scores.pos }}
</td>
<td #click="playerCard" :title="scores.member_no" :class="cell">
{{ scores.name }}
</td>
<td :class="cell" :title="scores.nationality">
<span>
<img
:class="flag"
:src="
('https://assets.ocs-sport.com/flags/svg/flag_' +
scores.nationality)
| lowercase
"
/>
</span>
</td>
<td v-if="scores.vspar < 0" :class="[up, cell]">
{{ scores.vspar }}
</td>
<td v-else-if="scores.vspar > 0" :class="[down, cell]">
{{ scores.vspar }}
</td>
<td v-else :class="cell">
{{scores.vspar}}
</td>
<td :class="cell">
<span v-if="scores.vspar_R1 < 0" :class="[up, cell]">
{{ scores.score_R1 }}
</span>
<span v-else-if="scores.vspar_R1 > 0" :class="[down, cell]">
{{ scores.score_R1 }}
</span>
<span v-else :class="cell">
{{ scores.score_R1 }}
</span>
</td>
<td v-if="roundsPlayed > 1" :class="cell">
{{ scores.score_R2 }}
</td>
<td v-if="roundsPlayed > 2" :class="cell">
{{ scores.score_R3 }}
</td>
<td v-if="roundsPlayed > 3" :class="cell">
{{ scores.score_R4 }}
</td>
<td v-if="roundsPlayed > 4" :class="cell">
{{ scores.score_R5 }}
</td>
<td v-if="roundsPlayed > 5" :class="cell">
{{ scores.score_R6 }}
</td>
<td v-if="roundsPlayed > 6" :class="cell">
{{ scores.score_R7 }}
</td>
<td v-else :class="cell">
{{ scores.score }}
</td>
</tr>
<tr
#click.stop="rowClicked(rowClicked === -1)"
v-if="index === clickedRow"
class="tr-kids"
:key="index + Math.random()"
>
<b-row>
<b-col>
<table :class="tableRow">
<thead>
<tr class="titleReport">
<th :class="cellReport">
Hole
</th>
<th :class="cellReport">
1
</th>
<th :class="cellReport">
2
</th>
<th :class="cellReport">
3
</th>
<th :class="cellReport">
4
</th>
<th :class="cellReport">
5
</th>
<th :class="cellReport">
6
</th>
<th :class="cellReport">
7
</th>
<th :class="cellReport">
8
</th>
<th :class="cellReport">
9
</th>
<th :class="cellReport">
OUT
</th>
</tr>
</thead>
<tbody>
<tr class="titleReport">
<td :class="cellReport">
Yards
</td>
<td
:class="cellReport"
v-for="number in playerCardData.course_length_yards
.split(',')
.slice(0, 9)"
:key="number"
>
{{ number }}
</td>
<td :class="cellReport">
{{ playerCardData.course_out_length.slice(1, 6) }}
</td>
</tr>
<tr class="titleReport">
<td :class="cellReport">
Par
</td>
<td
:class="cellReport"
v-for="number in playerCardData.course_par
.split(',')
.slice(0, 9)"
:key="number"
>
{{ number }}
</td>
<td :class="cellReport">
{{ playerCardData.course_out_par.slice(1, 5) }}
</td>
</tr>
<tr
v-for="(i, roundIt1) in range(1, roundsPlayed)"
:key="roundIt1"
:class="cellReport"
>
<td>R{{ i }}</td>
<template v-if="i === 1">
<td
:class="cellReport"
v-for="scoreRound in playerCardData.score_R1
.split(',')
.slice(0, 10)"
:key="scoreRound"
>
<span>
{{ scoreRound }}
</span>
</td>
</template>
<template v-else-if="i === 2">
<td
:class="cellReport"
v-for="scoreRound in playerCardData.score_R2
.split(',')
.slice(0, 10)"
:key="scoreRound"
>
<span>
{{ scoreRound }}
</span>
</td>
</template>
<template v-else-if="i === 3">
<td
:class="cellReport"
v-for="scoreRound in playerCardData.score_R3
.split(',')
.slice(0, 10)"
:key="scoreRound"
>
<span>
{{ scoreRound }}
</span>
</td>
</template>
<template v-else-if="i === 4">
<td
:class="cellReport"
v-for="scoreRound in playerCardData.score_R4
.split(',')
.slice(0, 10)"
:key="scoreRound"
>
<span>
{{ scoreRound }}
</span>
</td>
</template>
<template v-else-if="i === 5">
<td
:class="cellReport"
v-for="scoreRound in playerCardData.score_R5
.split(',')
.slice(0, 10)"
:key="scoreRound"
>
<span>
{{ scoreRound }}
</span>
</td>
</template>
<template v-else-if="i === 6">
<td
:class="cellReport"
v-for="scoreRound in playerCardData.score_R6
.split(',')
.slice(0, 9)"
:key="scoreRound"
>
<span>
{{ scoreRound }}
</span>
</td>
</template>
<template v-else-if="i === 7">
<td
:class="cellReport"
v-for="scoreRound in playerCardData.score_R7
.split(',')
.slice(0, 9)"
:key="scoreRound"
>
<span>
{{ scoreRound }}
</span>
</td>
</template>
</tr>
</tbody>
</table>
</b-col>
<b-col>
<table :class="tableRow">
<thead>
<tr class="titleReport">
<th :class="cellReport">
10
</th>
<th :class="cellReport">
11
</th>
<th :class="cellReport">
12
</th>
<th :class="cellReport">
13
</th>
<th :class="cellReport">
14
</th>
<th :class="cellReport">
15
</th>
<th :class="cellReport">
16
</th>
<th :class="cellReport">
17
</th>
<th :class="cellReport">
18
</th>
<th :class="cellReport">
IN
</th>
<th :class="cellReport">
TOT
</th>
</tr>
</thead>
<tbody>
<tr class="titleReport">
<td
:class="cellReport"
v-for="number in playerCardData.course_length_yards
.split(',')
.slice(9, 18)"
:key="number"
>
{{ number }}
</td>
<td :class="cellReport">
{{ playerCardData.course_in_length.slice(1, 6) }}
</td>
<td :class="cellReport">
{{ playerCardData.course_total_length.slice(1, 6) }}
</td>
</tr>
<tr class="titleReport">
<td
:class="cellReport"
v-for="number in playerCardData.course_par
.split(',')
.slice(9, 18)"
:key="number"
>
{{ number }}
</td>
<td :class="cellReport">
{{ playerCardData.course_in_par.slice(1, 5) }}
</td>
<td :class="cellReport">
{{ playerCardData.course_total_par.slice(1, 5) }}
</td>
</tr>
<tr
v-for="(i, roundIt2) in range(1, roundsPlayed)"
:key="roundIt2"
:class="cellReport"
>
<template v-if="i === 1">
<td
:class="cellReport"
v-for="scoreRound in playerCardData.score_R1
.split(',')
.slice(10, 21)"
:key="scoreRound"
>
<span>
{{ scoreRound }}
</span>
</td>
</template>
<template v-else-if="i === 2">
<td
:class="cellReport"
v-for="scoreRound in playerCardData.score_R2
.split(',')
.slice(10, 21)"
:key="scoreRound"
>
<span>
{{ scoreRound }}
</span>
</td>
</template>
<template v-else-if="i === 3">
<td
:class="cellReport"
v-for="scoreRound in playerCardData.score_R3
.split(',')
.slice(10, 21)"
:key="scoreRound"
>
<span>
{{ scoreRound }}
</span>
</td>
</template>
<template v-else-if="i === 4">
<td
:class="cellReport"
v-for="scoreRound in playerCardData.score_R4
.split(',')
.slice(10, 21)"
:key="scoreRound"
>
<span>
{{ scoreRound }}
</span>
</td>
</template>
<template v-else-if="i === 5">
<td
:class="cellReport"
v-for="scoreRound in playerCardData.score_R5
.split(',')
.slice(10, 21)"
:key="scoreRound"
>
<span>
{{ scoreRound }}
</span>
</td>
</template>
<template v-else-if="i === 6">
<td
:class="cellReport"
v-for="scoreRound in playerCardData.score_R6
.split(',')
.slice(10, 21)"
:key="scoreRound"
>
<span>
{{ scoreRound }}
</span>
</td>
</template>
<template v-else-if="i === 7">
<td
:class="cellReport"
v-for="scoreRound in playerCardData.score_R7
.split(',')
.slice(10, 21)"
:key="scoreRound"
>
<span>
{{ scoreRound }}
</span>
</td>
</template>
</tr>
</tbody>
</table>
</b-col>
</b-row>
</tr>
</template>
</tbody>
<tbody v-else>
<tr v-for="(scores, index) in scores" :key="index">
<td :class="cell">
{{ scores.pos }}
</td>
<td :class="{ up: scores.posChang === 'up', down: scores.posChang === 'down' }" >
{{ scores.name }}
</td>
<td :class="cell" :title="scores.nationality">
<span>
<img
:class="flag"
:src="
('https://assets.ocs-sport.com/flags/svg/flag_' +
scores.nationality)
| lowercase
"
/>
</span>
</td>
<td v-if="scores.vspar < 0" :class="[cell, up]">
{{ scores.vspar }}
</td>
<td v-else-if="scores.vspar > 0" :class='[cell, down]'>
{{scores.vspar}}
</td>
<td v-else :class="cell">
{{scores.vspar}}
</td>
<td v-if="scores.vspar < 0" :class="[cell, up]">
{{ scores.score }}
</td>
<td v-else-if="scores.vspar > 0" :class='[cell, down]'>
{{scores.score}}
</td>
<td v-else :class="cell">
{{scores.score}}
</td>
</tr>
</tbody>
</table>
</div>
</template>
<script>
import axios from "axios";
export default {
name: "rScore",
props: ["data", "home", "scores"],
data() {
return {
cell: "cell",
cellReport: "cellReport",
flag: "flag",
title: "title",
tableRow: "tableRow",
clickedRow: -1,
courseData: this.data,
cardID: [],
playerCardData: [],
id: this.$route.query.id,
code: this.$route.query.code,
up:'up',
down:'down'
};
},
methods: {
range: function(start, end) {
if (this.roundsPlayed === 1) return this.roundsPlayed;
else
return Array(end - start + 1)
.fill()
.map((_, idx) => start + idx);
},
rowClicked: function(index) {
this.clickedRow = index;
},
playerCard: function(event) {
var cardID = event.target.getAttribute("title");
return (
(this.cardID = cardID),
axios
.get(
" url" +
this.id +
"/" +
this.id +
"-" +
this.code +
"-cards-" +
this.cardID +
".json?randomadd=1613462964358"
)
.then((response) => (this.playerCardData = response.data))
);
},
},
watch: {
scores: {
immediate: true,
handler (newValue, oldValue) {
console.log(newValue)
console.log(oldValue)
newValue.forEach(newValue, function (value1, key1) {
oldValue.forEach(oldValue, function (value2, key2) {
if (value1.id === value2.id) {
if (key1 < key2) {
value1.posChang = 'up';
} else if (key1 > key2) {
value1.posChang = 'down';
}
}
});
});
},
deep: true
}
},
computed: {
roundsPlayed() {
return this.data.rounds_played;
},
},
filters: {
lowercase: function(value) {
if (!value) {
return "";
}
return value.toLowerCase() + ".svg";
},
},
};
</script>
<style scoped>
.cell {
width: 10%;
text-align: center;
}
.cellReport {
width: 10%;
text-align: center;
}
.tableRow {
width: 100%;
}
.flag {
width: 7%;
}
.row {
padding-top: 10%;
padding-bottom: 10%;
width: 405%;
padding-left: 20%;
background-color: white;
}
.col {
padding-left: 0;
padding-right: 0;
}
.title {
text-align: center;
background-color: #263056;
color: white;
}
.titleReport {
background-color: #1b509b !important;
color: white;
}
tr:nth-child(even) {
background-color: #f2f2f2;
}
.up{
color: #A52A2A;
}
.down{
color: #1E90FF;
}
</style>
Remove this. from newValue and oldValue
Took Mi-Creativity code pen above and modified as it was making everyone the same
this now works perfect. thanks for the help
watch: {
score: {
immediate: true,
handler(newValue, oldValue) {
newValue.forEach((value1, key1) => {
oldValue.forEach((value2, key2) => {
if (value1.id === value2.id) {
if(key1 < key2){
value1.posChang = 'up';
} else if (key1 > key2) {
value1.posChang = 'down';
}
}
});
});
console.log(newValue);
},
deep: true,
},
},
Related
How do I format Currency correctly in React using numberWithCommas Regex
I am trying to simulate data gotten from a Rest API into currency format and Decimal places etc. When i do something like this : const numberWithCommas = (x) => { return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','); } This code is supposed to help me to convert the data gotten from the REST api into decimal places so instead of 12345 i am supposed to have it as 1,2345.00 When i try something, it gives me a totally white screen. My code is looking thus import React, { useState } from 'react'; import { Link, useHistory } from 'react-router-dom'; const Balance = () => { const[accNum, setAccNum] = useState(''); const[accDetails, setAccDetails] = useState(''); const numberWithCommas = (x) => { return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','); } function getAccountBal(){ fetch('https://financeplus.herokuapp.com/api/getaccounts/'+accNum,{ method : 'GET', mode : 'cors', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, }).then((response)=>response.json()).then((responseJson)=>{ setAccDetails(responseJson.data) }) } return ( <div align="center"> <table border="0" width="98%"> <tr> <td> </td> </tr> <tr> <td className="homeFormTable"> <table border="0" width="100%"> <tr> <td> <img border="0" src="images/FinancePlus_color_colorLogo.png" width="157" height="36" /></td> <td width="136"> <p align="center" /> <li className="Logout"><Link to="/openaccount" className="ForLogout">Logout</Link></li> </td> </tr> </table> </td> </tr> <tr> <td> </td> </tr> <div className="upMenuTable"> <div className="nav-links"> <ul> <li><Link to="/homepage">Home</Link></li> <li><Link to="/openaccount">Account Opening</Link></li> <li><Link to="/balance">Account Balance</Link></li> <li><Link to="/transfers">Transfers</Link></li> <li><Link to="/cashmgt">Deposit/Withdrawals</Link></li> </ul> </div> </div> <div className="SideNav"> <div className="sideNav-links"> <ul> <li><Link to="/homepage">Home</Link></li> <li><Link to="/openaccount">Account Opening</Link></li> <li><Link to="/balance">Account Balance</Link></li> <li><Link to="/transfers">Transfers</Link></li> <li><Link to="/cashmgt">Deposit/Withdrawals</Link></li> </ul> </div> </div> <div className="playTable"> <table> <div align="center"> <tr> <td> <p align="right" /> <font face="Arial Black" size="1"> Account Number: </font> <input type="text" name="T1" size="59" onChange ={(e) =>setAccNum(e.target.value)} className="searchBoxAcc" /> </td> <td width="121"> <p align="right" /> <input onClick={getAccountBal} type="submit" value="Search" name="B1" className="searchBoxBtn" /> </td> </tr> </div> <div align="center" className="infoArea"> <tr> <td width="18%" align="right"><font face="Arial Black" size="1"> Full Name </font> </td> <td width="33%" align="left"> <font face="Arial Black" size="1"> {accDetails.fullname}</font> </td> <td width="1%"> </td> <td width="21%" align="right"><font face="Arial Black" size="1"> Address </font></td> <td width="24%" align="left" rowspan="2"> <font face="Arial Black" size="1"> {accDetails.address}</font><p /> <font face="Arial Black" size="2"> </font> </td> </tr> <tr> <td width="18%" align="right"><font face="Arial Black" size="1"> City </font> </td> <td width="33%" align="left"> <font face="Arial Black" size="1"> {accDetails.city}</font> </td> <td width="1%"> </td> </tr> <tr> <td width="18%" align="right"><font face="Arial Black" size="1"> Telephone </font> </td> <td width="33%" align="left"> <font face="Arial Black" size="1"> {accDetails.tel}</font> </td> <td width="1%"> </td> <td width="21%" align="right"><font face="Arial Black" size="1"> Balance </font></td> <td width="24%" align="left" rowspan="2"> <font face="Arial Black" size="2"> {numberWithCommas(accDetails.bal.toFixed(2))} Currency = {accDetails.ccy}</font><p /> <font face="Arial Black" size="2"> </font> </td> </tr> </div> </table> </div> <div className="Footer"> <table className="footerTable"> <p className="reserved"> © Finance Plus 2021</p> </table> </div> </table> </div> ); } export default Balance; how do I format this correctly?
Speed up record Load time laravel using Datatable
I am working with laravel project which uses a mysql database. I have 40,000 record in my database, it's take too much time around 20 min to load page. following is the function i use to get data. plase check my code and help me to solve this issue. Code is here CustomerController public function index() { // abort_if(Gate::denies('customer_access'), Response::HTTP_FORBIDDEN, '403 Forbidden'); $customers = Customer::all(); return view('admin.customers.index', compact('customers')); } IndexController #extends('layouts.admin') #section('content') #can('customer_create') <div style="margin-bottom: 10px;" class="row"> <div class="col-lg-12"> <a class="btn btn-success" href="{{ route("admin.customers.create") }}"> {{ trans('global.add') }} {{ trans('cruds.customer.title_singular') }} </a> | <a class="btn btn-success" href="importcustomer"> Import or Export </a> </div> </div> #endcan <div class="card"> <div class="card-header"> {{ trans('cruds.customer.title_singular') }} {{ trans('global.list') }} </div> <div class="card-body"> <div class="table-responsive"> <table id="tabls" width="100%" class=" table table-bordered table-striped table-hover datatable datatable-User"> <thead> <tr> <th class="small-col"> </th> <th class="small-col"> {{ trans('cruds.customer.fields.cid') }} </th> <th class="big-col"> {{ trans('cruds.customer.fields.cname') }} </th> <th class="big-col"> {{ trans('cruds.customer.fields.address') }} </th> <th class="big-col"> {{ trans('cruds.customer.fields.county') }} </th> <th class="big-col"> {{ trans('cruds.customer.fields.country') }} </th> <th class="big-col"> {{ trans('cruds.customer.fields.pcode') }} </th> <th class="big-col"> {{ trans('cruds.customer.fields.phone') }} </th> <th class="big-col"> {{ trans('cruds.customer.fields.mobile') }} </th> <th class="big-col"> {{ trans('cruds.customer.fields.email') }} </th> <th class="big-col"> {{ trans('cruds.customer.fields.web') }} </th> <th class="big-col"> {{ trans('cruds.customer.fields.mcat') }} </th> <th class="big-col"> {{ trans('cruds.customer.fields.scat') }} </th> <th class="small-col"> {{ trans('cruds.customer.fields.business') }} </th> <th class="big-col"> {{ trans('cruds.customer.fields.lead') }} </th> <th class="big-col"> {{ trans('cruds.customer.fields.rep') }} </th> </tr> </thead> <tbody> #foreach($customers as $key => $supplier) <tr data-entry-id="{{ $supplier->id }}"> <td> #can('customer_show') <a class="btn btn-xs btn-primary" href="{{ route('admin.customers.show', $supplier->id) }}"> {{ trans('global.view') }} </a> #endcan #can('customer_edit') <a class="btn btn-xs btn-info" href="{{ route('admin.customers.edit', $supplier->id) }}"> {{ trans('global.edit') }} </a> #endcan #can('customer_delete') <form action="{{ route('admin.customers.destroy', $supplier->id) }}" method="POST" onsubmit="return confirm('{{ trans('global.areYouSure') }}');" style="display: inline-block;"> <input type="hidden" name="_method" value="DELETE"> <input type="hidden" name="_token" value="{{ csrf_token() }}"> <input type="submit" class="btn btn-xs btn-danger" value="{{ trans('global.delete') }}"> </form> #endcan </td> <td> {{ $supplier->cid ?? 'NA' }} </td> <td> {{ $supplier->cname ?? '' }} </td> <td> {{ $supplier->address ?? '' }} </td> <td> {{ $supplier->county ?? '' }} </td> <td> {{ $supplier->country ?? '' }} </td> <td> {{ $supplier->pcode ?? '' }} </td> <td> {{ $supplier->phone ?? '' }} </td> <td> {{ $supplier->mobile ?? '' }} </td> <td> {{ $supplier->email ?? '' }} </td> <td> {{ $supplier->web ?? '' }} </td> <td> {{ $supplier->mcat ?? '' }} </td> <td> {{ $supplier->scat ?? '' }} </td> <td> {{ $supplier->business ?? '' }} </td> <td> {{ $supplier->lead ?? '' }} </td> <td> {{ $supplier->rep ?? '' }} </td> </tr> #endforeach </tbody> </table> </div> </div> </div> <style> .small-col { width: 100px !important; } .big-col { width: 200px !important; } table#tabls{ table-layout:fixed; }</style> #endsection #section('scripts') #parent <script> $(function () { let dtButtons = $.extend(true, [], $.fn.dataTable.defaults.buttons) #can('customer_delete') let deleteButtonTrans = '{{ trans('global.datatables.delete') }}' let deleteButton = { text: deleteButtonTrans, url: "{{ route('admin.users.massDestroy') }}", className: 'btn-danger', action: function (e, dt, node, config) { var ids = $.map(dt.rows({ selected: true }).nodes(), function (entry) { return $(entry).data('entry-id') }); if (ids.length === 0) { alert('{{ trans('global.datatables.zero_selected') }}') return } if (confirm('{{ trans('global.areYouSure') }}')) { $.ajax({ headers: {'x-csrf-token': _token}, method: 'POST', url: config.url, data: { ids: ids, _method: 'DELETE' }}) .done(function () { location.reload() }) } } } dtButtons.push(deleteButton) #endcan $.extend(true, $.fn.dataTable.defaults, { order: [[ 1, 'desc' ]], pageLength: 25, }); let options = { "sScrollX": "100%", "sScrollXInner": "110%", "bScrollCollapse": true, "colReorder": true, }; $('#tabls').DataTable(options); // { // buttons: dtButtons, // }), $('a[data-toggle="tab"]').on('shown.bs.tab', function(e){ $($.fn.dataTable.tables(true)).DataTable() .columns.adjust(); }); }) </script> #endsection Please advise me to solve this issue Thank you for your time.
Please refer this one for datatable integration https://github.com/yajra/laravel-datatables
Use server side datatables Install this a working example from my side into controller if ($request->ajax()) { $timeZone = $request->timeZone; $query = Order::query(); $data = $query->with('buyerOrders', 'sellerOrders', 'oderItems', 'teamMemberOrder') ->where('sellerId', Auth::id()) ->where('status', OrderStatusEnum::COMPLETED) ->orderBy('createdAt', 'DESC') ->get(); return Datatables::of($data) ->addIndexColumn() ->addColumn('C_name', function ($row) { return $row->buyerOrders->getFullNameAttribute(); }) ->addColumn('teamMember', function ($order) { if ($order->teamMemberId) { return $order->teamMemberOrder->getFullNameAttribute(); } else { return 'N/A'; } }) ->addColumn('price', function (Order $order) { return $order->oderItems->map(function ($oderItem) { return $oderItem->unitPrice * $oderItem->productQuantity; })->sum(); }) ->addColumn('isScheduled', function ($row) { if ($row->isScheduled == 1) { return ' <span class="badge badge-pill badge-info">Scheduled </span> '; } else { return ' <span class="badge badge-pill badge-success">Immediate</span> '; } }) ->addColumn('booking', function ($row) { return $row->id; }) ->addColumn('orderType', function ($row) { if ($row->isDeliverable == 1) { return ' <span class="badge badge-pill badge-info">Deliverable </span> '; } else { return ' <span class="badge badge-pill badge-success"> Non Deliverable</span> '; } }) ->addColumn('orderPlace', function ($order) use ($timeZone) { return Carbon::parse($order->updatedAt)->setTimezone($timeZone)->format('Y-m-d H:i:s'); }) ->rawColumns(['isScheduled', 'orderType']) ->make(true); } return view("Store.order.completeOrder"); }
html2canvas and pdfmake create blank pdf
I'm trying to generate a pdf from an html div with html2canvas and pdfMake but always got a blank page or just part of the div! How can I get the entire div content? Here is the js code : $scope.PrintFiche = function () { var prom = new Promise(function (resolve, reject) { html2canvas(document.getElementById('DevisImpression'), { onrendered: function (canvas) { var data = canvas.toDataURL("image/png"); var docDefinition = { content: [{ image: data }] }; resolve(docDefinition); } }); }) prom.then(function (docDef) { pdfMake.createPdf(docDef).download($scope.FicheName + ".pdf"); }) } And the html : <div class="Partie" id="DevisImpression"> <div id="PartieInner"> <h2 id="TitreDevis"> <b> Etablissement du devis </b> </h2> <div id="ImgVoitureDevis"> <img id="ImgVoitureAdapt" src="../../Assets/Images/test.jpg" /> </div> <div id="OptMult"> <table id="TableDevis"> <tr> <td class="td1"> Modèle : </td> <td class="td2"> {{modele.displayName}} </td> </tr> <tr> <td class="td1"> Classe : </td> <td class="td2"> {{classe.displayName}} </td> </tr> <tr> <td class="td1"> Moteur : </td> <td class="td2"> {{moteur.displayName}} </td> </tr> <tr> <td class="td1"> Couleur : </td> <td class="td2"> {{couleur.displayName}} </td> </tr> <tr> <td class="td1"> Jantes : </td> <td class="td2"> {{jante.displayName}} </td> </tr> </table> </div> <table id="Devis"> <tr> <th class="tdDevis2"> Options </th> <th class="tdDevis2"> Prix </th> </tr> <tr ng-repeat="optionDev in optionsDevis"> <td class="td3"> {{optionDev.displayName}} </td> <td class="td4"> {{optionDev.prix}} € </td> </tr> </table> </div> <h2 id="TotalTitre"> <b> TOTAL </b> </h2> <input type="text" id="Total" value="{{total}} €" disabled /> <br /> </div>
I have a simple solution. Try this. $scope.downloadQuotation = function () { html2canvas(document.getElementById('rosterPrintView'), { onrendered: function (canvas) { var data = canvas.toDataURL(); var docDefinition = { content: [{ image: data, width: 500 }] }; pdfMake.createPdf(docDefinition).download("Roster.pdf"); } }); };
how to loop through a table in selenium?
I am new to selenium and I have this question where I need to loop through a table and get the values in that table <table> <tr> <td style="width:5px"> </td> <td> <table class="reportTable" id="Allocations"> <tbody> <tr class="table_header"> <td style="width:5px;"> <img class="HideImage" src="Images/minus.gif" alt="Hide Details"> </td> <td style="width:33%"> Channel of Trade</td> <td style="width:33%"> PILOT TRAVEL CENTE-122194-W/S - UNB Contract</td> <td style="width:33%"> <span id="TruckLoading_10142602_Info" style="COLOR: white;text-decoration:underline;cursor:pointer"> Trucks loading - 0</span> </td> </tr> <tr> <td style="width:5px;"> </td> <td colspan="3"> <table rules="rows" class="reportTable" font-family="Tahoma" pagerstyle-visible="False" id="TerminalGrid" border="1"> <tbody> <tr class="productlabel2" align="left"> <td scope="col" style="width:5px;"> </td> <td> Product Details</td> </tr> <tr class="hdr2"> <td scope="col" style="width:5px;"> </td> <td scope="col"> Fuel Type</td> </tr> <tr class="FuelTypeHeader"> <td style="width:5px;border:none" onclick="ShowHideDetails(this)"> <img class="HideImage" src="Images/minus.gif" alt="Hide Details" id="Fuel_Img"> </td> <td style="border-left:none;border-right:none; padding-left:3px"> <table id="C_V" style="width:100%;border-collapse:collapse; border:none; padding:0px 0px 0px 0px; margin:0px 0px 0px 0px;"> <tbody> <tr> <td style="width:20em;"> <span> DSL - LSD/ULSD</span> </td> <td style="width:60em;"> <span id="CVSpan"> <span style="margin-right:10px"> <span style="float:left;padding-top:3px"> Currently:</span> <span style="float:left;width:6em;padding-top:2px; margin-left:5px; margin-right:5px;margin-top:2px;padding-bottom:2px; text-align:center; background-color:#00FF00;"> Available</span> <span style="float:left; padding-top:3px"> <b> 30,839</b> gallons remaining until Mon 8/1/2016 12:00:00 AM CDT</span> </span> </span> </td> <td align="right"> </td> </tr> </tbody> </table> </td> </tr> <tr style=""> <td style="width:5px;"> </td> <td> <table id="ProdDetails" rules="all" pagerstyle-visible="False" style="width: 100%" border="1"> <tbody> <tr class="table_header2"> <th scope="col"> Nominated Volume</th> <th scope="col"> Allocation Period</th> <th scope="col"> Allocation %</th> <th scope="col"> Allocation Start Amt</th> <th scope="col"> Allocation Lifted</th> <th scope="col"> Allocation Remaining</th> <th scope="col"> GPO Allowance</th> <th scope="col" class="center width8em"> GPO Remaining</th> <th scope="col"> Category Status</th> <th scope="col"> Ratability Status</th> <th scope="col"> Next Scheduled Refresh Date</th> <th scope="col"> Reference ID</th> </tr> <tr class="tablerow2"> <td class="right width8em"> 41,118</td> <td class="center width10em"> Daily</td> <td class="right"> 75%</td> <td class="right"> 30,839</td> <td class="right"> 0</td> <td class="right"> 30,839</td> <td class="right"> 0</td> <td class="right width8em bold" id="GPO_Rmd"> 0 </td> <td class="center" style="background-color:#00FF00;"> Available</td> <td class="center" style="background-color:#0099CC;"> Below Trend</td> <td class="center width20em"> 8/1/2016 12:00:00 AM CDT</td> <td class="center width20emWordWrap"> DSL - LSD/ULSD</td> </tr> <tr class="tablerow2"> <td class="right width8em"> 287,826</td> <td class="center width10em"> Weekly</td> <td class="right"> 125%</td> <td class="right"> 359,783</td> <td class="right"> 114,083</td> <td class="right"> 245,700</td> <td class="right"> 0</td> <td class="right width8em bold" id="GPO_Rmd"> 0 </td> <td class="center" style="background-color:#00FF00;"> Available</td> <td class="center" style="background-color:#0099CC;"> Below Trend</td> <td class="center width20em"> 8/4/2016 12:00:00 AM CDT</td> <td class="center width20emWordWrap"> DSL - LSD/ULSD</td> </tr> <tr class="tablerow2"> <td class="right width8em"> 1,233,540</td> <td class="center width10em"> Monthly</td> <td class="right"> 115%</td> <td class="right"> 1,418,571</td> <td class="right"> 1,361,264</td> <td class="right"> 57,307</td> <td class="right"> 0</td> <td class="right width8em bold" id="GPO_Rmd"> 0 </td> <td class="center" style="background-color:#FFFF00;"> Low</td> <td class="center" style="background-color:#00CC00;"> On Track</td> <td class="center width20em"> 8/1/2016 12:00:00 AM CDT</td> <td class="center width20emWordWrap"> DSL - LSD/ULSD</td> </tr> </tbody> </table> </td> </tr> <tr id="GPO_Row"> <td style="width:5px;"> </td> <td> </td> </tr> </tbody> </table> </td> </tr> <tr class="table_header"> <td style="width:5px;" onclick="ShowHideDetails(this)"> <img class="HideImage" src="Images/minus.gif" alt="Hide Details"> </td> <td style="width:33%"> Channel of Trade</td> <td style="width:33%"> PILOT TRAVEL CENTE-122194-W/S - UNB Fwrd Cont</td> <td style="width:33%"> <span id="TruckLoading_17049566_Info" style="COLOR: white;text-decoration:underline;cursor:pointer" onclick="GetTruckLoadingInformationJS(this,17049566);"> Trucks loading - 0</span> </td> </tr> <tr> <td style="width:5px;"> </td> <td colspan="3"> <table rules="rows" class="reportTable" font-family="Tahoma" pagerstyle-visible="False" id="TerminalGrid" border="1"> <tbody> <tr class="productlabel2" align="left"> <td scope="col" style="width:5px;"> </td> <td> Product Details</td> </tr> <tr class="hdr2"> <td scope="col" style="width:5px;"> </td> <td scope="col"> Fuel Type</td> </tr> <tr class="FuelTypeHeader"> <td style="width:5px;border:none" onclick="ShowHideDetails(this)"> <img class="HideImage" src="Images/minus.gif" alt="Hide Details" id="Fuel_Img"> </td> <td style="border-left:none;border-right:none; padding-left:3px"> <table id="C_V" style="width:100%;border-collapse:collapse; border:none; padding:0px 0px 0px 0px; margin:0px 0px 0px 0px;"> <tbody> <tr> <td style="width:20em;"> <span> DSL - LSD/ULSD</span> </td> <td style="width:60em;"> <span id="CVSpan"> <span style="margin-right:10px"> <span style="float:left;padding-top:3px"> Currently:</span> <span style="float:left;width:6em;padding-top:2px; margin-left:5px; margin-right:5px;margin-top:2px;padding-bottom:2px; text-align:center; background-color:#FF0000;"> Out</span> <span style="float:left; padding-top:3px"> <b> 0</b> gallons remaining until Mon 8/1/2016 12:00:00 AM CDT</span> </span> </span> </td> <td align="right"> </td> </tr> </tbody> </table> </td> </tr> <tr style=""> <td style="width:5px;"> </td> <td> <table id="ProdDetails" rules="all" pagerstyle-visible="False" style="width: 100%" border="1"> <tbody> <tr class="table_header2"> <th scope="col"> Nominated Volume</th> <th scope="col"> Allocation Period</th> <th scope="col"> Allocation %</th> <th scope="col"> Allocation Start Amt</th> <th scope="col"> Allocation Lifted</th> <th scope="col"> Allocation Remaining</th> <th scope="col"> GPO Allowance</th> <th scope="col" class="center width8em"> GPO Remaining</th> <th scope="col"> Category Status</th> <th scope="col"> Ratability Status</th> <th scope="col"> Next Scheduled Refresh Date</th> <th scope="col"> Reference ID</th> </tr> <tr class="tablerow2"> <td class="right width8em"> 0</td> <td class="center width10em"> Custom 1 day(s)</td> <td class="right"> 100%</td> <td class="right"> 0</td> <td class="right"> 0</td> <td class="right"> 0</td> <td class="right"> 0</td> <td class="right width8em bold" id="GPO_Rmd"> 0 </td> <td class="center" style="background-color:#FF0000;"> Out</td> <td class="center" style="background-color:#0099CC;"> Below Trend</td> <td class="center width20em"> 8/1/2016 12:00:00 AM CDT</td> <td class="center width20emWordWrap"> MERC-DSL</td> </tr> </tbody> </table> </td> </tr> <tr id="GPO_Row"> <td style="width:5px;"> </td> <td> </td> </tr> </tbody> </table> </td> </tr> </tbody> </table> </td> </tr> </table> I wanted to know how to loop the table so i can get a contracts that's the " PILOT TRAVEL CENTE-122194-W/S - UNB Contract","PILOT TRAVEL CENTE-122194-W/S - UNB Fwrd Cont" and "UNB Spot" along with the data of the table also. Thanks in advance.
Ok you didn't say nothing about the language you use so i will give you example in C# //Init table element (in this case by tag name but better chose by id or Name) IWebElement tableElement = driver.FindElement(By.TagName("table")); //Init TR elements from table we found into list IList<IWebElement> trCollection = tableElement.FindElements(By.TagName("tr")); //define TD elements collection. IList<IWebElement> tdCollection; //loop every row in the table and init the columns to list foreach(IWebElement element in trCollection) { tdCollection = element.FindElements(By.TagName("td")); //now in the List you have all the columns of the row string column1 = tdCollection[0].Text; string column2 = tdCollection[1].Text; ... } if you use other language just change the syntax the logic is the same.
First matching element using CSSSelector
So, I am trying to get the test of the column Title or Names or DateTime I'm trying to get the test td element and I have tried the following using CSSSelector: div#body-inner div#ctl00_ContentPlaceHolder1_Control1_pnlList div table#ctl00_ContentPlaceHolder1_Control1_gv.gv tbody tr.item td:nth-child(4) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head id="ctl00_PageHead"> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>Employee</title> <body id="ctl00_PageBody"> <form name="aspnetForm"> <div> </div> <div> </div> <table class="global-table" cellpadding="0" cellspacing="0"> <tr class="body"> <td> <div id="body"> <div id="body-inner"> <h1> Employee Information</h1> <div id="ctl00_ContentPlaceHolder1_Control1_pnlList" style="width: 100%;"> <div class='filter'> </div> <div> <table class="gv" cellspacing="0" border="0" id="ctl00_ContentPlaceHolder1_Control1_gv" style="border-collapse: collapse;"> <tr class="header"> <th class=" nolink" scope="col"> </th> <th scope="col"> <a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Control1$gv','Sort$Phone')"> Phone</a> </th> <th class=" sorted-desc" scope="col"> <a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Control1$gv','Sort$Title')"> Title</a> </th> <th scope="col"> <a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Control1$gv','Sort$SubTitle')"> SubTitle</a> </th> <th scope="col"> <a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Control1$gv','Sort$Names')"> Names</a> </th> <th scope="col"> <a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Control1$gv','Sort$Names')"> Enames</a> </th> <th scope="col"> <a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Control1$gv','Sort$Active')"> Active</a> </th> <th scope="col"> <a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Control1$gv','Sort$DateTime')"> DateTime</a> </th> </tr> <tr class="item"> <td align="center"> <a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Control1$gv','Select$0')"> Edit</a> </td> <td align="center" style="width: 15px;"> </td> <td> Test </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> 8/23/2011 </td> </tr> <tr class="altItem"> <td align="center"> <a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Control1$gv','Select$1')"> Edit</a> </td> <td align="center" style="width: 15px;"> </td> <td> Test1 </td> <td> 1 </td> <td> Employee </td> <td> </td> <td> </td> <td> 7/31/2014 </td> </tr> <tr class="item"> <td align="center"> <a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Control1$gv','Select$2')"> Edit</a> </td> <td align="center" style="width: 15px;"> </td> <td> Test2</td> <td> 111 </td> <td> Employer </td> <td> </td> <td> </td> <td> 7/31/2013 </td> </tr> <tr class="altItem"> <td align="center"> <a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Control1$gv','Select$3')"> Edit</a> </td> <td align="center" style="width: 15px;"> </td> <td> Test3</td> <td> </td> <td> </td> <td> </td> <td> </td> <td> 8/23/2011 </td> </tr> <tr class="item"> <td align="center"> <a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Control1$gv','Select$4')"> Edit</a> </td> <td align="center" style="width: 15px;"> </td> <td> Test4</td> <td> 2 </td> <td> Employer </td> <td> </td> <td> </td> <td> 7/31/2012 </td> </tr> <tr class="altItem"> <td align="center"> <a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Control1$gv','Select$5')"> Edit</a> </td> <td align="center" style="width: 15px;"> </td> <td> Test5</td> <td> 3 </td> <td> Employer </td> <td> </td> <td> </td> <td> 7/31/2012 </td> </tr> <tr class="item"> <td align="center"> <a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Control1$gv','Select$6')"> Edit</a> </td> <td align="center" style="width: 15px;"> </td> <td> Test6 </td> <td> a </td> <td> </td> <td> </td> <td> </td> <td> 7/20/2012 </td> </tr> </table> </div> </div> </div> </div> </td> </tr> <tr class="footer"> <td> </td> </tr> </table> </form> </body> </html> the result I am getting is all the matching elements but how can I get only the td which is test? <td > test </td> <td > DEMO TEST OCT 25 </td> <td class="firefinder-match"> DEMO TEST OCT 25 </td> <td > DEMO TEST OCT 25 </td> <td > DEMO TEST OCT 25 </td> <td > DEMO TEST OCT 25 </td>
The issue lies with tr.item. There are multiple rows with that class and you are selecting all of them. Be more specific by using the first-child pseudo class so it will only select the first tr.item and not all of them.
Here is how I able to get: table#ctl00_ContentPlaceHolder1_Control1_gv.gv tbody tr.item:nth-child(3) > td:nth-of-type(3)