1
Help for ParamQuery Pro / Re: Nested grid with track and shared source?
« on: May 16, 2014, 09:49:30 pm »
Yes. Makes sens. Thank you.
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>PQtest</title>
<link rel="stylesheet" href="pqgrid.dev.css">
<script src="jquery-1.10.2.js"></script>
<script src="jquery-ui-1.10.4.custom.js"></script>
<script src="pqgrid.dev.js"></script>
<script>
$(document).ready(function () {
/////// Put this in a file named "template.json" in the same directiry ////////////
// [
// { "ID": 11, "Name": "Test 1", "Attributes": [{"ID": "txt11" }, {"ID": "txt12" } ,{"ID": "txt13" }] },
// { "ID": 22, "Name": "Test 2", "Attributes": [{"ID": "txt21" }, {"ID": "txt22" } ,{"ID": "txt33" }] },
// { "ID": 33, "Name": "Test 3", "Attributes": [{"ID": "txt31" }, {"ID": "txt32" } ,{"ID": "txt33" }] }
// ]
var localTemplates = new Array;
var newItemRowID = 0;
var colTemplateModel = [
{ title: "", minWidth: 27, width: 27, type: "detail", resizable: false, editable: false },
{ title: "DbID", width: 30, dataType: "integer", dataIndx: "ID", editable: false, hidden: true },
{ title: "Name", minWidth: 150, dataType: "string", resizable: true, dataIndx: "Name", editable: true }
];
var $grid = $("#templateDataGrid").pqGrid({
width: 700,
height: 400,
dataModel: {
dataType: "JSON",
location: "remote",
recIndx: "ID",
url: "template.json",
getData: function (response) {
//console.log(response);
return { data: response };
}
},
colModel: colTemplateModel,
flexHeight: true,
wrap: false,
//flexWidth: true,
selectionModel: {
type: ''
},
scrollModel: {
//autoFit: true
},
hoverMode: 'cell',
editModel: {
saveKey: $.ui.keyCode.ENTER
},
title: "Templates",
track: true,
toolbar: {
items: [
{
type: 'button', icon: 'ui-icon-minus', label: 'Apply', listeners: [
{
"click": function (evt, ui) {
var $grid = $(this).closest('.pq-grid');
// AcceptTemplateChanges($grid);
}
}
]
}
]
},
detailModel: {
cache: true,
collapseIcon: "ui-icon-plus",
expandIcon: "ui-icon-minus",
init: function (ui) {
var rowDataMain = ui.rowData,
templateID = rowDataMain["ID"],
rowIndxMain = ui.rowIndx,
uiMain = ui;
localTemplates.push(rowDataMain);
//console.log(ui);
//Attributes
var colAttribModel = [
{ title: "Attribute name", width: 150, dataType: "string", dataIndx: "ID" },
{ title: "Default data", width: 200, dataType: "string", dataIndx: "Data" },
{ title: "", editable: false, width: 63, sortable: false, render: function (ui) { return "<button type='button' class='delete_attrib'>Delete</button>"; } }
];
var dataAttribModel = { data: rowDataMain["Attributes"], location: "local" };
var $attribGrid = {
width: 500, height: 200,
dataModel: dataAttribModel,
colModel: colAttribModel,
flexHeight: true,
title: "Attributes",
scrollModel: {
autoFit: true,
lastColumn: "auto"
},
wrap: false,
showTop: true,
showBottom: false,
collapsible: false,
showTitle: false,
oddRowsHighlight: true,
hoverMode: 'cell',
editModel: {
saveKey: $.ui.keyCode.ENTER
},
track: true, //to turn on the track changes.
cellSave: function (event, ui) {
//console.log(event);
//console.log(rowDataMain);
//Used for setting local changes data.
var rowData = ui.rowData;
delete rowData.pq_rowselect;
var found = false;
if (localTemplates.length > 0) {
localTemplates.forEach(function (templ) {
if (templateID == templ["ID"]) {
var data = { Data: "", ID: "" };
if (ui.dataIndx == "ID") {
data.ID = rowData.ID;
}
else if (ui.dataIndx == "Data") {
data.Data = rowData.Data;
}
if (ui.rowIndx > templ.Attributes.length - 1) {
templ.Attributes.push(data);
}
else {
//console.log(templ.Attributes[ui.rowIndx]);
//console.log(rowData);
templ.Attributes[ui.rowIndx][rowData] = rowData[ui.rowIndx];
}
}
});
// console.log(localTemplates);
//Used for updating data grid with latest data and med it marked for change
var detailIndx = $(this).closest('.pq-grid').parent().parent().attr("pq-row-indx");
var masterIndx = $('#templateDataGrid').find("tr.pq-detail-master[pq-row-indx=" + detailIndx + "]");
var masterIndexNr = $(masterIndx).attr('pq-row-indx');
$('#templateDataGrid').pqGrid("updateRow", { rowIndx: parseInt(masterIndexNr), row: { 'Name': rowDataMain.Name + ' ' } });
$('#templateDataGrid').pqGrid("updateRow", { rowIndx: parseInt(masterIndexNr), row: { 'Name': rowDataMain.Name } });
//console.log(uiMain);
//console.log(ui);
}
},
toolbar: {
items: [
{
type: 'button', icon: 'ui-icon-plus', label: 'Add attribute', listeners: [
{
"click": function (evt, ui) {
var $grid = $(this).closest('.pq-grid');
//append empty row at the end.
var rowDataEmpty = {}; //empty row
var rowIndx = $grid.pqGrid("addRow", { rowData: rowDataEmpty });
$grid.pqGrid("goToPage", { rowIndx: rowIndx });
$grid.pqGrid("editFirstCellInRow", { rowIndx: rowIndx });
}
}
]
}
]
},
refresh: function () {
//debugger;
var $grid = $(this);
if (!$grid) {
return;
}
//delete button
$grid.find("button.delete_attrib").button({ icons: { primary: 'ui-icon-close' } })
.unbind("click")
.bind("click", function (evt) {
var $grid = $(this).closest('.pq-grid');
if (isEditing($grid)) {
return false;
}
var $tr = $(this).closest("tr"),
rowIndx = $grid.pqGrid("getRowIndx", { $tr: $tr }).rowIndx;
DeleteDetailRow(rowIndx, $grid, localTemplates, templateID);
});
//rows which were in edit mode before refresh, put them in edit mode again.
var rows = $grid.pqGrid("getRowsByClass", { cls: 'pq-row-edit' });
if (rows.length > 0) {
var rowIndx = rows[0].rowIndx;
//editRow(rowIndx, $grid);
}
}
};
var $grid = $("<div></div>").pqGrid($attribGrid);
return $grid;
}
}
});
});
</script>
</head>
<body class="Test">
<div id="templateDataGrid" class="templateManagerGrid" style="overflow: hidden; " >
</div>
</body>
</html>