【发布时间】:2021-09-04 02:41:54
【问题描述】:
请看我下面的代码。我有一个YearEligible 列,每个记录的单元格中有Y 和N 值。基于此,我想确定单击Get rows 按钮时Year 列发送什么值。
详细说明我的要求:
当我单击Get rows 按钮时,我可以在console.log 中看到以下内容,看起来不错:
[{
available: true,
boundindex: 0,
firstname: "Mayumi",
lastname: "Nagase",
price: 3.25,
productname: "Espresso con Panna",
quantity: 6,
total: 19.5,
uid: 0,
uniqueid: "2128-24-28-17-311629",
visibleindex: 0,
yeareligible: "Y",
yearValue: "2011"
}]
但是,假设我选择Year Eligible 列的值为N 的记录并单击Get rows 按钮,我会在console.log 中看到以下内容:
[{
available: true,
boundindex: 1,
firstname: "Regina",
lastname: "Davolio",
price: 3.3,
productname: "Doubleshot Espresso",
quantity: 8,
total: 26.4,
uid: 1,
uniqueid: "2917-25-23-18-212828",
visibleindex: 1,
yeareligible: "N"
}]
我的观察:
上面的console.log 没有为yearValue 返回任何东西,这是有道理的,因为我没有选择任何东西。
我的问题:
-
在上述情况下,是否可以将默认值
yearValue作为-1返回?基本上,当用户选择Year Eligible值设置为N的记录时,我想将yearValue包含为-1。 -
在
Year列下Year Eligible列的值为N的单元格上显示不适用?
var data = new Array();
var firstNames = [
"Andrew", "Nancy", "Shelley", "Regina", "Yoshi", "Antoni", "Mayumi", "Ian", "Peter", "Lars", "Petra", "Martin", "Sven", "Elio", "Beate", "Cheryl", "Michael", "Guylene"];
var lastNames = [
"Fuller", "Davolio", "Burke", "Murphy", "Nagase", "Saavedra", "Ohno", "Devling", "Wilson", "Peterson", "Winkler", "Bein", "Petersen", "Rossi", "Vileid", "Saylor", "Bjorn", "Nodier"];
var yearEligible = [
"Y", "N", "Y", "Y", "Y", "Y", "N", "N", "N", "N", "Y", "N", "N", "N", "Y", "N", "Y", "N"];
var productNames = [
"Black Tea", "Green Tea", "Caffe Espresso", "Doubleshot Espresso", "Caffe Latte", "White Chocolate Mocha", "Cramel Latte", "Caffe Americano", "Cappuccino", "Espresso Truffle", "Espresso con Panna", "Peppermint Mocha Twist"];
var priceValues = [
"2.25", "1.5", "3.0", "3.3", "4.5", "3.6", "3.8", "2.5", "5.0", "1.75", "3.25", "4.0"];
for (var i = 0; i < 10; i++) {
var row = {};
var productindex = Math.floor(Math.random() * productNames.length);
var price = parseFloat(priceValues[productindex]);
var quantity = 1 + Math.round(Math.random() * 10);
row["firstname"] = firstNames[Math.floor(Math.random() * firstNames.length)];
row["lastname"] = lastNames[Math.floor(Math.random() * lastNames.length)];
row["yeareligible"]= yearEligible[Math.floor(Math.random() * lastNames.length)];
row["productname"] = productNames[productindex];
row["available"] = !!(i % 2);
row["price"] = price;
row["quantity"] = quantity;
row["total"] = price * quantity;
data[i] = row;
}
var source = {
localdata: data,
datatype: "array"
};
var dataAdapter = new $.jqx.dataAdapter(source, {
loadComplete: function (data) {},
loadError: function (xhr, status, error) {}
});
$("#jqxgrid").jqxGrid({
theme: 'energyblue',
width: 500,
autoheight: true,
editable: true,
source: dataAdapter,
columns: [{
text: 'Available',
datafield: 'available',
width: 100,
columntype: 'checkbox'
}, {
text: 'First Name',
datafield: 'firstname',
width: 100
}, {
text: 'Last Name',
datafield: 'lastname',
width: 100
}, {
text: 'Year Eligible',
datafield: 'yeareligible',
width: 100
},
{
text: 'Year',width: 120,datafield:'yearValue' , columntype: 'dropdownlist', editable: true,
createeditor: function (row, column, editor) {
var list = ['2010', '2011', '2012' ,'2013','2014','2015','2016','2017','2018','2019','2020','2021'];
editor.jqxDropDownList({ autoDropDownHeight: true, source: list, selectedIndex: 0 });
}
},
{
text: 'Product',
datafield: 'productname',
width: 180
}, {
text: 'Quantity',
datafield: 'quantity',
width: 80,
cellsalign: 'right'
}, {
text: 'Unit Price',
datafield: 'price',
width: 90,
cellsalign: 'right',
cellsformat: 'c2'
},
{
text: 'Total',
datafield: 'total',
width: 100,
cellsalign: 'right',
cellsformat: 'c2'
}]
});
$('#jqxbutton').click(function () {
var rows = $('#jqxgrid').jqxGrid('getrows');
var selectedRows = rows.filter(x => x.available)
console.log(selectedRows)
//alert(rows);
});
<link href="https://jqwidgets.com/public/jqwidgets/styles/jqx.energyblue.css" rel="stylesheet"/>
<link href="https://jqwidgets.com/public/jqwidgets/styles/jqx.base.css" rel="stylesheet"/>
<script src="https://jqwidgets.com/public/jqwidgets/jqx-all.js"></script>
<div id="jqxgrid"></div>
<input type="button" style="margin: 50px;" id="jqxbutton" value="Get rows" />
已编辑:根据 Calculuswhiz 的回答测试 2016 和 aggregate 的默认值:
let firstNames = [
"Andrew", "Nancy", "Shelley", "Regina", "Yoshi", "Antoni", "Mayumi", "Ian",
"Peter", "Lars", "Petra", "Martin", "Sven", "Elio", "Beate", "Cheryl", "Michael", "Guylene"
];
let lastNames = [
"Fuller", "Davolio", "Burke", "Murphy", "Nagase", "Saavedra", "Ohno",
"Devling", "Wilson", "Peterson", "Winkler", "Bein", "Petersen", "Rossi",
"Vileid", "Saylor", "Bjorn", "Nodier"
];
let yearEligible = [
"Y", "N", "Y", "Y", "Y", "Y", "N", "N", "N", "N", "Y", "N", "N", "N", "Y", "N", "Y", "N"
];
let productNames = [
"Black Tea", "Green Tea", "Caffe Espresso", "Doubleshot Espresso",
"Caffe Latte", "White Chocolate Mocha", "Cramel Latte", "Caffe Americano",
"Cappuccino", "Espresso Truffle", "Espresso con Panna", "Peppermint Mocha Twist"
];
let priceValues = [
"2.25", "1.5", "3.0", "3.3", "4.5", "3.6", "3.8", "2.5", "5.0", "1.75", "3.25", "4.0"
];
let data = [];
for (let i = 0; i < 10; i++)
{
let productindex = Math.floor(Math.random() * productNames.length);
let price = parseFloat(priceValues[productindex]);
let quantity = 1 + Math.round(Math.random() * 10);
let row = {
firstname : firstNames[Math.floor(Math.random() * firstNames.length)],
lastname : lastNames[Math.floor(Math.random() * lastNames.length)],
yeareligible : yearEligible[Math.floor(Math.random() * lastNames.length)],
productname : productNames[productindex],
available : !!(i % 2),
price : price,
quantity : quantity,
total : price * quantity
};
data.push(row);
}
let source = {
localdata: data,
datatype: "array"
};
let dataAdapter = new $.jqx.dataAdapter(source,
{
loadComplete: function(data) {},
loadError: function(xhr, status, error) {}
});
$("#jqxgrid").jqxGrid(
{
theme: 'energyblue',
width: 500,
autoheight: true,
editable: true,
source: dataAdapter,
columns: [{
text: 'Available',
datafield: 'available',
width: 100,
columntype: 'checkbox'
}, {
text: 'First Name',
datafield: 'firstname',
width: 100
}, {
text: 'Last Name',
datafield: 'lastname',
width: 100
}, {
text: 'Year Eligible',
datafield: 'yeareligible',
width: 100
},
{
text: 'Year',
width: 120,
datafield: 'yearValue',
columntype: 'dropdownlist',
editable: true,
createeditor: function(row, column, editor)
{
let list = [
'2010', '2011', '2012', '2013', '2014', '2015',
'2016', '2017', '2018', '2019', '2020', '2021'
];
editor.jqxDropDownList({
autoDropDownHeight: true,
source: list,
selectedIndex: 0
});
},
cellsrenderer : (row, columnfield, value, defaulthtml, columnproperties) =>
$("#jqxgrid").jqxGrid('getrowdata', row).yeareligible === 'N'
? '<p>N/A</p>'
: defaulthtml
},
{
text: 'Product',
datafield: 'productname',
width: 180
}, {
text: 'Quantity',
datafield: 'quantity',
width: 80,
cellsalign: 'right'
}, {
text: 'Unit Price',
datafield: 'price',
width: 90,
cellsalign: 'right',
cellsformat: 'c2'
},
{
text: 'Total',
datafield: 'total',
width: 100,
cellsalign: 'right',
cellsformat: 'c2'
}
]
});
// Addendum for OP's purposes
// Set default year to 2016 if available is checked
$("#jqxgrid").on('cellvaluechanged', function (evt)
{
let args = evt.args;
if (args.datafield === 'available' && args.newvalue)
{
let rowData = $('#jqxgrid').jqxGrid('getrowdata', args.rowindex);
if (rowData.yeareligible === 'N')
return;
$('#jqxgrid').jqxGrid('setcellvalue', args.rowindex, 'yearValue', 2016);
}
});
$('#jqxbutton').click(function()
{
let rows = $('#jqxgrid').jqxGrid('getrows');
let selectedRows = rows
let count = selectedRows.filter(x => x.available).length;
console.log("Printing selected rows/count length");
console.log(count);
rows.filter(x => x.available)
.map(x =>
x.yeareligible === 'N'
? Object.assign({}, x, {yearValue : '-1'})
: Object.assign({}, {yearValue : '2016'}, x)
);
console.log(selectedRows);
});
<link href="https://jqwidgets.com/public/jqwidgets/styles/jqx.energyblue.css" rel="stylesheet" />
<link href="https://jqwidgets.com/public/jqwidgets/styles/jqx.base.css" rel="stylesheet" />
<script src="https://jqwidgets.com/public/jqwidgets/jqx-all.js"></script>
<div id="jqxgrid"></div>
<input type="button" style="margin: 50px;" id="jqxbutton" value="Get rows" />
【问题讨论】:
标签: javascript jqwidget