1

良い一日!

現在プロジェクトに取り組んでおり、JQGridを使用してデータを表示しています。その機能の一部として、ユーザーは列を選択し、これらの列をデフォルトの列にすることができます。

'columnChooser'を使用して、ユーザーがデフォルトの列を選択できるようにしました。

私の問題は、ユーザーが選択した列を取得する方法ですか?
そして、それらの列をデフォルトの列として設定するにはどうすればよいですか?

誰かがこの問題で私を助けることができますか?

ありがとう

ジェイソン

4

1 に答える 1

3

ユーザーが列のレイアウトを変更した後、グリッドからcolModelを取得し、それを反復処理して、サーバーに送信されるjsonオブジェクトの配列に構成をプッシュできます。次のコードはこれを行います:

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)
        });
    }
}

「rn」と「cb」のチェックは、行番号とチェックボックスの列を取得しないことを意味します。

アップデート

列を表すクラスが必要になります。

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

着信リストを逆シリアル化するには、カスタムモデルバインダーも必要です。

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;
    }
}

アプリケーション開始時にモデルバインダーを登録します。

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

リストを処理するコントローラーのアクションは次のようになります。

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

列の順序は、リスト内の位置で表されることに注意してください。その後、この構成を簡単に読み取ってグリッドをレンダリングできます。

更新2

あなたの場合の関数はこのように呼び出されるべきです

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

しかし、columnChooserの呼び出し後ではありません。ユーザーが変更を保存することを選択したときに別のボタンを作成するか、done次のように列チューザーからイベントを処理することができます。

$("#freight_bill").jqGrid('columnChooser', {
    done: function (perm) {
            if (perm) { 
                $("#freight_bill").jqGrid("remapColumns", perm, true, false); 
            }
            saveColumnConfiguration($("#freight_bill"), "/Controller/Action");
        }
});
于 2011-07-19T06:35:28.270 に答える