gridview的sort_Gridview自动排序功能的实现

ViewState["sortExpression"] = "Isdistribution";

ViewState["sort"] = " ASC";

}

//绑定信息

BindNodeInfo();

}

public void BindNodeInfo()

{

NodeLogic log = new NodeLogic();

DataSet myset = log.GetNodeInfo();     //获取数据源

DataView myview = myset.Tables[0].DefaultView;

myview.Sort = ViewState["sortExpression"].ToString() +" "+ ViewState["sort"].ToString();

this.NodeGridView.DataSource = myview;

NodeGridView.DataKeyNames = new string[] { "node_id" };               //设置主键字段

NodeGridView.DataBind();                                                  //绑定GridView控件

}

protected void NodeGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)

{

this.NodeGridView.PageIndex = e.NewPageIndex;

BindNodeInfo();

}

protected void NodeGridView_RowDataBound(object sender, GridViewRowEventArgs e)

{

// 自动给第一列编号

if (e.Row.RowIndex > -1)

{

e.Row.Cells[0].Text = Convert.ToString(e.Row.RowIndex + 1);

}

}

protected void NodeGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)

{

NodeLogic log = new NodeLogic();

int id = int.Parse(this.NodeGridView.DataKeys[e.RowIndex].Values[0].ToString());

if (log.DeleteNodeInfo(id))

{

this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('删除成功!');", true);

}

else

this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('删除失败!');", true);

//重新更新数据显示

BindNodeInfo();

}

protected void NodemGridView_RowEditing(object sender, GridViewEditEventArgs e)

{

}

protected void AddNode_Click(object sender, EventArgs e)

{

Response.Redirect("AddNode.aspx");

}

protected void NodeGridView_Sorting(object sender, GridViewSortEventArgs e)

{

if (ViewState["sortExpression"] != null)

{

if (ViewState["sort"].ToString() == "Asc")

{

ViewState["sort"] = "Desc";

}

else

{

ViewState["sort"] = "Asc";

}

}

BindNodeInfo();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要给Yii2-GridView中的自定义字段增加排序功能,可以通过以下步骤实现: 1. 在GridView的columns属性中,为自定义字段添加‘attribute’属性(指定排序用的字段),‘label’属性(指定在表头显示的名称),以及‘value’属性(指定自定义字段的值)。 2. 在GridView的dataProvider属性中,为排序用的字段添加一个Sort属性。 3. 在GridView中的排序链接中,为自定义字段添加一个sort参数,指定排序用的字段。 以下是一个示例代码: ``` use yii\grid\GridView; use yii\data\ActiveDataProvider; use yii\data\Sort; $dataProvider = new ActiveDataProvider([ 'query' => $query, 'sort' => [ 'attributes' => [ 'custom_field' => [ 'asc' => ['custom_field' => SORT_ASC], 'desc' => ['custom_field' => SORT_DESC], 'label' => 'Custom Field', ], ], ], ]); echo GridView::widget([ 'dataProvider' => $dataProvider, 'columns' => [ 'id', 'name', [ 'attribute' => 'custom_field', 'label' => 'Custom Field', 'value' => function ($model) { return $model->customField; } ], ], 'sorter' => [ 'attributes' => [ 'custom_field' => [ 'asc' => ['custom_field' => SORT_ASC], 'desc' => ['custom_field' => SORT_DESC], ], ], ], ]); ``` 在以上代码中,我们为自定义字段‘custom_field’添加了一个Sort属性,然后在GridView中为该字段添加了‘attribute’、‘label’和‘value’属性。最后,在排序链接中为自定义字段添加了‘sort’参数,指定排序用的字段。这样就可以为Yii2-GridView中的自定义字段增加排序功能了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值