1

Good day!

I am working on a project right now and I am using JQGrid to show my data. As part of its functions, users can choose columns and make these columns as their default columns.

I used 'columnChooser' to let my user select for their default columns.

My problem now, how to retrieve those columns the user's selected?
And how to set those columns as their default columns?

Can someone help me with this problem.

Thanks

Jason

RJ.
  • 77
  • 3
  • 10

1 Answers1

3

After the user changes the column layout, you can get the colModel from the grid, iterate through it and push the configuration in a array of json objects which will then be sent to the server. The following code does this:

function saveColumnConfiguration(grid, url) {
    if (url.length > 0) {
        var colArray = new Array();
        var colModel = grid[0].p.colModel;
        for (var i = 0; i < colModel.length; i++) {
            if (colModel[i].name != "rn" && colModel[i].name != "cb") {
                colArray.push({
                    Name: colModel[i].name,
                    Width: colModel[i].width,
                    Visible: !colModel[i].hidden
                });
            }
        }
        $.ajax({
            url: url,
            type: 'POST',
            data: 'columnConfiguration=' + JSON.stringify(colArray)
        });
    }
}

The check for "rn" and "cb" means don't take the rownumber and checkbox columns.

UPDATE

You will need a class to represent the columns:

[Serializable]
public class JqGridColumn
{
    public string Name { get; set; }
    public int Width { get; set; }
    public bool Visible { get; set; }
}

You also need custom model binder to deserialize the incoming list:

public class JqGridConfigurationModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var conf = bindingContext.ValueProvider.GetValue("columnConfiguration").AttemptedValue;

        JavaScriptSerializer serializer = new JavaScriptSerializer();
        var configuration = serializer.Deserialize<IEnumerable<JqGridColumn>>(conf);

        return configuration;
    }
}

Register the model binder in application start:

ModelBinders.Binders.Add(typeof(IEnumerable<JqGridColumn>), new JqGridConfigurationModelBinder());

The action in the controller that handles the list will be something like this:

public void SaveColumnConfiguration(IEnumerable<JqGridColumn> columnConfiguration)
{
    // Save the list accordingly...
}

Note that the order of the columns is represented by their position in the list. You can then easily read this configuration and render the grid.

UPDATE 2

The function in your case should be called like this

saveColumnConfiguration($("#freight_bill"), "/Controller/Action");

But not after the call for columnChooser. You can either make another button to save changes when the user chooses to do so or handle the done event from the column chooser like this:

$("#freight_bill").jqGrid('columnChooser', {
    done: function (perm) {
            if (perm) { 
                $("#freight_bill").jqGrid("remapColumns", perm, true, false); 
            }
            saveColumnConfiguration($("#freight_bill"), "/Controller/Action");
        }
});
shizik
  • 910
  • 6
  • 16
  • Hello Shizik I have tried your code and its not working :(. Is there any other ways to do this? – RJ. Jul 20 '11 at 00:57
  • I see the question is tagged in asp.net mvc. If that is the case you need to make custom model binder that will transform the incoming list. I will update the answer accordingly. – shizik Jul 20 '11 at 09:39
  • Hello Shizik, I have an error the moment I added ModelBinders.Binders.Add(typeof(IEnumerable), new JqGridConfigurationModelBinder()); in my global file under Application_Start(). – RJ. Jul 28 '11 at 02:42
  • It says, "Argument '2': cannot convert from 'APR_Plus.JqGridConfigurationModelBinder' to 'System.Web.Mvc.IModelBinder'" and "The best overloaded method match for 'System.Web.Mvc.ModelBinderDictionary.Add(System.Type, System.Web.Mvc.IModelBinder)' has some invalid arguments" – RJ. Jul 28 '11 at 02:43
  • I have fixed the error already... :) i'll continue working now. thanks a lot – RJ. Jul 28 '11 at 02:51
  • Hello Shizik, I have added all codes in my project now. My question is this, the function above saveColumnConfiguration(grid, url) will be included in my view file right? so now, when calling this function what would be the values for grid and url? Sorry but still naive when it comes to JQGrid... And function saveColumnConfiguration(grid, url) will be called in here? "onClickButton: function() { $("#freight_bill").jqGrid('columnChooser'); saveColumnConfiguration(?, ?); }" Thanks a lot.... RJ – RJ. Jul 28 '11 at 03:03
  • Useful, detailed response! Thank you! – Steve Sep 15 '11 at 00:21