Horizontal Scrolling And Only Date is not working in my Data table in Asp .NET 6 Web App - datatables

Here, I am using "datatables" for my asp.net 6 web app. everything is working except "Horizontal-Scrolling" & "Only Date Property" I know that I am doing some wrong here, so please help me to solve this issue. and Thanks in advance.
My related table js file, view, index and model are given below:
"btbPending.js" is given below:
Here, I use "scrollX" for the horizontal scroll bar. But it didn't show the scroll option rather than the data table taking its extra space and looks so ugly. So where I use the "scrollX" function ?
And also I use "render: $.fn.dataTable.render.moment('x','DD/MM/YYYY')" for showing the date only property but it shows "invalid Date" in my data table. Now, also I need to know how to show this "Date-Only" property in my Data table.
var dataTable;
$(document).ready(function () {
$('#tblData').DataTable({
"scrollX": true,
});
loadDataTable();
});
function loadDataTable() {
dataTable = $('#tblData').DataTable({
"ajax": {
"url": "/Admin/BTBPending/GetAll"
},
"columns": [
{ "data": "supplierSelectList.supplier", "width": "15%" },
{ "data": "countrySelectList.country", "width": "15%" },
{ "data": "itemSelectList.item", "width": "15%" },
{ "data": "value", render: $.fn.dataTable.render.number(',', '.', 2, '$') , "width": "15%" },
{ "data": "piNo", "width": "15%" },
{ "data": "shipDate", render: $.fn.dataTable.render.moment('x','DD/MM/YYYY'), "width": "15%" },
{ "data": "buyerSelectList.buyer", "width": "10%" },
{ "data": "supplierSelectList.supplier", "width": "15%" },
{ "data": "countrySelectList.country", "width": "15%" },
{ "data": "itemSelectList.item", "width": "15%" },
{ "data": "value", render: $.fn.dataTable.render.number(',', '.', 2, '$'), "width": "15%" },
{ "data": "piNo", "width": "15%" },
{ "data": "shipDate", render: $.fn.dataTable.render.moment('x', 'DD/MM/YYYY'), "width": "15%" },
{ "data": "buyerSelectList.buyer", "width": "10%" },
]
});
}
"BTBPending" Index :
<div class="card-body">
<div class="tab-content">
<div class="tab-pane fade show active" role="tabpanel">
<div class="mb-1">
<table id="tblData" class="table table-bordered table-hover table-sm align-middle m-0" style="width:100%">
<thead class="" style="text-align:center;background-color: #17A2B8">
<tr>
<th>Supplier</th>
<th>Country</th>
<th>Item</th>
<th>Value</th>
<th>PI</th>
<th>Ship Date</th>
<th>Buyer</th>
<th>Supplier</th>
<th>Country</th>
<th>Item</th>
<th>Value</th>
<th>PI</th>
<th>Ship Date</th>
<th>Buyer</th>
</tr>
</thead>
</table>
</div>
</div>
</div>
</div>

Related

Uncaught TypeError: f[0] is undefined

One of my Datatable throws an error like on the title, but my other datatable running smoothly, I don't understand why I keep getting the error, I triple check every close tags, brackets etc. I also tried to minimize my data array for a second to check whats the problem and still I dont get the problem. here's my code,
Javascript:
$("#MyNameOfDatatable").DataTable({
"language": {
"emptyTable": "No data available"
},
"dom": "<'top'f>rtp<'bottom'l>",
"searching": false,
"data": res.data, //The data came from my ajax server side
"destroy": true,
"processing": true,
"serverside": true,
"info": true,
"autoWidth": false,
"lengthMenu": [[10, 20, 50, -1], [10, 20, 50, "All"]],
"columns": [
{
data: null, "orderable": true,
"render": function (data, type, full, meta) {
return meta.row + 1;
}
},
{ data: "name", orderable: false }
]
})
Html:
<table class="table table-hover table-bordered table-striped cell-border" id="MyNameOfDatatable">
<thead>
<tr class="bg-primary text-light">
<th>SEQ</th>
<th>Name</th>
</tr>
</thead>
</table>
Sample data that server throws to me:
{
"data": [
{
"name": "Lorem Ipsum",
}
]
}

How Can I get a specific array id from a v-for loop input and pass it into a data store?

Here is my code
<template>
<div class="w-full bg-white mt-13">
<div class="flex flex-col mx-12 mt-8">
<div>
<table class="w-full h-auto my-12 border border-collapse table-auto">
<tr class="border">
<th v-for="i in columns" :key="i.name" class="border">
{{ i.title }}
</th>
</tr>
<tr v-for="p in getParticipants" :key="p.id">
<td class="border"></td>
<td class="border">{{ p.fullName }}</td>
<td class="border">{{ p.phone }}</td>
<td class="border">{{ p.participantId }}</td>
<td
v-for="(btn, index) in buttonCheckAtt"
:key="index"
class="border select-attendance"
>
<div>
<button
:unique-key="true"
class="w-full h-16 text-transparent"
:class="[
btn.className,
{ selected: selectedIndex === index },
]"
#click="selectedIndex = index"
>
{{ btn.btnText }}
</button>
</div>
</td>
<td class="w-16 border">
<input
ref="p.participantId"
#change="updateRemarks()"
v-model="note"
/>
</td>
</tr>
<tr></tr>
</table>
</div>
</div>
</div>
</template>
<script>
import Button from "#/components/form/Button.vue";
import { mapGetters } from "vuex";
const columns = [
{
title: "#",
},
{
title: "Name",
},
{
title: "Phone Number",
},
{
title: "ID",
},
{
title: "P",
},
{
title: "A",
},
{
title: "AP",
},
{
title: "L",
},
{
title: "Remarks",
},
];
const data = [];
export default {
components: { Button },
data() {
return {
note: null,
participantId: "abc-1",
};
},
methods: {
updateRemarks() {
let data = {
participants: [
{
participantId: this.participantId,
attendance: {
status: this.status,
note: this.note,
markBy: "organizer-id",
markMethod: "manual",
},
},
],
};
this.$store.dispatch(
"$_studyGroup/addRemarks",
{ participantData: data },
{ root: true }
);
},
},
computed: {
...mapGetters({
getParticipants: "$_studyGroup/getParticipants",
}),
},
mounted() {
this.$store.dispatch("$_studyGroup/getParticipants", {}, { root: true });
},
};
</script>
I want to use this input to this input to make patch request into my getParticipants api however it requires me to pass the participantId into the participants data in order to make patch request and I have no idea how to retrieve that participantId from the v-for loop
<input
ref="p.participantId"
#change="updateRemarks()"
v-model="note"
/>
and down below is what my getParticipants api looks like I want to pass the abc-1 and abc-2 id into the attendance in order to make patch request
getParticipants = [
{
"status": "join",
"createdAt": "2022-09-20T07:30:06.753Z",
"calendarId": "6553c8ea-0139-4802-b5d6-127e44b95412",
"email": "john#gmail.com",
"fullName": "John",
"participantId": "abc-1",
"attendance": {
"participantId" : {{participantId}},
"markBy": "organizer-id",
"markMethod": "manual",
"note": "Because of traffic jam",
"status": "Present"
},
{
"status": "join",
"createdAt": "2022-09-20T07:30:06.753Z",
"calendarId": "6553c8ea-0139-4802-b5d6-127e44b95412",
"email": "chris#gmail.com",
"fullName": "Chris",
"participantId": "abc-2",
"attendance": {
"participantId" : {{participantId}},
"markBy": "organizer-id",
"markMethod": "manual",
"note": "Because of traffic jam",
"status": "Late"
},]

How to pass data from api to a vue method

I have the following data
"meta": {
"total_count": 17
},
"items": [
{
"id": 80,
"meta": {
"type": "",
"detail_url": "",
"html_url": "",
"slug": "",
"first_published_at": ""
},
"title": "some title",
"image": {
"id": 46,
"meta": {
"type": "wagtailimages.Image",
"detail_url": "some url",
"download_url": "/media/original_images/im.png"
},
"title": "im.png",
"width": 100,
"height": 80
},
}
]
Now if I try to get the width of an image inside :src attribute it works but if I try to pass it to a method it doesn't
, a snippet of the HTML code is:
<li v-for="(item, index) in response_data">
<a v-bind:href="item.meta.html_url">
<div class="icon" :class="classObject(item.image.width,item.image.height)">
<img v-if="item.image" v-bind:src="item.image.meta.download_url" alt=""/>
<img v-else src="{% static 'images/logo-dummy.svg' %}" alt="logo-dummy.svg"/>
</div>
where classObject is a method that compares the width and the height, I tried also to make it a computed property, the above code doesn't work but if for example I wrote
<img v-if="item.image" v-bind:src="item.image.width">
I can see the width inside the src attribute, what am I doing wrong?
Edit: classObject code is the following
classObject: function (width, height) {
if (width > height) return 'icon1'
return 'icon2'
},
The problem I encountered was because sometimes there was no image at all, otherwise the code is correct so now I just check if there's an image like this
<div v-if="item.image" :class="classObject(item.image.width,item.image.height)">
<img v-bind:src="item.image.meta.download_url" alt=""/>
</div>

How to populate dataTable with multidimensional array with dynamic key

I have an array like this:
"products": {
"111111": {
"date": "01.01.2018",
"amount": 10,
"user_id": "user1",
}
"222222": {
"date": "10.10.2018",
"amount": 15,
"user_id": "user1,
}
}
"111111" and "222222" are IDs and they are generated dynamically, depending on selected user.
I want to display the following columns: product_id, date, amount, user_id
Is there a way to populate the table when a column name is dynamic ?
Thanks!
If you can change the format of your json slightly, you can easily just assign it as an array for the data attribute when initializing datatables. If that is not an option, you could just parse it into the below format by iterating through the key-value pairs and adding that data to an array of jsons.
$(document).ready(function() {
var products = [{
"product_id": "111111",
"date": "01.01.2018",
"amount": 10,
"user_id": "user1",
},
{
"product_id": "222222",
"date": "10.10.2018",
"amount": 15,
"user_id": "user1",
}
];
var table = $('#example').DataTable({
data: products,
columnDefs: [
{ targets: 0, data: "product_id"},
{ targets: 1, data: "date" },
{ targets: 2, data: "amount" },
{ targets: 3, data: "user_id" }
]
});
});
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<link href="https://nightly.datatables.net/css/jquery.dataTables.css" rel="stylesheet" type="text/css" />
<script src="https://nightly.datatables.net/js/jquery.dataTables.js"></script>
<div class="container">
<table id="example" class="display nowrap" width="100%">
<thead>
<tr>
<th>product_id</th>
<th>date</th>
<th>amount</th>
<th>user_id</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>

Vue Recommendation for Storing Variable Inside Templated Looping Structure?

Given the following code
// Assuming records is an array of objects
// Record is of the form {"fname": "John", "lname": "Smith", "dob": 1499994497871}
// Field mapper is
[
{
"name": "fname",
"label": "First Name",
"render": function(val){ return val}
},
{
"name": "lname",
"label": "Last Name",
"render": function(val){ return val}
},
{
"name": "dob",
"label": "Date of Birth",
"render": function(val){ return new Date(val).toDateString()}
}
]
// Assume findField returns the mapper instance for a given record in the records array
<div v-for="record in records">
<div>
<table class="table">
<tbody>
<tr v-for="(value, key) in record">
<th v-if="findField(key)">{{ findField(key).label }}</th>
<td v-if="findField(key)">{{ findField(key).render(value) }}</td>
</tr>
</tbody>
</table>
</div>
</div>
I'm making four calls to findField().
What's the Vue recommendation to storing variables inside looping structure?
In other words, after the first tr, something like:
let localInstance = findField(key)
One way is to use a component.
Vue.component("row", {
props:["map", "value"],
template:`
<tr>
<th v-if="map">{{ map.label }}</th>
<td v-if="map">{{ map.render(value) }}</td>
</tr>
`
})
And modify your template to use it.
<tr is="row" v-for="(value, key) in record"
:map="findField(key)"
:value="value">
</tr>
console.clear()
const fieldMap = [{
"name": "fname",
"label": "First Name",
"render": function(val) {
return val
}
},
{
"name": "lname",
"label": "Last Name",
"render": function(val) {
return val
}
},
{
"name": "dob",
"label": "Date of Birth",
"render": function(val) {
return new Date(val).toDateString()
}
}
]
const records = [{
"fname": "John",
"lname": "Smith",
"dob": 1499994497871
},
{
"fname": "John",
"lname": "Smith",
"dob": 1499994497871
},
{
"fname": "John",
"lname": "Smith",
"dob": 1499994497871
}
]
Vue.component("row", {
props: ["map", "value"],
template: `
<tr>
<th v-if="map">{{ map.label }}</th>
<td v-if="map">{{ map.render(value) }}</td>
</tr>
`
})
new Vue({
el: "#app",
data: {
records
},
methods: {
findField(key) {
return fieldMap.find(m => m.name === key)
}
},
})
<script src="https://unpkg.com/vue#2.2.6/dist/vue.js"></script>
<div id="app">
<div v-for="record in records">
<div>
<table class="table">
<tbody>
<tr is="row" v-for="(value, key) in record" :map="findField(key)" :value="value">
</tr>
</tbody>
</table>
</div>
</div>
</div>