The Grid Class¶
- class djqgrid.grid.Grid(**kwargs)¶
The Grid class everybody will use.
We use BaseGrid, and add the fields metaclass magic, as is done in the Django forms
Creates a Grid
- Args:
- **kwargs: All the arguments will go to the grid’s option array.
The grid’s option array will be used to instantiate the jqGrid on the browser side: $("#grid").jqGrid(options)
If you want to pass handlers for events, such as loadComplete, wrap then with function so that the options are rendered correctly in JavaScript. For example:
grid = MyGrid(loadComplete=function('loadCompleteHandler'))
For more information, see the json_helpers module.
- _apply_query(queryset, querydict)¶
This function lets a Grid instance change the default query, if it’s ever necessary
- Args:
- queryset: The default query (self.model.objects) querydict: The request’s query string
- Returns:
- The actual queryset to use
Override this function if the default query is not enough
- _apply_sort(queryset, querydict)¶
Applys sorting on the queryset.
jqGrid supports sorting, by passing sidx and sord in the request’s query string. _apply_sort applies this sorting on a queryset.
- Args:
- queryset: A queryset for the objects of tje grid querydict: The request’s querydict with sidx and sord
- Returns:
- An ordered queryset.
- _get_additional_data(model)¶
Retrieves additional data to be sent back to the client.
- Args:
- model: The model of the currently rendered row
- Returns:
- A dictionary with more data that will be sent to the client, or None if there’s no such data
- _get_query_results(querydict)¶
Returns a queryset to populate the grid
- Args:
- querydict: The request’s query dictionary
- Returns:
- The queryset that will be used to populate the grid. Paging will be applied by the caller.
- _model_to_dict(model)¶
Takes a model and converts it to a Python dictionary that will be sent to the jqGrid.
This is done by going over all the columns, and rendering each of them to both text and HTML. The text is put in the result dictionary. The result dictionary also has an html dictionary, which contains the HTML rendering of each column.
This is done to solve a bug with jqGrid’s inline editing, that couldn’t handle HTML values of cells properly. So instead we use a formatter, called customHtmlFormatter (in djqgrid_utils.js) that can take the value from the html dictionary.
Sometimes more information is required by the JavaScript code (for example, to choose row CSS styles). It is possible to add additional information. The method _get_additional_data returns this additional information, which is put in result JSON as well.
So, for example, a Grid with two columns will have a JSON looking likes this:- {col1: ‘col1-text’,
col2: ‘col2-text’, html: {
‘col1’: ‘col1-html’, ‘col2’: ‘col2-html’}, additional: { ... }
}
- classmethod get_grid_id()¶
Returns the grid’s class ID.
This is done by computing a CRC32 of the class’s name. Using CRC32 is not a security risk - IDs are passed in the HTTP anyway, so they are no secret and being able to generate them is not going to help an attacker.
- get_json_data(querydict)¶
Returns a JSON string with the grid’s contents
- Args:
- querydict: The request’s query dictionary
- Returns:
- JSON string with the grid’s contents
DO NOT override this method unless absolutely necessary. _apply_query and _get_additional_data should be overridden instead.
- get_options(override=None)¶
Returns the grid’s options - this options will be passed to the JavaScript jqGrid function
- Args:
- override: A dictionary that overrides the options provided when the grid was initialized.
- Returns:
- A dictionary with the grid’s options.
Some fields cannot be overridden: - colNames are created from the grid’s Column fields - colModels are also created from the grid’s Column fields. - url always points to the djqgrid.views.query view with the grid’s ID.