TreeGrid extension for DataTables - how to get a checkbox added after name? - datatables

I found a TreeGrid extension for DataTables:
https://homfen.github.io/dataTables.treeGrid.js/
but instead of the name I would like to add a column between name and position and place a checkbox here.
However when I do this e.g.:
var columns = [
{
title: '',
target: 0,
className: 'treegrid-control',
data: function (item) {
if (item.children) {
return '<span>+<\/span>';
}
return '';
}
},
{
title: 'Name',
target: 1,
data: function (item) {
return item.name;
}
},
{
defaultContent: '',
target: 2,
className: 'select-checkbox',
function(item) {
return item;
}
},
{
title: 'Position',
target: 3,
data: function (item) {
return item.position;
}
},
{
title: 'Office',
target: 4,
data: function (item) {
return item.office;
}
},
{
title: 'Extn.',
target: 5,
data: function (item) {
return item.extn;
}
},
{
title: 'Start date',
target: 6,
data: function (item) {
return item.start;
}
},
{
title: 'Salary',
target:7,
data: function (item) {
return item.salary;
}
}
];
I get an extra column but when checking the parent does not select all underlying children rows.
Anyone have an idea how to establish this?
Edit: updated the columns definition.
When I add a button to read the selected values e.g.:
dom: 'Bfrtip',
select:true,
buttons: [
{
text: 'Alert selected',
action: function(e, dt, node, config) {
var data = table.rows({
selected: true
}).data().toArray();
var i;
var text = new Array();
for (i = 0; i < data.length; i++) {
text.push(data[i].name);
}
alert("you selected: " + text.join(",") );
console.log("text---" + text.join(","));
}
}
]
the table starts to behave oddly for example: the selection of underlying children stops.

Related

Vue.js multiple search with checkboxes

I'm trying to create a search form with input text fields and checkboxes.
Right now I have the search working for text fields but I can't make it work for checkboxes.
I'm totally new with vue.js so probably I'm missing some basic stuff.
The problem is in the computed part in the last filter, with the categories checkboxes:
computed: {
filteredExperiences() {
return this.experiences.filter( item => {
return (
item.destination.toLowerCase().indexOf(this.searchDestinations.toLowerCase()) > -1
&& item.title.toLowerCase().indexOf(this.searchExperiences.toLowerCase()) > -1
&& item.categories.map(cat => cat.title.toLowerCase()).indexOf(this.checkedCategories.toLowerCase()) > -1
)
})
}
}
So searchDestinations and searchExperiences work fine but search by categories doesn't.
Any idea why? What am I doing wrong?
This is the full code:
var app = new Vue({
el: '#app',
data: {
destinations: [{
title: 'Madrid',
slug: 'madrid'
},
{
title: 'London',
slug: 'london'
},
{
title: 'Chicago',
slug: 'chicago'
},
{
title: 'Los Angeles',
slug: 'los-angeles'
}
],
categories: [{
title: 'Concerts',
slug: 'concerts'
},
{
title: 'Museums',
slug: 'museums'
},
{
title: 'Theaters',
slug: 'theaters'
},
{
title: 'Cinemas',
slug: 'cinemas'
}
],
experiences: [{
id: 1,
title: 'Bruce Springsteen Live Madrid',
destination: 'Madrid',
categories: ['concerts', 'theaters'],
duration: '2h'
},
{
id: 2,
title: 'Mulan 2 the movie',
destination: 'London',
categories: ['cinemas', 'theaters'],
duration: '2h'
},
{
id: 3,
title: 'British Museum',
destination: 'London',
categories: ['museums'],
duration: '3h'
}
],
checkedCategories: [],
searchDestinations: '',
searchExperiences: ''
},
computed: {
filteredExperiences() {
return this.experiences.filter(item => {
return (item.destination.toLowerCase().indexOf(this.searchDestinations.toLowerCase()) > -1 && item.title.toLowerCase().indexOf(this.searchExperiences.toLowerCase()) > -1)
})
}
}
});
Here is the codepen:
See the Pen
vue.js filtered multi-search by Javier (#oterox)
on CodePen.
the problem is here:
&& item.categories.map(cat => cat.title.toLowerCase()).indexOf(this.checkedCategories.toLowerCase()) > -1
like this it should work:
computed: {
filteredExperiences() {
return this.experiences.filter( item => {
if(item.destination.indexOf(this.searchDestinations) < 0) { return false }
if (item.title.indexOf(this.searchExperiences) < 0 ) { return false }
let matchedCats = item.categories.filter(cat => {
let hasCat = this.checkedCategories.findIndex(checkCat => cat.toLowerCase() === checkCat.toLowerCase());
return hasCat > -1;
})
if(this.checkedCategories.length > 0 && matchedCats.length < 1)
{
return false;
}
return true;
})
}
}

Trying to refresh label when click on it. Cannot read property '_meta' of undefined"

I'm trying that the percentages in a pie-chart refresh when clicking in a legend to hide data.
So far, I can display the chart with percentages, but they don't change if I hide one of the legends.
This is the chart: initial chart
This is how looks after the click: after click
We expect that instead 55.6%, it shows 100%.
This is my code so far:
<script>
import {Pie} from "vue-chartjs";
import ChartJsPluginDataLabels from 'chartjs-plugin-datalabels';
export default {
extends: Pie,
ChartJsPluginDataLabels,
props: {
data: Array,
bg: Array,
labels: Array
},
data() {
return {
}
},
computed: {
chartData() {
return this.data
},
bgData() {
return this.bg
},
total() {
return this.data.reduce((a, b) => a + (b || 0), 0)
}
},
methods: {
renderPieChart() {
this.renderChart({
labels: this.labels,
datasets: [
{
label: "Data One",
backgroundColor: this.bgData,
data: this.chartData,
hoverBackgroundColor: "#f78733"
}
]
}, {
responsive: true,
plugins: {
datalabels: {
formatter: (value) => {
let sum = this
.$refs.canvas.getContext('2d').dataset._meta[1].total; //use this.total to fix percentages
let percentage = (value * 100 / sum).toFixed(1) + "%";
return percentage;
},
color: '#fff'
}
}
})
console.log()
},
updateSelected(point, event) {
const item = event[0]
this.selected = {
index: item._index,
value: this
.chartData
.datasets[0]
.data[item._index]
}
}
},
watch: {
bg: function () {
this.renderPieChart();
},
data: function () {
this.renderPieChart();
}
},
}
</script>
In order to obtain the expected result, you should define plugins.datalabels.formatter as follows:
formatter: (value, context) => {
return (value * 100 / context.dataset._meta[0].total).toFixed(1) + "%";
}
new Chart(document.getElementById("myChart"), {
type: "pie",
data: {
labels: ['Savings', 'House'],
datasets: [{
label: "Data One",
backgroundColor: ['#4e9258', '#64e986'],
data: [9, 7],
hoverBackgroundColor: "#f78733"
}]
},
options: {
plugins: {
datalabels: {
formatter: (value, context) => {
return (value * 100 / context.dataset._meta[0].total).toFixed(1) + "%";
},
color: '#fff'
}
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels"></script>
<canvas id="myChart" height="100"></canvas>

Reset Vue Bootstrap Table

i am using vue-bootstrap4-table in my application i have a custom input though which i search and populate the data,now i need to build a feature in which their is a cross button inside the search field and on clicking on it it should reset the table to empty state here is my code
<template>
<auto-complete
#autocomplete-result-selected="setCustomer"
placeholder="Enter Customer name"
:selected="selectedCustomer"
:styles="{width: 'calc(100% - 10px)'}"
index="locations"
attribute="name"
>
<template slot-scope="{ hit }">
<span>
{{ hit.customer.company && hit.customer.company + ' - ' }}{{ hit.customer.fname }}
{{ hit.customer.lname }}
</span>
</template>
</auto-complete>
<i class="fas fa-times" #click="clearTable()" v-show="selectedCustomer"></i>
</div>
</div>
</template>
<script>
import http from "../../helpers/api.js";
import AutoComplete from "../autocomplete/Autocomplete";
import axios from "axios";
import VueBootstrap4Table from "vue-bootstrap4-table";
export default {
components: {
"auto-complete": AutoComplete,
VueBootstrap4Table
},
computed: {},
data() {
return {
value: "",
selectedCustomer: "",
selectedFirstName: "",
selectedLastName: "",
selectedFields: [
{ name: "Invoice", value: "invoices" },
{
name: "Estimate",
value: "workorder_estimates"
}
],
filters: [
{ is_checked: true, value: "invoices", name: "Invoice" },
{ is_checked: true, value: "workorder_estimates", name: "Estimate" }
],
selectedFilters: [],
estimateChecked: false,
invoiceChecked: false,
config: {
card_mode: false,
show_refresh_button: false,
show_reset_button: false,
global_search: {
placeholder: "Enter custom Search text",
visibility: false,
case_sensitive: false
},
pagination: true,
pagination_info: false,
per_page: 10,
rows_selectable: true,
server_mode: true,
preservePageOnDataChange: true,
selected_rows_info:true
},
classes: {},
rows: [],
columns: [
{
label: "TYPE",
name: "type"
},
{
label: "ID",
name: "distinction_id"
},
{
label: "CUSTOMER NAME",
name: "full_name"
},
{
label: "SERVICE DATE",
name: "service_date"
},
{
label: "ADDRESS",
name: "address1"
},
{
label: "CREATE DATE",
name: "created_at"
}
],
queryParams: {
sort: [],
filters: [],
global_search: "",
per_page: 10,
page: 1
},
selected_rows: [],
total_rows: 0
};
},
methods: {
onNameSearch() {
this.selectedFilters = ["invoices", "workorder_estimates"];
this.fetchData();
},
clearTable(){
this.rows=[];
console.log(this.config.selected_rows_info);
this.config.selected_rows_info=false;
},
onChangeQuery(queryParams) {
console.log(queryParams);
this.queryParams = queryParams;
this.fetchData();
},
onRowClick(payload) {
console.log(payload);
},
setCustomer(selectedResult) {
this.selectedCustomer = selectedResult.customer.company
? `${selectedResult.customer.company + " - "}${
selectedResult.customer.fname
} ${selectedResult.customer.lname}`
: `${selectedResult.customer.fname} ${selectedResult.customer.lname}`;
this.selectedFirstName = selectedResult.customer.fname;
this.selectedLastName = selectedResult.customer.lname;
},
changeCheck(event, index, value) {
var checked = event.target.checked;
switch (value) {
case "invoices":
if (checked) {
this.selectedFields.push({ name: "Invoice", value: "invoices" });
this.invoiceChecked = true;
var data = this.filters[index];
data.is_checked = true;
Vue.set(this.filters, data, index);
} else {
var index = this.selectedFields.findIndex(
item => item.value === value
);
this.selectedFields.splice(index, 1);
this.invoiceChecked = false;
var data = this.filters[index];
data.is_checked = false;
Vue.set(this.filters, data, index);
}
break;
case "workorder_estimates":
if (checked) {
this.selectedFields.push({
name: "Estimate",
value: "workorder_estimates"
});
var data = this.filters[index];
data.is_checked = true;
Vue.set(this.filters, data, index);
} else {
var index = this.selectedFields.findIndex(
item => item.value === value
);
this.selectedFields.splice(index, 1);
this.estimateChecked = false;
var data = this.filters[index];
data.is_checked = false;
Vue.set(this.filters, data, index);
}
break;
}
},
removeFilter(index, value) {
switch (value) {
case "workorder_estimates":
this.selectedFields.splice(index, 1);
this.estimateChecked = false;
var i = this.filters.findIndex(item => item.value === value);
var data = this.filters[i];
data.is_checked = false;
Vue.set(this.filters, data, i);
break;
case "invoices":
this.selectedFields.splice(index, 1);
this.invoiceChecked = false;
var i = this.filters.findIndex(item => item.value === value);
var data = this.filters[i];
data.is_checked = false;
Vue.set(this.filters, data, i);
break;
}
},
updateFilters() {
this.selectedFilters = [];
this.selectedFields.forEach(element => {
this.selectedFilters.push(element.value);
});
if(this.selectedFilters.length == 0){
this.selectedFilters = ['invoices', 'workorder_estimates'];
}
this.fetchData();
},
async fetchData() {
var final = [];
try {
var result = await http.post("/estimate-invoice-search", {
type: this.selectedFilters,
search: {
value: this.selectedFirstName + " " + this.selectedLastName
},
per_page: this.queryParams.per_page,
page: this.queryParams.page,
sort: this.queryParams.sort
});
this.total_rows = result.recordsFiltered;
result.data.forEach(element => {
element.full_name = element.first_name + " " + element.last_name;
final.push(element);
});
this.rows = final;
} catch (error) {
console.log(error);
}
}
},
mounted() {}
};
</script>
now the method named clearTable here i want to reset my table to the point lie we see on page refresh in the method i used this.rows=[]; this clears all the rows which is exactly what i want but the text which shows the number of rows is still their and i cant seem to remove it please view the below image for clarification
i read the documentation on link but cant seem to find a solution for hiding the text, is their any way?
It looks like you're using total_rows as the variable for the number of rows in your template here:
<span>{{total_rows}}</span> Result(s)
The only spot in code that you set this value is in fetchData() where you set:
this.total_rows = result.recordsFiltered;
You can either:
1) Make total_rows a computed property (recommended) that returns the length of rows (I believe rows is always the same length as total_rows from your code)
-or-
2) Just set this.total_rows = 0; in your clearTable() function

DataTable initialization events fired before table loaded

I have a page with a jQuery Datatable being from Ajax, and drawn with the npm treeGrid plugin. The table is drawn fine, but i'm trying to catch the end of the table loading to do some stuff (get additional data to be ready when the tree is expanded). The init.dt event or the initComplete option are fired before the table is drawn. If i put an alert in the event it is shown after the table top and bottom are drawn but before the data is rendered. And if i try to access the table data it is undefined.
Important: I get the same behavior if I remove the treeGrid object from the DataTable options. I even removed the treeGrid javascript...
So my question is: how can i have code run when everything is really ready?
Below are two implementations of the Datatble, with either init.dt or initComplete. I am also adding the columns object. Below are two screenshots of the alert and then the table drawn.
$(document).ready(function () {
$('#mainTable')
.on('init.dt', function () {
alert('Table initialisation complete')
})
.DataTable({
"ajax": '/API/RRate',
'treeGrid': {
'left': 20,
'expandIcon': '<span>+</span>',
'collapseIcon': '<span>-</span>'
},
"columns": columns
});
});
$(document).ready(function () {
$('#mainTable')
.DataTable({
"ajax": '/API/RRate',
'treeGrid': {
'left': 20,
'expandIcon': '<span>+</span>',
'collapseIcon': '<span>-</span>'
},
"columns": columns,
"initComplete": function (settings, json) {
alert('Table initialisation complete')
}
});
});
var columns = [
{
title: '',
target: 0,
className: 'treegrid-control',
data: function (item) {
if (item.children) {
return '<span>+</span>';
}
return '';
}
},
{
title: 'Name',
target: 1,
data: function (item) {
return item.name;
}
},
{
title: 'HeadCount',
target: 2,
data: function (item) {
return item.headCount;
}
},
{
title: 'Responded',
target: 3,
data: function (item) {
return item.responded;
}
},
{
title: 'Percentage',
target: 4,
data: function (item) {
return item.percentage;
}
},
{
title: 'InProcess',
target: 5,
data: function (item) {
return item.inProcess;
}
},
];
OK, i found how to do this:
function AfterLoad()
{
table.rows().each(function () {
console.log(this.cells(0));
var rowData = this.data();
console.log(rowData[0]);
});
}
$(document).ready(function () {
table = $('#mainTable')
.DataTable({
"ajax": '/API/RRate',
'treeGrid': {
'left': 20,
'expandIcon': '<span>+</span>',
'collapseIcon': '<span>-</span>'
},
"columns": columns,
"initComplete": function (settings, json) {
AfterLoad();
}
});
});

Sencha touch 2 list with buttons

Need to create a list in sencha touch 2 with items like
label1
label2
button1 button2 button3
label1
label2
button1 button2 button3
on clicking the button a poppup should come pointing it.
I know I need to use Dataview for creating the list. But I have no idea of creating such a layout using dataview. any help would greatly appreciated.
Here is the code required to create your layout.
Ext.Loader.setConfig({
enabled: true
});
Ext.application({
launch: function () {
Ext.define('MyListItem', {
extend: 'Ext.dataview.component.DataItem',
requires: ['Ext.Button'],
xtype: 'mylistitem',
config: {
labelPanel:{
itemId:'labelpanel',
layout:'hbox',
defaults:{
//flex:1,
xtype:'label'
}
},
fnameLabel: true,
lnameLabel: {
style:'margin-left:5px'
},
horizontalPanel: {
layout: 'hbox',
defaults:{
xtype:'button',
flex:1
},
items: [{
text: 'First Name',
btnId:1
}, {
text: 'Last Name',
btnId:2
}, {
text: 'Age',
btnId:3
}]
},
dataMap: {
getFnameLabel: {
setHtml: 'fname'
},
getLnameLabel: {
setHtml: 'lname'
}
},
layout: 'vbox'
},
applyFnameLabel: function (config) {
return Ext.factory(config, Ext.Label, this.getFnameLabel());
},
updateFnameLabel: function (newFnameLabel, oldFnameLabel) {
if (oldFnameLabel) {
this.down('panel[itemId="labelpanel"]').remove(oldFnameLabel);
}
if (newFnameLabel) {
this.down('panel[itemId="labelpanel"]').add(newFnameLabel);
}
},
applyLnameLabel: function (config) {
return Ext.factory(config, Ext.Label, this.getLnameLabel());
},
updateLnameLabel: function (newLnameLabel, oldLnameLabel) {
if (oldLnameLabel) {
this.down('panel[itemId="labelpanel"]').remove(oldLnameLabel);
}
if (newLnameLabel) {
this.down('panel[itemId="labelpanel"]').add(newLnameLabel);
}
},
applyLabelPanel: function (config) {
return Ext.factory(config, Ext.Panel, this.getLabelPanel());
},
updateLabelPanel: function (newLabelPanel, oldLabelPanel) {
if (oldLabelPanel) {
this.remove(oldLabelPanel);
}
if (newLabelPanel) {
this.add(newLabelPanel);
}
},
applyHorizontalPanel: function (config) {
return Ext.factory(config, Ext.Panel, this.getHorizontalPanel());
},
updateHorizontalPanel: function (newHorizontalPanel, oldHorizontalPanel) {
if (oldHorizontalPanel) {
this.remove(oldHorizontalPanel);
}
if (newHorizontalPanel) {
//console.info(newHorizontalPanel.down('button[btnId=1]'));
newHorizontalPanel.down('button[btnId=1]').on('tap', this.onButtonTap, this);
newHorizontalPanel.down('button[btnId=2]').on('tap', this.onButtonTap, this);
newHorizontalPanel.down('button[btnId=3]').on('tap', this.onButtonTap, this);
this.add(newHorizontalPanel);
}
},
onButtonTap: function (button, e) {
var record = this.getRecord();
var id = button.config.btnId;
switch(id){
case 1: var value = record.get('fname');break;
case 2: var value = record.get('lname');break;
case 3: var value = record.get('age').toString();break;
}
Ext.Msg.alert(value,"The value is: " +value);
}
});
Ext.create('Ext.DataView', {
fullscreen: true,
store: {
fields: ['fname','lname','age'],
data: [{
fname: 'Jamie',
lname: 'Avins',
age: 100
}, {
fname: 'Rob',
lname: 'Dougan',
age: 21
}, {
fname: 'Tommy',
lname: 'Maintz',
age: 24
}, {
fname: 'Jacky',
lname: 'Nguyen',
age: 24
}, {
fname: 'Ed',
lname: 'Spencer',
age: 26
}]
},
useComponents: true,
defaultType: 'mylistitem'
});
}
});
This fiddle should give you an idea. Read this link from the sencha blog. It explains the code.