|
// Global variable to hold reference to Web Rule
// (optional, just for the sake of convenience)
var webrule;
// Init everything on page load
$(function ()
{
// Use Web Rule's global shortcut $ce to get reference to
// the control by its server ID (not its client ID)
webrule = $ce("filterControl");
$("#ddlEntity").change(loadSettings);
$("#ddlTheme").change(loadSettings);
$("#chkHelp").click(loadSettings);
$("#spnSearch").click(search);
$("#spnClear").click(search);
// Finally, get filter's settings
loadSettings();
});
// Method that requests Web Rule UI settings from the
// server and initializes Web Rule
function loadSettings()
{
clear();
// Call the LoadSettings MVC action declared in the AjaxController to get the settings
post("/LoadSettings", JSON.stringify({ entity: $("#ddlEntity option:selected").val(), theme: $("#ddlTheme option:selected").val(), help: $("#chkHelp").is(":checked")}), settingsLoaded);
};
function settingsLoaded(data)
{
// Pass the received json string to Web Rule
webrule.loadSettings(data);
};
function search()
{
// Calls the search() server action and passes the search result to searched() function
post("/Search", JSON.stringify({ entity: $("#ddlEntity option:selected").val(), clientData: webrule.extract() }), searched);
};
function searched(data)
{
// Reset the grid
var grid = $("#divGrid").html("");
if (data.IsFilterEmpty)
{
// The filter is empty. Notify the user and do nothing
$("#spnInfo").text("The filter is empty");
}
else if (!data.IsFilterValid)
{
// The filter is invalid. Notify the user (optionally) and pass the invalid data to Web Rule
$("#spnInfo").text("The filter is invalid");
webrule.loadInvalids(data.ClientInvalidData);
}
else
{
if (data.Records.length == 0)
{
// No matching records found
$("#spnInfo").text("No records found");
}
else
{
// For all table haters out there: relax guys, it's a TABULAR data :)
var tbl = document.createElement("TABLE"), row, cell, label, list;
tbl.cellPadding = tbl.cellSpacing = tbl.border = 0;
tbl.className = "filter";
// headers
row = tbl.insertRow(-1);
for (var i in data.Headers)
{
label = data.Headers[i];
cell = row.insertCell(-1);
cell.className = "filterHeader";
$(cell).text(label);
}
var alt = false;
// Going through the records, constructing each record as a table row.
for (var i in data.Records)
{
row = tbl.insertRow(-1);
list = data.Records[i];
row.className = alt ? "filterAltRow" : "filterRow";
// Using headers as a counter, going through all records and constructing
// each row by adding cells, where each cell represents a label in the record
for (var j in data.Headers)
{
cell = row.insertCell(-1);
cell.className = "filterCell";
$(cell).text(list[j]);
}
alt = !alt;
}
// Finally, add the table to the grid's div container
grid.append(tbl);
$("#spnInfo").text(data.Records.length + " records found");
}
}
};
// Clears the Filter Area, removes the grid table and resets the info message
function clear()
{
webrule.clear();
$("#divGrid").html("");
$("#spnInfo").text("Demo version. Search is delayed for 1 second.");
};
// Utility wrapper that handles HTTP posts to the server.
// jQuery is used here as an example; you can use any framework
// that wraps calls to XmlHttpRequest
function post(url, data, delegate)
{
$.ajax({
type: "POST",
url: window.location.pathname + url,
contentType: "application/json; charset=utf-8",
dataType: "json",
data: data,
cache: false,
success: delegate,
error: error /*all errored calls will be delegated to the error() method declared below*/
});
};
// Handler of errored server calls
function error(e)
{
if (!e) throw Eror("Generic server error.");
else
{
// We are using properties of the complex "e" error object to notify
// the user about the error. This error handling has nothing
// to do with Web Rule and exists here only for the sake of completeness.
if (e.responseText)
{
$("#spnInfo").text("Server error");
alert(e.responseText);
}
else throw Error(e);
}
};
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.